Issue with page load

Get help with using AutoHotkey and its commands and hotkeys
sqlcode
Posts: 27
Joined: 02 Nov 2017, 08:55

Issue with page load

06 Mar 2018, 17:22

I have a strange issue with page load and I have tried a lot of options from forum but it's not helping. My code looks like below snippet. I am trying to make sure, page load is complete before I take further action. Page load confirms an entry in the file which I am using in the macro later (code not shown below).

However, script doesn't wait till page load and as a result I am getting btn.click() executed twice and script behaves strangely. I have also tried below snippet for page load wait but same result.

Could someone please help? I have tried to increase sleep to 1000 as well but no change.

Code: Select all

while pwb.busy or pwb.ReadyState != 4 ;Wait for page to load
	Sleep, 100

Code: Select all

... ;some lines not shown 
pwb.document.all.btn.click()	                                                       ;Click  
while (pwb.busy or pwb.readystate <> 4)                                              	; waits for the page to be loaded
         sleep, 200

replace :=   {sqlcode:"sqlcode100"}

For k, v in replace                                                                
   StringReplace, String, String, %k%, %v%, All                      

pwb.document.all.TextBar.Value :=String
pwb.document.all.btn.click()	                                                       ;Click 

;************Pointer to Open IE Window******************
WBGet(WinTitle="ahk_class IEFrame", Svr#=1) {               						
   static msg := DllCall("RegisterWindowMessage", "str", "WM_HTML_GETOBJECT")
        , IID := "{0002DF05-0000-0000-C000-000000000046}"   						
   SendMessage msg, 0, 0, Internet Explorer_Server%Svr#%, %WinTitle%

   if (ErrorLevel != "FAIL") {
      lResult:=ErrorLevel, VarSetCapacity(GUID,16,0)
      if DllCall("ole32\CLSIDFromString", "wstr","{332C4425-26CB-11D0-B483-00C04FD90119}", "ptr",&GUID) >= 0 {
         DllCall("oleacc\ObjectFromLresult", "ptr",lResult, "ptr",&GUID, "ptr",0, "ptr*",pdoc)
         return ComObj(9,ComObjQuery(pdoc,IID,IID),1), ObjRelease(pdoc)
      }
   }
}


Thanks in advance.
sqlcode
Posts: 27
Joined: 02 Nov 2017, 08:55

Re: Issue with page load

07 Mar 2018, 11:33

Hello,
could someone please help on this? Do I have a typo or is this not how the logic is supposed to work?
gregster
Posts: 3914
Joined: 30 Sep 2013, 06:48

Re: Issue with page load

07 Mar 2018, 11:40

Hard to say, like this... there seems nothing obviously wrong in your code snippet, at least to me.

Can you provide some source code (extract) that allows to reproduce the problem? Otherwise, it might be a guessing game at best - or not, depending on which experts might come around. But more info - and something to test - is surely better than less and will increase the likelihood to find a solution to the problem, or possibly a workaround.
sqlcode
Posts: 27
Joined: 02 Nov 2017, 08:55

Re: Issue with page load

07 Mar 2018, 16:41

Thank you for prompt response. So I tried this for testing and I am not even sure if this is a valid test but it failed. I got Msgbox popped up before even page load was complete.

Code: Select all

pwb := WBGet()

pwb.Navigate("www.msn.com") 											;Navigate to URL
while pwb.busy or pwb.ReadyState != 4 									;Wait for page to load
	Sleep, 1000

MsgBox Test

;************Pointer to Open IE Window******************
WBGet(WinTitle="ahk_class IEFrame", Svr#=1) {               ;// based on ComObjQuery docs
   static msg := DllCall("RegisterWindowMessage", "str", "WM_HTML_GETOBJECT")
        , IID := "{0002DF05-0000-0000-C000-000000000046}"   ;// IID_IWebBrowserApp
   SendMessage msg, 0, 0, Internet Explorer_Server%Svr#%, %WinTitle%

   if (ErrorLevel != "FAIL") {
      lResult:=ErrorLevel, VarSetCapacity(GUID,16,0)
      if DllCall("ole32\CLSIDFromString", "wstr","{332C4425-26CB-11D0-B483-00C04FD90119}", "ptr",&GUID) >= 0 {
         DllCall("oleacc\ObjectFromLresult", "ptr",lResult, "ptr",&GUID, "ptr",0, "ptr*",pdoc)
         return ComObj(9,ComObjQuery(pdoc,IID,IID),1), ObjRelease(pdoc)
      }
   }
}
Thanks,
gregster
Posts: 3914
Joined: 30 Sep 2013, 06:48

Re: Issue with page load

07 Mar 2018, 19:28

This page works for me like that - some external ads might not have loaded completely, but the msn stuff seems to be ready after the while-loop. At least, this works for me if I allow popups for this page - then, it will open the search results for "Hello world" in a new tab:

Code: Select all

pwb.Navigate("http://www.msn.com/") 
while pwb.busy or pwb.ReadyState != 4 									;Wait for page to load
	Sleep, 1000

pwb.document.GetElementbyId("q").value := "Hello world"
sleep, 1000
pwb.document.GetElementsbyName("bingSearch")[0].Click()
So far, it didn't fail in about 15 attempts...

But perhaps a second, additional while-loop, looking for the existence of the needed page element could make it even more reliable:

Code: Select all

pwb.Navigate("http://www.msn.com/") 
while pwb.busy or pwb.ReadyState != 4 									;Wait for page to load
	Sleep, 1000
while !(pwb.document.GetElementbyId("q"))								; "while not element q"   ! = not
	sleep, 100

pwb.document.GetElementbyId("q").value := "Hello world"
sleep, 1000
pwb.document.GetElementsbyName("bingSearch")[0].Click()

If there is no element with the ID "q", it shouldn't even leave the second waiting loop... (in practice, you would probably want some timeout exit/error handling)
I guess, this should work on the webpage in question, too. (Don't forget to adjust the element IDs, names etc. for your use case)

Return to “Ask For Help”

Who is online

Users browsing this forum: anautohotkeydummy, AriesKnight, Bing [Bot] and 346 guests