Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Determine if a WebPage is completely loaded in IE


  • Please log in to reply
75 replies to this topic
Sean
  • Members
  • 2462 posts
  • Last active: Feb 07 2012 04:00 AM
  • Joined: 12 Feb 2007
It'll navigate and wait until a webpage is completely loaded in an Internet Explorer.

NEED the latest COM Standard Library.

#Persistent
sUrl :=	"http://www.autohotkey.com/forum/"

COM_Init()
pweb :=	COM_CreateObject("InternetExplorer.Application")
sink :=	COM_ConnectObject(pweb, "IE_")
COM_Invoke(pweb, "Visible", True)

bComplete := False
COM_Invoke(pweb, "Navigate2", sUrl)
While !bComplete
      Sleep, 500

COM_DisconnectObject(sink)
COM_Release(pweb)
COM_Term()
Return

OnComplete:
bComplete := True
Return

IE_DocumentComplete(prms, sink)
{
	If	NumGet(NumGet(prms+0)+24) = NumGet(sink+12)
		SetTimer, OnComplete, -10
/* more rigorous way
	COM_Release(punk1:=COM_QueryInterface(NumGet(NumGet(prms+0)+24),0))
	COM_Release(punk2:=COM_QueryInterface(NumGet(sink+12),0))
	If   (punk1 = punk2)
		SetTimer, OnComplete, -10
*/
}

IEReady(hIESvr = 0)
{
	If Not	hIESvr
	{
		Loop,	50
		{
			ControlGet, hIESvr, hWnd, , Internet Explorer_Server1, A ; ahk_class IEFrame
			If	hIESvr
				Break
			Else	Sleep 100
		}
		If Not	hIESvr
			Return	"""Internet Explorer_Server"" Not Found."
	}
	Else
	{
		WinGetClass, sClass, ahk_id %hIESvr%
		If Not	sClass == "Internet Explorer_Server"
			Return	"The specified control is not ""Internet Explorer_Server""."
	}

	COM_Init()
	If	DllCall("SendMessageTimeout", "Uint", hIESvr, "Uint", DllCall("RegisterWindowMessage", "str", "WM_HTML_GETOBJECT"), "Uint", 0, "Uint", 0, "Uint", 2, "Uint", 1000, "UintP", lResult)
	&&	DllCall("oleacc\ObjectFromLresult", "Uint", lResult, "Uint", COM_GUID4String(IID_IHTMLDocument2,"{332C4425-26CB-11D0-B483-00C04FD90119}"), "int", 0, "UintP", pdoc)=0
	&&	pdoc && pweb:=COM_QueryService(pdoc,IID_IWebBrowserApp:="{0002DF05-0000-0000-C000-000000000046}")
	{
		While,	COM_Invoke(pweb, "ReadyState") <> 4
		Sleep,	500
		While,	COM_Invoke(pweb, "document.readyState") <> "complete"
		Sleep,	500
		COM_Release(pweb)
	}
	COM_Release(pdoc)
	COM_Term()
	Return	pweb ? "DONE!" : False
}


majkinetor
  • Moderators
  • 4512 posts
  • Last active: May 20 2019 07:41 AM
  • Joined: 24 May 2006
Many times asked so far. You made ppl happy now.
Posted Image

SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005
Thanks Sean. I need this for a couple of scripts.

:D

boge
  • Members
  • 11 posts
  • Last active: Jul 10 2007 09:32 AM
  • Joined: 16 May 2007
very nice sean...

btw, do you have something similar for firefox?
since I work most of the time with firefox...

Chris
  • Administrators
  • 10727 posts
  • Last active:
  • Joined: 02 Mar 2004
Great work. I've added it to the FAQ:

How to detect when a web page is finished loading?

With Internet Explorer, perhaps the most reliable method is to use DllCall and COM as demonstrated at <!-- m -->http://www.autohotke...c19255.html<!-- m -->.



Sean
  • Members
  • 2462 posts
  • Last active: Feb 07 2012 04:00 AM
  • Joined: 12 Feb 2007

Great work. I've added it to the FAQ

Thanks!

Sean
  • Members
  • 2462 posts
  • Last active: Feb 07 2012 04:00 AM
  • Joined: 12 Feb 2007

btw, do you have something similar for firefox?
since I work most of the time with firefox...

Unfortunately, no. I don't know of a way with firefox.

bennoman
  • Guests
  • Last active:
  • Joined: --
Im relativley new to ahk, so maybe im doing something wrong. I copied your code and created a new ahk file on my desktop. Then copied the CoHelper code and saved it as CoHelper.ahk also on my desktop. I then double click on the first file to run it and I get this error:
Error at line 64 in #include file "C:\Documents and Settings\bdmock\Desktop\CoHelper.ahk".
Line Text: :
Error: The leftmost character above is illegal in an expression.
The program will exit.

I have been charged with automating the test order process on one of my companies websites. I have started working on this already and found that the only way to have my script wait for the page to load is to use ImageSearch. This process works so far but this sounds like it will work more efficiently.
What am I doing wrong? How will this work within another script?

Thank you in advance.

n-l-i-d
  • Guests
  • Last active:
  • Joined: --
Just a guess: Wordwrap error while copying?

bennoman
  • Members
  • 11 posts
  • Last active: Jul 17 2007 02:06 PM
  • Joined: 14 Jun 2007
I recopied the code for both programs and still got the same error. I tried copying the code into notepad, textpad, and word all with the same result.

Am I using it properly? Do I need to assign a hotkey to it or call it from another script? It sounds like it is supposed to throw a MsgBox when the page under the mouse is finished loading, when I start the script my mouse is on the desktop. Could this be the problem? If so, how do I start the script while the page is loading?

bennoman
  • Members
  • 11 posts
  • Last active: Jul 17 2007 02:06 PM
  • Joined: 14 Jun 2007
by the way, line 64 is:

Unicode2Ansi(ByRef wString, ByRef sString, nLen = 0)
{
pString := wString + 0 > 65535 ? wString : &wString
If !nLen
nLen := DllCall("WideCharToMultiByte", "Uint", 0, "Uint", 0, "Uint", pString, "int", -1, "Uint", 0, "int", 0, "Uint", 0, "Uint", 0)
VarSetCapacity(sString, nLen)
DllCall("WideCharToMultiByte", "Uint", 0, "Uint", 0, "Uint", pString, "int", -1, "str", sString, "int", nLen, "Uint", 0, "Uint", 0)
}

n-l-i-d
  • Guests
  • Last active:
  • Joined: --
Next guess: Try updating your AutoHotkey to the latest version...

bennoman
  • Members
  • 11 posts
  • Last active: Jul 17 2007 02:06 PM
  • Joined: 14 Jun 2007
That worked! I was still using the version I downloaded last year around september because I do not have admin rights on my computer so I had to call a helpdesk tech over to install the newer version.
Thank you for your help. Next time I have problems like this I will make sure Im using the current version before wasting everyones time in the forum.
Thanks again.

n-l-i-d
  • Guests
  • Last active:
  • Joined: --
No prob. You can download the zipped version of AutoHotkey too, for your portable-AutoHotkey-pleasure :wink:

bennoman
  • Members
  • 11 posts
  • Last active: Jul 17 2007 02:06 PM
  • Joined: 14 Jun 2007
Thanks Sean, this works beautifully in my scripts.
Great Job!