al78310 al78310 - 4 months ago 13
Vb.net Question

Get text in a div that doesn't have a name or id

I would like to get the text inside this code:

<div class="js-text-container"></div>


when there is an ID, i use getelementbyId, no problem, but in this case no ID and even nothing inside the 2 >< (although something is displayed)

I found an interesting solution here and tried to adapt it to my case:

Dim divs = WebBrowser1.Document.Body.GetElementsByTagName("div")

For Each d As HtmlElement In divs
If d.GetAttribute("class") = "js-text-container" Then
TextBox1.Text = d.InnerText
End If
Next


But nothing appears in my textbox. Do someone have an idea? I think its because
InnerText
refers to nothing in this case...
I hope I was clear enough.

Thanks a lot

Answer

Instead of d.GetAttribute("class") = "js-text-container" use

d.GetAttribute("className") = "js-text-container"

I tested it locally, I believe you can use it in VB.Net

foreach (HtmlElement el in webBrowser1.Document.GetElementsByTagName("div"))
if (el.GetAttribute("class") == "js-text-container")
{
     textBox1.Text = el.InnerText;
}

Hope it helps!