Jump to content

Need help for my first script


Recommended Posts

Hello,

I'd like to write a autoit script that basically goes to a site, opens some web pages of that site, and does some stuff in these pages.

I'd like to use Firefox (a personal version, actually) to do these tasks.

Please help me, as I've tried really hard. This might be a bit complicated for a first script.

I'll take the autoitscript forum as an example for the site I'd like to go to, even though it's not the case.

Here is my algorithm (it can be improved - if you have some ideas, they're welcome, I have some ideas too - but for now, let's try to make a script with that algorithm) :

1- Run firefox

2- If a window opens and says that firefox is already running, press enter (it will close the firefox that is running), and go to the step 1-.

3- Wait for 15 seconds for Firefox to open.

4a- First possibility: it didn't open. if a window opens and says that Add-on updates are available, then close that window, and continue to the next step (5-). Else, stop the whole script, and create a message window untitled Error saying that Firefox didn't open.

4b- Second possibility : it opened. Then continue to the next step (5-).

5- Maximize the Firefox window (alt+space+up+up+enter).

6- Press F6, and write the url. For example : http://www.autoitscript.com/forum/index.ph...orum=2&st=0.

7- Wait for the page to load completely (I wrote a function called _wait_load() in order to do that).

8- If there's a problem loading page (connection issue), then close firefox and go to the step 1-.

9- Search for a word, with ctrl+F. For example, "last post by".

10- Make the mouse go to the selected word. With my version of firefox, it's easy, as the word is overlined in green.

11- Click the word (in my case, it's actually a link right above the word) that was searched.

12- Same as 7-.

13- Same as 8-.

14- Do some things in that page (for example, copy paste the topic into a .txt)

15- Come back to the previous page, that shows all the topics (alt+left).

16- Find the next "last post by" (ctrl+G) (Firefox saves the position of the last one that was overlined).

17- Redo the steps 10 to 16 included, 20 times (as there are 20 topics displayed).

18- Redo the steps 6 to 17 included, 2274 times (there are 2274 pages in the subforum "General Help"), but change the last part of the url of the step 6. In my case, the number just needs to be increased by 1, but in the case of the autoitscript forum, the number needs to be increased by 20 : st=0, st=20, st=40, ...

19- Make a message box saying that the script is finished.

This script only needs to be used by me. As a result, I sometimes used specific numbers, that "Autoit window info" gave me.

Don't bother about the numbers, just tell me if it looks correct.

Here is the script that I made :

Func  _wait_load() ;->waits for the page to load
    $initialsum=1293582875
    While PixelChecksum(1084,975,1180,988)=$initialsum
        Sleep(50)
    WEnd
    While PixelChecksum(1084,975,1180,988)=$initialsum
        Sleep(100)
    WEnd
EndFunc  ; ->_wait_load

Global $p=0 ; this is the number of the pages that have already been done (see the url below).
Global $c=0 ; this is the number of the topics that have been dealt with successfully (it's just a variable that is meant to count the number of success operations).

While 1
Run("C:\Program Files\Mozilla Firefox\firefox.exe")
If WinWaitActive("Mozilla Firefox is already running") Then 
    Send("{Enter}")
    continueLoop 
EndIf ; If firefox is already running, shut it down and go to step 1.
If Not(WinWait("Google - Mozilla Firefox",15)) Then   
    If WinExists("Firefox Add-on Updates") Then
        WinClose("Firefox Add-on Updates") ; close available updates.
    else
    MsgBox(0,"Error","Firefox didn't open")
    EndIf
EndIf
WinSetState("Google - Mozilla Firefox","",@SW_MAXIMIZE)
WinActivate("Google - Mozilla Firefox")

While $p<=2274 ; number of pages
Sleep(3000)
Send("{F6}http://www.autoitscript.com/forum/index.php?showforum=2&st="&(20*$p)) ; 
_wait_load()
If WinActive("Problem loading page - Mozilla Firefox") Then
    Winclose("Problem loading page - Mozilla Firefox") 
    ContinueLoop 2
EndIf
Send("^f")
Send("last post by")
For t = 1 to 19 ; there are 20 topics, so this action must be redone 19 times.
local $coord=PixelSearch(400,300,700,950,0x38D878)
If Not(@error) Then
    MouseMove($coord[0]+30,$coord[1]-12)
    EndIf ;don't mind about the specific numbers.
MouseClick()
_wait_load()
If WinActive("Problem loading page - Mozilla Firefox") Then
    WinClose("Problem loading page - Mozilla Firefox")
    ContinueLoop 3
EndIf
; 
; here will be all the operations that need to be done for each topic, such as copy/paste it into a .txt.
;
$c=$c+1 ; c is the number of successfull operations made during the whole script.
Send("!{LEFT}") ; go to previous page.
Send("^g") ;next research.
Next 
$p=$p+1
WEnd
    
ExitLoop ; If we reached this point, we did all we needed to do. Exit the main loop.
WEnd
MsgBox(0,"Congratulations","The script has ended successfully")

Please help me and tell me if there are any mistakes.

Thanks.

PS: I'm sorry my English is not perfect, I hope you understood me. Otherwise, tell me what you didn't understand, and I'll try to say it again.

Edited by Elishac
Link to comment
Share on other sites

  • Replies 49
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

Now I can test it. There aren't any mistakes anymore (at least there isn't any error message window).

It runs firefox, but then it stops, and doesn't do anything else.

Can you see what is wrong ?

Link to comment
Share on other sites

Yeah this is my very first one. But there must be a lot of mistakes. That's why I need your help.

(besides, this is my second topic, and I asked some questions in the first one :-), they helped me a lot).

Please help me :-).

(I made the _wait_load function separately though - so this is my second one - but my second script includes my first script :-)).

Link to comment
Share on other sites

Hint #1: Don't use WinActive() immediately after Run(). The executable takes time to create its GUI, but you are testing if it is active within microseconds of starting. You might try WinWaitActive() instead (see the help file).

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

This is a total waste of time while you can use _IE to do these things and it is much easier that way. My collection of IE automation include AutoIt forum You can simply strip code from there and continue on.

1- Run firefox

2- If a window opens and says that firefox is already running, press enter (it will close the firefox that is running), and go to the step 1-.

I don't see how this can happen since firefox can always have multi-windows(unless you somehow customize it which the option is stupid as it sounded)

3- Wait for 15 seconds for Firefox to open.

_IELoadWait can make sure all the elements in a page is loaded and the above method you used is not accurate

5- Maximize the Firefox window (alt+space+up+up+enter).

Please look up WinSetState()

6- Press F6, and write the url. For example : http://www.autoitscript.com/forum/index.ph...orum=2&st=0.

Can be replaced by _IENavigate(), again your method is not accurate and sometimes if there's a pop up your script will fail completely

7- Wait for the page to load completely (I wrote a function called _wait_load() in order to do that).

Again _IELoadWait, you can set the timeout depends on your internet connection

9- Search for a word, with ctrl+F. For example, "last post by".

10- Make the mouse go to the selected word. With my version of firefox, it's easy, as the word is overlined in green.

Every version of firefox will highlight the word green if found, not a reliable method. To search and click on text you can simply use _IELinkClickByText, if error you can use error handler and code for coordinated respond

14- Do some things in that page (for example, copy paste the topic into a .txt)

As unreliable as all of your above methods you can either strip down the html code or use send

In the end it is totally unnecessary to use firefox since there's almost nothing IE can't do that firefox can. You have no advantage over using firefox, in fact disadvantage(s) because your method of checking events are totally unreliable. Your whole script can even be replicated by most of the marco program, in fact look into those because it is easier.

Link to comment
Share on other sites

Thanks for your answer, Generator.

I've looked up WinSetState(), and I've made the appropriate modifications.

Except from that suggestion, all you are saying is that I'd better use Internet Explorer. I know that, some people already told me. But I'd rather use Firefox, for some other reasons. Now, given my choice, can you please help me to make my script work ? I know it won't be perfect, and not reliable, but please help me to make it as good as possible.

At this very moment, my script is not working at all, which is not normal, I guess, so please help me.

Link to comment
Share on other sites

Slow down a bit on the bumps... but also your gonna have to get more specific about what doesn't work.

While ProcessExists('Andrews bad day.exe')
	BlockInput(1)
	SoundPlay('Music.wav')
	SoundSetWaveVolume('Louder')
WEnd
Link to comment
Share on other sites

WinActivate("Google - Mozilla Firefox")

While $p<=2274 ; number of pages
Send("{F6}http://www.autoitscript.com/forum/index.php?showforum=2&st="&(20*$p)) ;

What is this thing for anyway? In the code above, try adding a sleep of a few seconds before your send command and see if that works. Could be the send command is sending before the window is ready.

While ProcessExists('Andrews bad day.exe')
	BlockInput(1)
	SoundPlay('Music.wav')
	SoundSetWaveVolume('Louder')
WEnd
Link to comment
Share on other sites

Someone,

What is this thing for anyway?

What are you referring to when you say "What is this thing for anyway?" ?

If you're referring to your quote, then the first line is meant to activate the mozilla firefox window, (even though it should already be activated, given the previous lines of the script).

The second one is the beginning of a loop that will be made 2274 times, given that there are 2274 pages in the subforum called "general help and support".

The third line is meant to write the url that leads to the forum. In the end, st=0, st=20, st=40, st=..., st=20p, where p is the number of pages. For instance, in the first loop, Send("{F6}http://www.autoitscript.com/forum/index.php?showforum=2&st=0).

In the code above, try adding a sleep of a few seconds before your send command and see if that works.

I've tried, it doesn't work.

ZoSTeR,

Your flawed approach aside this might help you a little: Firefox command line arguments

No need for that F6 stuff at all.

I don't speak English fluently and I'm not sure I've understood you completely.

Are you talking about this :

"C:\Program Files\Mozilla Firefox\firefox.exe" -new-window http://www.google.com/

?

Are you talking about something else than that line or is the link you gave only is significant because of that line ?

Anyway, this line doesn't work on my computer, and anyway, I need to write 2274 times an URL. Thus, I don't think I can use this run command, or else it would open 2274 Firefox windows.

As said before you might be much more successful with _INetGetSource or _InetGet and a couple of string searches or regular expressions.

Does this work with Firefox too ? Anyway, how could I have any use of these functions ? Edited by Elishac
Link to comment
Share on other sites

Did you try adding the sleep? I get what those commands do, but its entirely possible that you are sending the keystrokes before the window is activated. For instance if I run this;

Run("C:\Program Files\Internet Explorer\IEXPLORE.exe")
WinActivate("Enterprise") ;my home page
;Sleep(3000)
Send("{F6}http://www.autoitscript.com/forum/index.php?showtopic=71098&st=0")

It never works properly. If you comment out the sleep command it does work. Yes I know this test uses IE but I don't have firefox installed.

While ProcessExists('Andrews bad day.exe')
	BlockInput(1)
	SoundPlay('Music.wav')
	SoundSetWaveVolume('Louder')
WEnd
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...