How to take the Text of a Div Class and Put it inside a Label?

Asked

Viewed 1,072 times

1

I’m trying to take the "value" inside the div to put inside a label.

Div:

<div class="info">valor</div>

First Attempt

Variavel1 = WebBrowser1.Document.GetElementById("info").GetAttribute("innerText")

Label1.Text = Variavel1

Second Attempt

For Each Element As HtmlElement In WebBrowser1.Document.GetElementsByTagName("div")
    If Element.OuterHtml.Contains("class") = "info" Then
       Variavel1 = Element.GetAttribute("innerText")
       Return
    End If
Next

Label1.Text = Variavel1

I found that on the page there are several classes "Info"...

inserir a descrição da imagem aqui

This code worked, but brought another result, beyond the expected...

    Dim theElementCollection As HtmlElementCollection
    theElementCollection = WebBrowser1.Document.GetElementsByTagName("Div")
    For Each curElement As HtmlElement In theElementCollection
        If curElement.OuterHtml.Contains("info") Then
            Variavel1 = (curElement.GetAttribute("InnerText"))
        End If
    Next

Label1.Text = Variavel1

The result was the value of this Div that also has the class "info".

<div class="msg"><div class="info">OMG/BTC and OMG/ETH  OmiseGO markets added</div>
  • If you have several div s with classes called "info" it is difficult to define which "info" you want to bring, by your code that worked, only the last one will be stored, since inside the foreach is traversed all div s that call "info".

1 answer

0

Browser other questions tagged

You are not signed in. Login or sign up in order to post.