internet explorer - Detect when a web page is loaded without using sleep -


i creating vb script on windows opens site in ie. want: detect when web page loaded , display message. achieved using sleep (wscript.sleep) approx. seconds when site gets loaded. however, site pops user name, password in midway. when user enter credentials, finishes loading page. don't want use "sleep" approx seconds, instead exact function or way detect page got loaded. checked on line , tried using do while loop, onload, onclick functions, nothing works. simplify, if write script open site yahoo , detect, display message "hi" when page loaded: doesn't work without using sleep (wscript.sleep).

try conventional method:

set objie = createobject("internetexplorer.application") objie.visible = true objie.navigate "https://www.yahoo.com/" while objie.readystate <> 4     wscript.sleep 10 loop ' code here ' ... 

upd: 1 should check errors:

set objie = createobject("internetexplorer.application") objie.visible = true objie.navigate "https://www.yahoo.com/" on error resume next      if objie.readystate = 4         if err = 0             exit         else             err.clear         end if     end if     wscript.sleep 10 loop on error goto 0 ' code here ' ... 

upd2: wrote ie gets disconnected login pop-up comes in, hypothetically there way catch disconnection, , ie instance again. note "abnormal programming" :) hope helps:

option explicit dim objie, strsignature, strinittype  set objie = createobject("internetexplorer.application") ' create ie instance objie.visible = true strsignature = left(createobject("scriptlet.typelib").guid, 38) ' generate uid objie.putproperty "marker", strsignature ' tokenize instance strinittype = typename(objie) ' typename objie.navigate "https://www.yahoo.com/" msgbox "initial type = " & typename(objie) ' visualisation  on error resume next while typename(objie) = strinittype ' wait until typename changes (actvex disconnection), may cause error 800a000e if not within oern     wscript.sleep 10 loop msgbox "changed type = " & typename(objie) ' visualisation  set objie = nothing ' excessive statement, clearance     each objie in createobject("shell.application").windows ' loop through explorer windows find tokenized instance         if objie.getproperty("marker") = strsignature ' our instance found             if typename(objie) = strinittype exit ' may excessive type check         end if     next     wscript.sleep 10 loop msgbox "found type = " & typename(objie) ' visualisation on error goto 0  while objie.readystate <> 4 ' conventional wait if instance not ready     wscript.sleep 10 loop  msgbox "title = " & objie.document.title ' visualisation 

you can text nodes, links etc. dom, follows:

option explicit dim objie, coltags, strresult, objtag, objchild, arrresult  set objie = createobject("internetexplorer.application") objie.visible = true objie.navigate "https://www.yahoo.com/"  while objie.readystate <> 4     wscript.sleep 10 loop  set coltags = objie.document.getelementsbytagname("a") strresult = "total " & coltags.length & " dom anchor nodes:" & vbcrlf each objtag in coltags     strresult = strresult & objtag.getattribute("href") & vbcrlf next showinnotepad strresult  set coltags = objie.document.getelementsbytagname("*") arrresult = array() each objtag in coltags     each objchild in objtag.childnodes         if objchild.nodetype = 3             redim preserve arrresult(ubound(arrresult) + 1)             arrresult(ubound(arrresult)) = objchild.nodevalue         end if     next next strresult = "total " & coltags.length & " dom object nodes + total " & ubound(arrresult) + 1 & " #text nodes:" & vbcrlf strresult = strresult & join(arrresult, vbcrlf) showinnotepad strresult  objie.quit  sub showinnotepad(strtofile)     dim strtemppath     createobject("scripting.filesystemobject")         strtemppath = createobject("wscript.shell").expandenvironmentstrings("%temp%") & "\" & .gettempname         .createtextfile(strtemppath, true, true)             .writeline (strtofile)             .close         end         createobject("wscript.shell").run "notepad.exe " & strtemppath, 1, true         .deletefile (strtemppath)     end end sub 

also get text data

upd3: want place here additional check if webpage loading , initialization completed:

' ... ' navigating url objie.navigate strurl ' wait ie ready while objie.readystate <> 4 or objie.busy     wscript.sleep 10 loop ' wait document complete while objie.document.readystate <> "complete"     wscript.sleep 10 loop ' processing loaded webpage code ' ... 

upd4: there cases when need track if target node have been created in document (usually it's necessary if object required error while attempting access node .getelementbyid, etc.):

if page uses ajax (loaded page source html doesn't contain target node, active content javascript creates dynamically), there example in below snippet of page, showing how like. text node 5.99 might created after page loaded, , other requests server data displayed have taken place:

... <td class="price-label">     <span id="priceblock" class="price-big color">         5.99     </span> </td> ... 

or if loading e. g. google search result page , waiting next button appeared (especially, if invoked .click method on previous page), or loading page login web form , waiting username input field <input name="userid" id="userid" type="text" maxlength="24" required="" placeholder="username" autofocus="">.

the below code allows make additional check if target node accessible:

with objie     ' navigating url     .navigate strurl     ' wait ie ready     while .readystate <> 4 or .busy         wscript.sleep 10     loop     ' wait document complete     while .document.readystate <> "complete"         wscript.sleep 10     loop     ' wait target node created     while typename(.document.getelementbyid("userid")) = "null"         wscript.sleep 10     loop     ' processing target node     .document.getelementbyid("userid").value = "myusername"     ' ...     ' end 

Comments

Popular posts from this blog

java - Intellij Synchronizing output directories .. -

git - Initial Commit: "fatal: could not create leading directories of ..." -