COMs Wait For Page to load in Internet Explorer Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Vaklev
Posts: 47
Joined: 04 Mar 2019, 13:58
Contact:

COMs Wait For Page to load in Internet Explorer

02 May 2019, 09:09

Hey guys, I am a complete newbie when it comes to COMS, don't know the first thing about using it to manipulate IE but I heard its possible.
Right now I am using sleep commands and image search to find pixels on the screen for the next page in internet explorer when I navigate, but can someone help me link my hotkey code to IE and initiate a wait for page to load command before initiating the rest of the code?

I am basically using IE to fill in data and sometimes the page takes longer to load than my initial sleep command but I don't want to make it sleep to long I want my auto fill as fast as possible so its best to have AHK detect once the page has fully loaded and start using the rest of the code to fill in the data asap.

Thanks in advance!
MannyKSoSo
Posts: 440
Joined: 28 Apr 2018, 21:59

Re: COMs Wait For Page to load in Internet Explorer

02 May 2019, 09:25

Code: Select all

wb := ComObjCreate("InternetExplorer.Application")
wb.Navigate("https://www.autohotkey.com/boards/viewtopic.php?f=76&t=64206") ;This topic
wb.Visible := false
while wb.readyState!=4 || wb.document.readyState != "complete" || wb.busy
    sleep 10
MsgBox % "Loading is done!"
Exitapp
An example of loading a webpage and the script will give a message box that it is done
Vaklev
Posts: 47
Joined: 04 Mar 2019, 13:58
Contact:

Re: COMs Wait For Page to load in Internet Explorer

02 May 2019, 09:30

Thank you for the prompt reply, can you please explain what each line means, first we create the variable wb to create the com object, then we tell it to navigate to this page, what happens if visible was true? Also is ready key state 4 meaning it has loaded?
Les medecins choisissent viagrasansordonnancefr.com sans danger pour la sante
User avatar
tank
Posts: 3122
Joined: 28 Sep 2013, 22:15
Location: CarrolltonTX
Contact:

Re: COMs Wait For Page to load in Internet Explorer

02 May 2019, 09:32

I prefer this approach

Code: Select all

wb := ComObjCreate("InternetExplorer.Application")
wb.Navigate("https://www.autohotkey.com/boards/viewtopic.php?f=76&t=64206") ;This topic
wb.Visible := false
while try !(pdoc := wb.document)
    sleep 10
; based on an element you want to ensure has loaded
while try !(ele:=pdoc.querySelector("#message-box > textarea"))
    Sleep, 10
msgbox some code here
We are troubled on every side‚ yet not distressed; we are perplexed‚
but not in despair; Persecuted‚ but not forsaken; cast down‚ but not destroyed;
Telegram is the best way to reach me
https://t.me/ttnnkkrr
If you have forum suggestions please submit a
Check Out WebWriter
Vaklev
Posts: 47
Joined: 04 Mar 2019, 13:58
Contact:

Re: COMs Wait For Page to load in Internet Explorer

02 May 2019, 09:39

Thank you for the quick reply guys, just a few questions as I am trying to learn and understand, what is the purpose of defining wb.Visible := false, what happens if its true? also what kind of elements are my options for while try !(pdoc := wb.document), also @tank I wanted to reach you earlier as this is related to an RDA project im working on in the financial institution I work at and wanted to join the RPA forums!
Les medecins choisissent viagrasansordonnancefr.com sans danger pour la sante
Vaklev
Posts: 47
Joined: 04 Mar 2019, 13:58
Contact:

Re: COMs Wait For Page to load in Internet Explorer

02 May 2019, 10:49

Question guys, I am always inputting new data into the field, so when it comes to navigating to the page, how can I make the web link include the variable? The website im using is http://www.swiftcodesinfo.com/sort-code/ and I am always inputting a new sort code for it to load, so the web link would change
Les medecins choisissent viagrasansordonnancefr.com sans danger pour la sante
User avatar
tank
Posts: 3122
Joined: 28 Sep 2013, 22:15
Location: CarrolltonTX
Contact:

Re: COMs Wait For Page to load in Internet Explorer

02 May 2019, 12:01

http://lmgtfy.com/?q=autohotkey+string+concatenation
I think maybe you have a lot to learn before entering the discussion about RPA
We are troubled on every side‚ yet not distressed; we are perplexed‚
but not in despair; Persecuted‚ but not forsaken; cast down‚ but not destroyed;
Telegram is the best way to reach me
https://t.me/ttnnkkrr
If you have forum suggestions please submit a
Check Out WebWriter
Vaklev
Posts: 47
Joined: 04 Mar 2019, 13:58
Contact:

Re: COMs Wait For Page to load in Internet Explorer

02 May 2019, 12:08

tank wrote:
02 May 2019, 12:01
http lmgtfy.com /?q=autohotkey+string+concatenation Broken Link for safety
I think maybe you have a lot to learn before entering the discussion about RPA
Yes 100% agreed, I only started reading about expressions and variables commands and functions today and it is a lot to take in, but I feel I have mastered the image search command which is amazing for my code, it automatically detects the information on the screen and navigates for me based on a match against the huge data base of images I have created, I am kind of struggling to understand how to use expressions and functions on my own but I am not giving up! I am reading the string concatenation you just linked me as we speak thank you Tank, and feel free to hit me up with more info that will help me enhance my knowledge further I really appreciate it!
Les medecins choisissent viagrasansordonnancefr.com sans danger pour la sante
Vaklev
Posts: 47
Joined: 04 Mar 2019, 13:58
Contact:

Re: COMs Wait For Page to load in Internet Explorer

03 May 2019, 08:59

Hey guys I need some help, I am getting an error when I inserted the code into my script, after reading about string concatenation I understand now what := and . operators are used for, but what is the pdoc := wb.document? pdoc is the variable, but what does the parameter "document" do? Also what are some examples of elements I can assign for "querySelector", I basically click search and there is a loading time so im not sure what element I can use for that. I am getting an error "0x80004005 - Unspecified error Specifically: document" for that line, keep in mind im here to learn so please don't bite too hard :P

Here is my code, I don't need to have coms navigate to the page, I just need it to wait for the page to load once I have my script click the "search" button on the website im using. CODE:

Code: Select all

+q:: ; GOOGLE SEARCH UK SORT CODE
MouseMove 744,474 ; COUNTRY DROPDOWN
MouseClick left
Send uuuu ; UK
Send {Enter}
sleep 1000
MouseMove 559,234 ; TAGS
MouseClick left
ImageSearch, FoundX,FoundY, 822,229, 928,429, scroll.png
if ErrorLevel = 1
    Loop
    {
        MouseClick left
        sleep 500
        ImageSearch, FoundX,FoundY, 822,229, 928,429, scroll.png
        if ErrorLevel = 0
        break
    }
MouseMove 885,313 ; TAGS SCROLL
Send {lbutton down}
MouseMove 0,400,0,r ; Scrolls down
Send {lbutton up}
MouseMove 883,266
MouseClick left
MouseClick left
sleep 300
MouseMove 594,275 ; SORT CODE in tags
MouseClick left
MouseClick left
Send {LControl Down}c{Lcontrol Up}

WinActivate ahk_exe iexplore.exe Sort Code
wb := ComObjCreate("InternetExplorer.Application")
wb.Visible := false
while try !(pdoc := wb.document)
    sleep 10
; based on an element you want to ensure has loaded
while try !(ele:=pdoc.querySelector("#message-box > textarea"))
    Sleep, 10

MouseMove 205,267 ; sort code field on website
MouseClick left
MouseClick left
MouseClick left
Send {LControl Down}v{Lcontrol Up}
Send {enter}
Sleep 1000
MouseMove 33,412
MouseClick left
MouseClick left
MouseClick left
Send {LControl Down}c{Lcontrol Up}
WinActivate CanadaFINTRAC Page -- Webpage Dialog
MouseMove 222,372
MouseClick left
MouseClick left
MouseClick left
Send {LControl Down}v{Lcontrol Up}
return

Les medecins choisissent viagrasansordonnancefr.com sans danger pour la sante
User avatar
tank
Posts: 3122
Joined: 28 Sep 2013, 22:15
Location: CarrolltonTX
Contact:

Re: COMs Wait For Page to load in Internet Explorer

03 May 2019, 11:41

you took out the navigation so there is no document.
OK looking at AHK documentation :headwall: I have constructed the following code. Note that waitForIE()may be called with or without a specified element to wait for in case of dynamic content

Code: Select all

#IfWinExist, ahk_class IEFrame
f1::waitForIE()
#IfWinExist
waitForIE(querySelector := false){
    pwb:=pwb()
    while pwb.busy
        sleep,10

    if querySelector
        ; based on an element you want to ensure has loaded
        while try !(ele:=pwb.document.querySelector(querySelector))
            Sleep, 10
    msgbox
    return true
}

pwb(){
    ; Query for the WebBrowserApp service. In this particular case,
    ; the SID and IID are the same, but it isn't always this way.
    static IID_IWebBrowserApp := "{0002DF05-0000-0000-C000-000000000046}"
    static SID_SWebBrowserApp := IID_IWebBrowserApp
    
    return, ComObj(9,ComObjQuery(pDoc(), SID_SWebBrowserApp, IID_IWebBrowserApp),1)
}

pDoc(){
    static msg := DllCall("RegisterWindowMessage", "Str", "WM_HTML_GETOBJECT")
    SendMessage msg, 0, 0, Internet Explorer_Server1, ahk_class IEFrame
    if ErrorLevel = FAIL
        return  ; IE not found.

    lResult := ErrorLevel
    VarSetCapacity(GUID, 16, 0)
    DllCall("oleacc\ObjectFromLresult", "Ptr", lResult
        , "Ptr", (DllCall("ole32\CLSIDFromString", "WStr", "{332C4425-26CB-11D0-B483-00C04FD90119}", "Ptr", &GUID) >= 0 ? &GUID : "")
        , "Ptr", 0, "Ptr*", IWebDOCUMENT)

    if !IWebDOCUMENT
        return, pDoc()

    return, IWebDOCUMENT
}

We are troubled on every side‚ yet not distressed; we are perplexed‚
but not in despair; Persecuted‚ but not forsaken; cast down‚ but not destroyed;
Telegram is the best way to reach me
https://t.me/ttnnkkrr
If you have forum suggestions please submit a
Check Out WebWriter
Vaklev
Posts: 47
Joined: 04 Mar 2019, 13:58
Contact:

Re: COMs Wait For Page to load in Internet Explorer

03 May 2019, 12:04

My goodness the previous code was overwhelming to understand as it is now you just went next level on me @tank. Unfortunately, I am having a super hard time trying to understand what each function and variable does/means, is there no simpler solution than this? I'm not just looking for a solution, I am trying to learn how to fish rather than be given a fish if you know what I mean. I might just ditch the whole COM concept for now and stick to my image search method where I loop the image search until it finds the image present on the next page before continuing...
Les medecins choisissent viagrasansordonnancefr.com sans danger pour la sante
User avatar
tank
Posts: 3122
Joined: 28 Sep 2013, 22:15
Location: CarrolltonTX
Contact:

Re: COMs Wait For Page to load in Internet Explorer

03 May 2019, 12:20

variables like IID_IWebBrowserApp can be googled as they are taken directly from Microsoft documentation
RegisterWindowMessage can similarly be googled to Microsoft documentation But none of this is important to understand. you can drive a car without understanding how a transmission works. all you need to know is how to call a function and include these functions in your code. I dont expect obvious noobies to understand every line of code otherwise i would have been obstinate and made you figure it out with cryptically helpful hints.
We are troubled on every side‚ yet not distressed; we are perplexed‚
but not in despair; Persecuted‚ but not forsaken; cast down‚ but not destroyed;
Telegram is the best way to reach me
https://t.me/ttnnkkrr
If you have forum suggestions please submit a
Check Out WebWriter
Vaklev
Posts: 47
Joined: 04 Mar 2019, 13:58
Contact:

Re: COMs Wait For Page to load in Internet Explorer

03 May 2019, 12:27

tank wrote:
03 May 2019, 12:20
variables like IID_IWebBrowserApp can be googled as they are taken directly from Microsoft documentation
RegisterWindowMessage can similarly be googled to Microsoft documentation But none of this is important to understand. you can drive a car without understanding how a transmission works. all you need to know is how to call a function and include these functions in your code. I dont expect obvious noobies to understand every line of code otherwise i would have been obstinate and made you figure it out with cryptically helpful hints.
I guess my learning style is different, I don't feel comfortable coding unless I understand the purpose of calling in the functions and in order to understand the purpose of the functions you need to understand what the variables do and how they are linked, are there tutorials out there you can recommend for me to do? I feel extremely comfortable with commands, but I am struggling a lot with variables and expressions. I read all the documentation but I need a tutorial to give me examples of how I can apply the concepts that I read. I do appreciate all your efforts Tank don't get me wrong, but this is a bit too advanced for a newbie with my type of learning style.
Les medecins choisissent viagrasansordonnancefr.com sans danger pour la sante
User avatar
tank
Posts: 3122
Joined: 28 Sep 2013, 22:15
Location: CarrolltonTX
Contact:

Re: COMs Wait For Page to load in Internet Explorer

04 May 2019, 10:22

obviously the point of the function is to halt script execution till iE has fully loaded the page. unfortunately much of the documentation around these variables will require a bit of programming foreknowledge that it doesn't appear you have. As an example it probably does no good to tell you that IID_IWebBrowserApp is the CLSID for the iWebBrowser2 interface. Even if i told you that that means with it you can use the properties, methods and objects exposed in the Trident rendering engine. Unfortunately, there is lot more you need to learn before any of these explanations will mean much.
What you really need to know is that calling waitForIE() function ensures the page has loaded completely before the next line of code executes.
We are troubled on every side‚ yet not distressed; we are perplexed‚
but not in despair; Persecuted‚ but not forsaken; cast down‚ but not destroyed;
Telegram is the best way to reach me
https://t.me/ttnnkkrr
If you have forum suggestions please submit a
Check Out WebWriter
A_AhkUser
Posts: 1147
Joined: 06 Mar 2017, 16:18
Location: France
Contact:

Re: COMs Wait For Page to load in Internet Explorer

05 May 2019, 01:04

Vaklev wrote:
02 May 2019, 09:39
[...] just a few questions as I am trying to learn and understand, what is the purpose of defining wb.Visible := false, what happens if its true? [...]
Here's a reference guide for a quick overview before using the WebBrowser control: WebBrowser control - Reference for Visual Basic Developers.
see also: Basic Webpage Controls with JavaScript / COM - Tutorial.
my scripts
Vaklev
Posts: 47
Joined: 04 Mar 2019, 13:58
Contact:

Re: COMs Wait For Page to load in Internet Explorer

06 May 2019, 12:16

tank wrote:
04 May 2019, 10:22
IID_IWebBrowserApp is the CLSID for the iWebBrowser2 interface. Even if i told you that that means with it you can use the properties, methods and objects exposed in the Trident rendering engine
That is exactly the type of answer I was looking for, and also with the library that A_AhkUser linked me (many thanks!) I can now do some digging into seeing the different types of functions I can manipulate to see whats possible! Thank you guys! Also I just want to ask, should I include the entire code you created outside of my hotkey and only use the waitForIE() function whenever I need to wait for the page to load?

Example below:

Code: Select all


#IfWinExist, ahk_class IEFrame
waitForIE()
#IfWinExist
waitForIE(querySelector := false){
    pwb:=pwb()
    while pwb.busy
        sleep,10

    if querySelector
        ; based on an element you want to ensure has loaded
        while try !(ele:=pwb.document.querySelector(querySelector))
            Sleep, 10
    msgbox
    return true
}

pwb(){
    ; Query for the WebBrowserApp service. In this particular case,
    ; the SID and IID are the same, but it isn't always this way.
    static IID_IWebBrowserApp := "{0002DF05-0000-0000-C000-000000000046}"
    static SID_SWebBrowserApp := IID_IWebBrowserApp
    
    return, ComObj(9,ComObjQuery(pDoc(), SID_SWebBrowserApp, IID_IWebBrowserApp),1)
}

pDoc(){
    static msg := DllCall("RegisterWindowMessage", "Str", "WM_HTML_GETOBJECT")
    SendMessage msg, 0, 0, Internet Explorer_Server1, ahk_class IEFrame
    if ErrorLevel = FAIL
        return  ; IE not found.

    lResult := ErrorLevel
    VarSetCapacity(GUID, 16, 0)
    DllCall("oleacc\ObjectFromLresult", "Ptr", lResult
        , "Ptr", (DllCall("ole32\CLSIDFromString", "WStr", "{332C4425-26CB-11D0-B483-00C04FD90119}", "Ptr", &GUID) >= 0 ? &GUID : "")
        , "Ptr", 0, "Ptr*", IWebDOCUMENT)

    if !IWebDOCUMENT
        return, pDoc()

    return, IWebDOCUMENT
}

+q:: ; GOOGLE SEARCH UK SORT CODE
MouseMove 744,474 ; COUNTRY DROPDOWN
MouseClick left
Send uuuu ; UK
Send {Enter}
sleep 1000
MouseMove 559,234 ; TAGS
MouseClick left
ImageSearch, FoundX,FoundY, 822,229, 928,429, scroll.png
if ErrorLevel = 1
    Loop
    {
        MouseClick left
        sleep 500
        ImageSearch, FoundX,FoundY, 822,229, 928,429, scroll.png
        if ErrorLevel = 0
        break
    }
MouseMove 885,313 ; TAGS SCROLL
Send {lbutton down}
MouseMove 0,400,0,r ; Scrolls down
Send {lbutton up}
MouseMove 883,266
MouseClick left
MouseClick left
sleep 300
MouseMove 594,275 ; SORT CODE in tags
MouseClick left
MouseClick left
Send {LControl Down}c{Lcontrol Up}

MouseMove 205,267 ; sort code field on website
MouseClick left
MouseClick left
MouseClick left
Send {LControl Down}v{Lcontrol Up}
Send {enter} ; page begins to load

waitForIE()

MouseMove 33,412
MouseClick left
MouseClick left
MouseClick left
Send {LControl Down}c{Lcontrol Up}
WinActivate CanadaFINTRAC Page -- Webpage Dialog
MouseMove 222,372
MouseClick left
MouseClick left
MouseClick left
Send {LControl Down}v{Lcontrol Up}
return
EDIT: I TRIED THE CODE AND IT WORKS! 1 Thing I need to eliminate, it pops up a msg box asking me to click ok before it continues to the next page, and I tried looking for the piece of code that launches that msgbox, when I took out SendMessage msg, 0, 0, Internet Explorer_Server1, ahk_class IEFrame my program crashed, can you please help me find out which piece of code launches the msg box?
Les medecins choisissent viagrasansordonnancefr.com sans danger pour la sante
User avatar
tank
Posts: 3122
Joined: 28 Sep 2013, 22:15
Location: CarrolltonTX
Contact:

Re: COMs Wait For Page to load in Internet Explorer  Topic is solved

06 May 2019, 14:59

look again and you will see the msgbox in the code
We are troubled on every side‚ yet not distressed; we are perplexed‚
but not in despair; Persecuted‚ but not forsaken; cast down‚ but not destroyed;
Telegram is the best way to reach me
https://t.me/ttnnkkrr
If you have forum suggestions please submit a
Check Out WebWriter

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], onurcoban and 379 guests