locked
Vb script to detect a web-page is loaded or not RRS feed

  • Question

  • I have a Vb Script code that waits till the web-page is completely loaded. The problem I face is that at times the VB Script continues looping in the background even though the page has loaded completely. Even the script does not timeout. Any quick resolution to this problem is welcome as it's very important for me.

    The code is:

    WScript.Timeout = 60

    Set IE = CreateObject("InternetExplorer.Application")

    IE.Navigate mURL

    WaitForLoad(IE)

    Sub WaitFor(Load)

    Do While IE.Busy : Loop

    Do while IE.readyState <> 4 : Loop

    End Sub

    Friday, April 19, 2013 6:04 PM

Answers

  • Try it the easy way:

    Set ie = CreateObject("InternetExplorer.Application")
    ie.Navigate mURL
    Do While ie.Busy
       WScript.Sleep 200
    Loop
    


    ¯\_(ツ)_/¯

    • Marked as answer by Richa Iyer Monday, April 22, 2013 2:39 PM
    Friday, April 19, 2013 6:40 PM
  • Yep...doesn't work so any way to kill Wscript.exe running in the task manager as if it gets stuck, it uses maximum of the resources.

    You have a bad system or you are not use WScript.Sleep 200 to break the loop.

    The following will force the script to terminate after 60 seconds

    cscript myscript.vbs //T:60


    ¯\_(ツ)_/¯

    • Marked as answer by Richa Iyer Friday, April 19, 2013 10:41 PM
    Friday, April 19, 2013 8:18 PM

All replies

  • Try it the easy way:

    Set ie = CreateObject("InternetExplorer.Application")
    ie.Navigate mURL
    Do While ie.Busy
       WScript.Sleep 200
    Loop
    


    ¯\_(ツ)_/¯

    • Marked as answer by Richa Iyer Monday, April 22, 2013 2:39 PM
    Friday, April 19, 2013 6:40 PM
  • Here is a food-for-thought script.

    sPage="http://social.technet.microsoft.com/Forums/en-US/ITCG/threads"
    if GetPage( sPage ,ie) Then
        ie.Visible=True
        MsgBox ie.Document.Body.innerHTML
    Else
        MsgBox "Page failed"
    End If
    
    
    Function GetPage(sPageUrl, oIE )
    
         Set oIE = CreateObject("InternetExplorer.Application")
    
         ie.Navigate sPageUrl
         Do While ie.Busy
              WScript.Sleep 200
         Loop
    
         GetPage = True
    
    End Function


    ¯\_(ツ)_/¯

    Friday, April 19, 2013 7:02 PM
  • Does simply looping cause any issues?

    Is it necessary to cause the 500 msec delay? Just out of curiosity.

    Friday, April 19, 2013 7:26 PM
  • Yes and Yes.

    You cannot wait in a loop on the readystate.  It will not be available if the page has an issue.  Busy is the only thing to wait on.

    You cannot effectively get page state using Internet Explorer from script.  You will need to use the HTTP Client.


    ¯\_(ツ)_/¯

    Friday, April 19, 2013 7:28 PM
  • The Web Application I am working on is a bit weird. I click on a submit button and the next thing I see is a Loading watermark kinda thing.

    So, I need to wait till the loading watermark disappears. And earlier I was using the code you mentioned but it failed.

    But let me try the second code you mentioned.

    Thanks for all the help.

    Friday, April 19, 2013 7:33 PM
  • Most well designed web sites cannot be scripted.

    There is no way to detect when the server is busy.  The page will be loaded but other activity will cause it to change.  You cannot detect this.  In most cases this is by design to prevent exactly what you are trying to do.


    ¯\_(ツ)_/¯

    Friday, April 19, 2013 7:39 PM
  • Gotcha. And you have said it very perfectly.

    But, is there any way I can kill my script after say 60 seconds.

    Friday, April 19, 2013 7:53 PM
  • Set a counter on the loop. - It still won't likely work.

    ¯\_(ツ)_/¯

    Friday, April 19, 2013 8:01 PM
  • Yep...doesn't work so any way to kill Wscript.exe running in the task manager as if it gets stuck, it uses maximum of the resources.
    Friday, April 19, 2013 8:05 PM
  • Yep...doesn't work so any way to kill Wscript.exe running in the task manager as if it gets stuck, it uses maximum of the resources.

    You have a bad system or you are not use WScript.Sleep 200 to break the loop.

    The following will force the script to terminate after 60 seconds

    cscript myscript.vbs //T:60


    ¯\_(ツ)_/¯

    • Marked as answer by Richa Iyer Friday, April 19, 2013 10:41 PM
    Friday, April 19, 2013 8:18 PM