MS1995
MS1995

Reputation: 11

how to get text from html element class vb.net?

I have this element

<div id="cp-0" class="caption-line" data-time="19.5">
   <div class="caption-line-time">0:19</div>
   <div class="caption-line-text">I used to bite my tongue and hold my breath Scared to rock the boat and make a mess</div>             
</div>

I want to get

0:19
I used to bite my tongue and hold my breath Scared to rock the boat and make a mess

Upvotes: 0

Views: 5401

Answers (2)

MS1995
MS1995

Reputation: 11

thanks for all i solve it by this

    If SaveFileDialog1.ShowDialog = DialogResult.OK Then
             FileOpen(1, SaveFileDialog1.FileName, OpenMode.Output)
           For Each telement As HtmlElement In WebBrowser1.Document.GetElementsByTagName("div")
            Dim tElementClass As String = "caption-line-time"
            Dim selementclass As String = "caption-line-text"
            If telement.OuterHtml.Contains(tElementClass) AndAlso telement.OuterHtml.Contains(selementclass) Then
            Dim conStr As String = telement.GetAttribute("innertext")
            PrintLine(1, conStr)

            End If
               Next
   End If


        FileClose(1)

    End If

Upvotes: 0

Gabor
Gabor

Reputation: 3246

What you need is the InnerText property of an XmlElement.

Details and an example program:
https://msdn.microsoft.com/en-us/library/system.xml.xmlelement.innertext%28v=vs.110%29.aspx

UPDATE:
You may use HtmlElement type as well. It also has InnerText property.

You can also access the inner text by casting the HtmlElement to one of the unmanaged MSHTML interfaces, e.g. IHTMLElement which has the innerText property.

You have to add Microsoft HTML Object Library COM reference to your project.
(The file name is mshtml.tlb.)

Here is an example:

For Each child As HtmlElement In element.Children

    ' using System.Windows.Forms.HtmlElement type
    If (String.Equals(child.TagName, "div") AndAlso
        String.Equals(child.GetAttribute("class"), "caption-line-time")) Then

        Console.WriteLine("Time: {0}", child.InnerText)

    End If

    ' using mshtml.IHTMLElement interface
    Dim htmlElement As IHTMLElement = DirectCast(child.DomElement, IHTMLElement)

    If (String.Equals(htmlElement.tagName, "div") AndAlso
        String.Equals(htmlElement.className, "caption-line-time")) Then

        Console.WriteLine("Time: {0}", htmlElement.innerText)

    End If
Next

Upvotes: 2

Related Questions