Results 1 to 12 of 12

Thread: Webbrowser Document Complete

  1. #1

    Thread Starter
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,828

    Resolved Webbrowser Document Complete

    I'm using a webbrowser control to pull some information from a website. It works fine.

    I load the web page when the user clicks a button and then I pull the information from the site when the page completely loads. I have this bit of code in the DocumentComplete event.

    I am doing this for two different websites. So, my code as it is, looks something likes this:
    vb.net Code:
    1. Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
    2.      WebBrowser1.Navigate("http://www.website.com")
    3.         End If
    4.  
    5. Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
    6.  
    7.             Dim q As String = WebBrowser1.Document.GetElementById("123").InnerText
    8.             txtOne.Text = (q)
    9.         End If

    If I add a second Navigate to the same WebBrowser control, and then add another Document.GetElementById line, using different variables, it doesn't work.

    Its obvious that it loads both sites and then it finishes. So, it never has time to finish the first load and then pull the info into the text box.

    Is there a way to do this? To load the first site, pull the info, load the second site, then pull the info from there?

    Thanks
    Last edited by weirddemon; May 13th, 2009 at 08:59 PM.
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Webbrowser Document Complete

    vb Code:
    1. Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click    
    2.       WebBrowser1.Navigate("http://www.website.com")
    3.       While Not WebBrowser1.ReadyState = WebBrowserReadyState.Complete        
    4.             Application.DoEvents()
    5.       End While      
    6.       txtOne.Text = WebBrowser1.Document.GetElementById("123").InnerText            
    7. End If

  3. #3

    Thread Starter
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,828

    Re: Webbrowser Document Complete

    Thanks, Paul
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  4. #4

    Thread Starter
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,828

    Re: Webbrowser Document Complete

    I thought I had this resolved, but apparently I'm mistaken.

    I put the code into two If statements that depend on the checked state of a check box.

    I tried this:
    vb.net Code:
    1. If CheckBox1.Checked And CheckBox2.Checked = True Then
    2.             WebBrowser1.Navigate("http://www.website.com")
    3.             While Not WebBrowser1.ReadyState = WebBrowserReadyState.Complete
    4.                 Application.DoEvents()
    5.             End While
    6.             Dim q As String = WebBrowser1.Document.GetElementById("123").InnerText
    7.             TextBox1.Text = (q)
    8.         End If
    9.  
    10.         If CheckBox3.Checked And CheckBox4.Checked = True Then
    11.             WebBrowser1.Navigate("http://www.website.com")
    12.             While Not WebBrowser1.ReadyState = WebBrowserReadyState.Complete
    13.                 Application.DoEvents()
    14.             End While
    15.             Dim q As String = WebBrowser1.Document.GetElementById("456").InnerText
    16.             TextBox2.Text = (q)
    17.         End If

    When I run the code, it loads the first item into the first box. After that, the code doesn't work.

    I think it has something to do with the state of the webbrowser, but i'm not sure.
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  5. #5
    PowerPoster sparrow1's Avatar
    Join Date
    May 2005
    Location
    Globetrotter
    Posts
    2,820

    Re: Webbrowser Document Complete

    Hi,

    Wouldn't be better to put these if statements in a select case statement.
    Wkr,
    sparrow1

    If I helped you, don't forget to Rate my post. Thank you

    I'm using Visual Studio.Net 2003 and
    2005
    How to learn VB.Net Create setup with VB 2005 Drawing for beginners VB.Net Tutorials GDI+ Tutorials
    Video's for beginners

  6. #6

    Thread Starter
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,828

    Re: Webbrowser Document Complete

    It probably would be, Sparrow. I've just been using If statements since I started and I didn't want to learn select case because If statements have been working.

    I didn't want to introduce something new and possibly have some more unknown problems.

    But, I should probably get to it finally. I'll see what I come up with.

    Thanks
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  7. #7
    Frenzied Member
    Join Date
    Mar 2005
    Location
    Sector 001
    Posts
    1,577

    Re: Webbrowser Document Complete

    DocumentCompleted can fire multiple times and only the last time will be the real McCoy. You can get lost in your code where one DocumentCompleted is waiting for the While to finish and another DocumentCompleted gets raised, your code starts another Do...While and so on.

    Try it with a simple If:
    If WebBrowser1.ReadyState = WebBrowserReadyState.Complete Then
    'your code

    A simpler (lamer) way would be:
    Code:
    If WebBrowser1.ReadyState <> WebBrowserReadyState.Complete Then
    Exit Sub
    End If
    
    'your code
    VB 2005, Win Xp Pro sp2

  8. #8
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Webbrowser Document Complete

    When DocumentCompleted fires, it also contains the URL in the eventargs for the page it just loaded. If it was a frame on the page it loaded, the URL will be specifically that frames URL, not the page you navigated to. When the event fires the final time, it should be for the actual URL you were navigated to.

  9. #9
    Junior Member scrapersNbots.com's Avatar
    Join Date
    Dec 2016
    Location
    Torrington, CT
    Posts
    25

    Re: Webbrowser Document Complete

    While Not WebBrowser1.ReadyState = WebBrowserReadyState.Complete IS NOT RELIABLE.
    There are many website today that NEVER fire a complete event.

  10. #10
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Webbrowser Document Complete

    Quote Originally Posted by scrapersNbots.com View Post
    While Not WebBrowser1.ReadyState = WebBrowserReadyState.Complete IS NOT RELIABLE.
    There are many website today that NEVER fire a complete event.
    You should have mentioned that 7.5 years ago, when this thread was last active

  11. #11
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: Webbrowser Document Complete

    Quote Originally Posted by scrapersNbots.com View Post
    While Not WebBrowser1.ReadyState = WebBrowserReadyState.Complete IS NOT RELIABLE.
    There are many website today that NEVER fire a complete event.
    Yes and no. You're right, websites don't fire a complete event... they don't fire any event. It's the webbrowser that fires it.
    But that's not where the unreliability comes in. The .ReadyState should be in the DocumentComplete event, and it shouldn't be in a loop. That's going to be the most accurate way to determine if a page has finished loading. But you're right in that it isn't 100% reliable, in that sites these days use AJAX and other methods to dynamically load stuff. THOSE don't fire the documentComplete event and those don't reflect the ReadyState. So you have to keep polling something on the page, before deciding if you should continue or not.

    So the DocumentComplete event along with checking the ReadyState IN the event (no in a click or in a loop) will get you there 90% of the time. But there are sometimes when that's not going to be enough.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  12. #12
    Junior Member scrapersNbots.com's Avatar
    Join Date
    Dec 2016
    Location
    Torrington, CT
    Posts
    25

    Re: Webbrowser Document Complete

    What I have done is created a control that I named BrowserMonitor which has a timer set at interval of 200

    Private Sub Tmr_Timer()
    On Error GoTo err:
    Static sText As String

    'the document won't have any innerText unless the document is complete (enough)

    Code:
    Dim sT As String:            sT = Trim(mWebBrowser.Document.body.innerText)
     
        'the text on the page has changed
        If sText <> sT Then
                      sText = sT
                      mWebBrowser.Silent = True
                      v.bBrowserReady = True
                      RaiseEvent BrowserReady
        End If
    When I have my program navigate to a webpage via webbrowser.navigate the next line of code is to a function that pauses and then the next line of code calls this function within the same control.

    Code:
    'call this function to see when the browser is ready
    Public Function bfBrowserReady() As Boolean
    
        Do 
               Pause.subFor 1
        Loop Until v.bBrowserReady = True
    
        bfBrowserReady = True
        v.bBrowserReady = False
    
    End Function

    so for example:

    Code:
    webbrowser1.navigate "http://www.scrapersnbots.com"
    pause.subFor 2
    if BrowserMonitor.bfBrowserReady = "false" then stop
    Because its a function call it won't return until it returns true

    By the way, the pause code is (separate module)


    Code:
    Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
    
    
    Sub subFor(ByVal Delay As Single)
    
    Delay = (Timer + Delay)
        
    Do
            DoEvents: DoEvents: If vars.v.bClosingApplication Then Exit Do: DoEvents: DoEvents
    
             Sleep 10
    Loop While Delay > Timer
    
    End Sub

    Surprisingly the whole thing has proven to be pretty reliable because even if only part of a page changes
    do to ajax call this still picks it up. I am determined to invent a real elegant solution, however, this is just for nows.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width