Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate

Wait for Webpage?


  • Please log in to reply
11 replies to this topic
oliverkahn
  • Guests
  • Last active:
  • Joined: --
Hello,
i would like to open a url in the standard browser (ie), wait for the page to load and then do other things on the page.
Does anyone have an idea how i could do that?
thx a lot


Chris
  • Administrators
  • 10727 posts
  • Last active: Nov 15 2014 07:47 AM
  • Joined: 02 Mar 2004
Since you mentioned that MSIE is your browser, I think something like this will work:
Run, www.yahoo.com
WinWait, Yahoo! - Microsoft Internet Explorer
WinActivate
StatusBarWait, Done, 10
if ErrorLevel <> 0
   MsgBox, The wait timed out or the window was closed.
else
   MsgBox, The page is done loading.

I think I read some other techniques for doing this sort of thing on the AutoIt forum, some of which may be more reliable.

skrommel
  • Guests
  • Last active:
  • Joined: --
The previous tip won't work on pages with frames!

I use Naviscope (www.naviscope.com) to check for network activity. It shows all open connections, and can be set to autohide.

Just check if the window exists!

Anatoly Larkin
  • Guests
  • Last active:
  • Joined: --
I have struggled with this and found an answer that is pretty reliable (the slower you go - see below).
See, if this is useful for you.
1. The previous reply is correct in saying that it won't work on a page with frames. Namely, you mouse has to be over the main window space for the StatusBar to display the correct loading information - Done being the last message displayed.
----> USe MouseMove command to move the mouse to such area. (Experiment where such area would be most likely to occur - how about the status bar itself?)
2. Once the mouse is in place you have to wait for the final Done.
Notice the word "Final". In IE (unlike some other browsers) Done tends to flash frequently between other messages - therefore StatusBarWait, Done is an inadequate command here.
The way to tell the page is Done loading is by checking that Done has been showing consistently (wihtout flickering) for good 2-3 seconds (you can try less - but sometimes (especially with dial up - it can stay on for long, even while the page is still loading.)
That way you know everything has been loaded and you are ready to start working.
Here is a special Loop procedure that you can use by Gosubbing to it every time you need to wait for the page to finish loading.


LabelWaitDone:
{
;"Done" Loop
;this loop checks to make sure that the StatusBar is set on Done. ;Sometimes Done flashes so often that we need to have a way ; of testing the fact that Done hadn't changed to other strings ;for at least 2 seconds (loop, iterations - iterations take a small
;amount of time)
Loop
{
StatusBarGetText, statusBarText,,%winTitle%
if statusBarText = Done
{
Loop,%looptimes%
{
StatusBarGetText, statusBarText,,%winTitle%
if statusBarText <> Done
break
}
}
;closes the If statement block
if statusBarText = Done
break
;otherwise it means that StatusBar is not
;set on Done at this time
}

return
}


As you can see, as soon as StatusBar shows Done the procedure enters the inner loop where the %looptimes% number is retrieved from elsewhere in your script. 300 is about 2 seconds worth of looping. (Experiment on your system). If, after %looptimes% iterations, the StatusBar text is still showing Done (rather than some other value because it was not the final Done), it exits the loops and returns to your script.

Let me know how it works out.

Anatoly Larkin :)

NiJo
  • Members
  • 73 posts
  • Last active: Jul 17 2007 11:51 AM
  • Joined: 12 Nov 2005
@Anatoly Larkin, it's useless and a waste of resources to make it run that many times...
I simplified it, and I got this:
waiting:
    loop, 10  ; 100ms each run, so loop, 10 takes 1 sec to look.
    {
        sleep 100
        StatusBarGetText, statusBarText, , ahk_id %WinID%
        if statusBarText not contains Done,ConcluĂ­do  ;To add suport for extra languages, just add the translated-"Done" here.
            goto waiting
    }
    return


BoBo
  • Guests
  • Last active:
  • Joined: --
@oliverkahn

Does anyone have an idea how i could do that?

jenslehmann ?

adamisageek
  • Guests
  • Last active:
  • Joined: --
@NiJo, good job slimming that down :D , but it looks like your code may not work correctly for all, Ill explain:

If you are using the code by itself, then you might not have a problem, but if you use the code as a sub routine that will be called within another routine or hotkey (as I did), you may have issues with the return statement not working correctly. I think a better scripted snippet of your code would be:
waiting:
    varAttempts = 0
    loop
    {
         varAttempts += 1
         StatusBarGetText, statusBarText, , ahk_id %WinID%
         if statusBarText contains Done
         {
             ErrorLevel = 0
             break
         }
         if (varAttempts = 10)
         {
             ErrorLevel = 1
             break
         }
         Sleep 100
    }
    return
Basically the main thing I changed here was I inserted a break call instead of recalling the subroutine. By doing this, you avoid unecessary and error-inviting recursion. The loop simply continues to loop until the web page loads, then exits the loop at which point the subroutine's return call is triggered and you are returned safely to where it was called from. If the page fails to load after 10 tries, the subroutine leaves ErrorLevel with a value of 1 which can then be checked in the calling routine. :D

Now the code can be safely used to wait until a site is done loading, here's an example:
^!c::
    Run, http://www.autohotkey.com
    Gosub, waiting
    if ErrorLevel
    {
        MsgBox AutoHotKey web site not loaded!
    }
    else
    {
        MsgBox AutoHotKey web site loaded!
    }
return

Might be a bit long for some, but the better the code, the more secure its going to be. You can show Pi in a single line of code, but you can display it forever with a paragraph. :wink: Hope this helped!
[email protected]

Jaytech
  • Members
  • 243 posts
  • Last active: Dec 24 2008 09:22 PM
  • Joined: 11 Dec 2006
Here's the code I use (as a subroutine):
StatusWaitDone:
	
	SLEEP 100
	AHKID := WinExist("A") 
	Ctr=0 
	Loop
		{ 
		Sleep, 10
		Ctr+=1 
		ControlGet, Progress, Visible,, msctls_progress321, ahk_id %AHKID% 
		If (Progress=1)
			{ 
			Ctr=0 
			Continue 
			} 
		If (Progress=0 AND Ctr>10) 
		Break 
		} 
		RETURN

P.S.- This topic has been brought up about 5 different time that I can remember.

  • Guests
  • Last active:
  • Joined: --
@jaytech Great code! Good idea to check it's visibility...
But tell me, It only works with internet explorer, right? Nothing near that to mozilla? And about the mouse position: This method do work if mouse is over a Link :D

Here is a good example of why Posts can be revived :p

adamisageek
  • Members
  • 20 posts
  • Last active: May 07 2008 07:59 PM
  • Joined: 05 Mar 2007
First off, Ill tell you, I mostly use AutoHotKey for work; in short, I have Excel sheets containing many various part numbers that (usually once a month) need to all be checked for new SKUs on the site. Luckily for me, each item we sell has its own URL and the SKU is located in it. I use a script to throw the part number at the search box on our site, then, after the page loads, copy and paste the URL and pick out the SKU, then AHK pastes the SKU into my Excel sheet next to the part number. I was using a subroutine that checked when the page was done loading, but instead (and to make the script a lot faster) I decided to check when the URL was loaded so the whole page wouldn't need to load. Here's my subroutine, this is entered directly after the search for the part number is started:
		Send ^l{DELETE}
		attempts = 0
		loop
		{
			clipboard =
			Send ^l^c
			if %clipboard%
			{
				Send {ESC}
				break
			}
			attempts += 1
			if %attempts% = 70
				break
			sleep 100
		}
So basically it clears anything in the URL, then it keeps copying the URL which stays empty until the site starts to load, at which point the escape key is sent to stop the page so it doesn't load the whole page. After this, I run my other subroutine to grab the URL and dissect it for my SKU.

*NOTE* - This works in Firefox because Ctrl+L = select the address bar/URL and Escape stops loading the current page. I have not tested for IE but it could easily be modified by changing those commands to reflect IE's shortcut keys (if there are any, Im a die hard Firefox user :wink: ).

Yay, first registered post!

IKShadow
  • Members
  • 28 posts
  • Last active: Mar 17 2011 06:42 PM
  • Joined: 19 Dec 2005

Here's the code I use (as a subroutine):

StatusWaitDone:
	
	SLEEP 100
	AHKID := WinExist("A") 
	Ctr=0 
	Loop
		{ 
		Sleep, 10
		Ctr+=1 
		ControlGet, Progress, Visible,, msctls_progress321, ahk_id %AHKID% 
		If (Progress=1)
			{ 
			Ctr=0 
			Continue 
			} 
		If (Progress=0 AND Ctr>10) 
		Break 
		} 
		RETURN

P.S.- This topic has been brought up about 5 different time that I can remember.


Great solution, any idea how to make it work with IE 7 and Firefox ?

Helpy
  • Guests
  • Last active:
  • Joined: --

Great solution, any idea how to make it work with IE 7 and Firefox ?

No hope for FF, as it has non-standard status bar. ImageSearch and PixelGetColor might be used, though.
Is IE7 using a non-standard status bar as well?