Select span, and grab the text inside

Asked

Viewed 893 times

1

I have the following HTML code:

<div class="dados">

<span><strong>Nome:</strong> Yuri Santos</span>
<span><strong>E-mail:</strong> [email protected]</span>

<span><strong>Endereço:</strong> Rua treze</span>

<span><strong>Cidade:</strong> Dos Sonhos</span>

</div>

And using a WebBrowser I want to take the values that are within a certain span.

However, I want to select which ones, e.g.: Name, Email.

I have the following code:

For Each h As HtmlElement In WebBrowser1.Document.GetElementsByTagName("div")
        If Not Object.ReferenceEquals(h.GetAttribute("className"), Nothing) AndAlso h.GetAttribute("className").Equals("dados") Then
            txtDados.Text = h.InnerText
            Exit For
        End If
   Next

However, it takes all values (name, email, address, city).

Like they don’t have ID or nome, i don’t know how to identify them to catch only the ones I want.

  • Take a look at [tour]. You can accept an answer if it solved your problem. You can vote on every post on the site as well. Did any help you more? You need something to be improved?

1 answer

1

Don’t use the WebBrowser this is a page renderer and you don’t need it. Or use the WebClient or better still use the library HTML Agility Pack who does this more appropriately.

These tools will help but you have to do a little "manual labor". What you will have at your disposal is a more organized and easier to filter data structure.

  • Okay, I’ll look it up. Thanks.

Browser other questions tagged

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