Webbrowser Document Complete-VBForums
Results 1 to 8 of 8

Thread: Webbrowser Document Complete

  1. #1

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

    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
    vb Coda .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    18,896

    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,827

    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,827

    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,817

    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,827

    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,579

    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
    Moderator.NET kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,203

    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.
    Using VS.NET 2013/.NET 2.0 through 4.5 * Please mark you thread resolved using the Thread Tools above
    PLEASE INDICATE WHAT VERSION OF VB YOU USE!!!!!!!!!!!
    * If you found a post useful then please Rate it! * DO NOT PM ME WITH LINKS TO YOUR THREADS FOR ANSWERS PLEASE!

    Code Bank:Manipulate HTML Page content in the Web Browser Control from VB - Drag Drop from Windows into Win Form - Launch new default browser instance to open URL - Display Internet Image in Picturebox - Download Files From Web With Progress Bar - IP Textbox User Control - Installing .NET Framework with INNO Setup
    ZerosAndTheOne.com
    -=Matt=-

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.