3 Replies - 1242 Views - Last Post: 25 November 2012 - 07:01 PM Rate Topic: -----

#1 LilGhost  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 98
  • Joined: 12-October 12

Get html code (Webbrowser control)

Posted 25 November 2012 - 01:26 PM

Hey, let's say i load up a webpage in the default webbrowser control and part of the source looks like:
<div class="missionName ">Invade Enemy Land</div>


How would I in my vb.net code go about getting the part that says "Invade Enemy Land"?

This is what i have:
        For Each ele As HtmlElement In WebBrowser1.document.All
            If ele.GetAttribute("class").Contains("missionName") Then
                ' This is where I need help.
            End If
        Next



Is This A Good Question/Topic? 0
  • +

Replies To: Get html code (Webbrowser control)

#2 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: Get html code (Webbrowser control)

Posted 25 November 2012 - 06:44 PM

Well, I would have thought that would work, and I tried to do the same thing. It would appear that I can parse attributes and successfully extract the InnerText, as long as the Attribute isn't "class". Here's what I tried on a file that has 7 occurrences of <a href="#top"><b><big>Back to Top</big></b></a> in it. It found all seven.
Public Class Form1

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        WebBrowser1.Navigate("file:///J:/AuroraMonhelp.html")
    End Sub

    Private Sub WebBrowser1_DocumentCompleted(sender As Object, e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
        For Each ele As HtmlElement In WebBrowser1.document.GetElementsByTagName("a")
            If ele.GetAttribute("href").Contains("#top") Then
                Dim content As String = ele.InnerText
                ListBox1.Items.Add(ele.InnerText)
            End If
        Next

    End Sub
End Class




But as soon as I try the same thing with TagName("div") and Attribute("class"), and Contains("missionName"), I get nothing. It will get to the If on each div tag, but at that point, both ele.InnerText and ele.InnerHtml both equal "Invade Enemy Land".

I wonder if there is something special about "Class" or a div. Tell me, does your program actually get to ' This is where I need help.? You could put something in there like a msgbox() to find out.

Anyone else have an idea?

This post has been edited by lar3ry: 25 November 2012 - 06:46 PM

Was This Post Helpful? 0
  • +
  • -

#3 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: Get html code (Webbrowser control)

Posted 25 November 2012 - 06:52 PM

Arrrgh! I knew thiss sounded familiar I looked back at a few of my older programs and found the solution:
Public Class Form1

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        WebBrowser1.Navigate("file:///J:/AuroraMonhelp.html")
    End Sub

    Private Sub WebBrowser1_DocumentCompleted(sender As Object, e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
        For Each ele As HtmlElement In WebBrowser1.document.GetElementsByTagName("div")
            If ele.GetAttribute("className").Contains("missionName") Then
                Dim content As String = ele.InnerText
                ListBox1.Items.Add(ele.InnerText)
            End If
        Next

    End Sub
End Class


Was This Post Helpful? 1
  • +
  • -

#4 LilGhost  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 98
  • Joined: 12-October 12

Re: Get html code (Webbrowser control)

Posted 25 November 2012 - 07:01 PM

Thanks for your help, i'll be trying that out. And i've tested my original code with a message box and it never triggered. I'll be trying what you suggested in your second post and i'll see if that works. Thanks for the help.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1