Results 1 to 3 of 3

Thread: Get HTML code not workin

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2011
    Posts
    346

    Get HTML code not workin

    When i run this code to get results from yahoo or google it does not work and it says that that ids not there but it always is."Object reference not set to an instance of an object."
    Code:
    
    Public Class Form2
    
    
        Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
            If (google.Checked = True) Then
                Dim PageElements As HtmlElement = WebBrowser1.Document.GetElementById("rso")
                TextBox2.Text = PageElements.OuterHtml
                Dim text = TextBox2.Text
            End If
            If (yahoo.Checked = True) Then
                Dim PageElements As HtmlElement = WebBrowser1.Document.GetElementById("we")
                TextBox2.Text = PageElements.OuterHtml
                Dim text = TextBox2.Text
            End If
    
    
        End Sub
    
            Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
                WebBrowser1.Navigate(TextBox1.Text)
            End Sub
    
        Private Sub Google_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Google.CheckedChanged
            TextBox1.Text = "http://www.google.com/search?q="
        End Sub
    
        Private Sub Yahoo_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Yahoo.CheckedChanged
            TextBox1.Text = "http://search.yahoo.com/web?fr="
        End Sub
    End Class

  2. #2
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,686

    Re: Get HTML code not workin

    The attached VS2010 project shows how to get elements from a WebBrowser set to www.google.com, determine what was typed in for a search and how to set the search text. I don't use Yahoo or care to even tinker with Yahoo so nothing for Yahoo.

    I fully tested it as is not to cause a run time exception.
    Attached Files Attached Files

  3. #3
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Get HTML code not workin

    I guess that the problem is caused by attempting to access the Document Object before the page requested has loaded. You could use the 'sender' object and the ReadyState property of the WebBrowser to make sure
    Code:
        Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
            If sender Is WebBrowser1 And WebBrowser1.ReadyState = WebBrowserReadyState.Complete Then
                If (google.Checked = True) Then
                    Dim PageElements As HtmlElement = WebBrowser1.Document.GetElementById("rso")
                    TextBox2.Text = PageElements.OuterHtml
                    Dim text = TextBox2.Text
                End If
                If (yahoo.Checked = True) Then
                    Dim PageElements As HtmlElement = WebBrowser1.Document.GetElementById("we")
                    TextBox2.Text = PageElements.OuterHtml
                    Dim text = TextBox2.Text
                End If
            End If
    
    
        End Sub

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