Get the data of Anchor tag.
Hello there,
I have added WebBrowser component on my form. It loads my own website on webbrowser.navigate(). Then I login in and navigate to my private page.
Now, I want to grab some data between anchor tag. There are multiple anchor tags in that page. Like,
HTML Code:
<a href="something" class-index="something" id="something"> My Data </a>
<a href="something" class-index="something" id="something"> My Data 2</a>
<a href="something" class-index="something" id="something"> My Data 3</a>
I want to loop through get these values "My Data","My Data 2","My Data 3" ... and so on..
How can I do this ?
Thanks!
Re: Get the data of Anchor tag.
Use the InnerText property to obtain content values from anchor tags.
VB.NET Code:
Dim myData As New List(Of String)
Dim anchorCollection As HtmlElementCollection
anchorCollection = WebBrowser1.Document.GetElementsByTagName("a")
If (Not anchorCollection Is Nothing And anchorCollection.Count > 0) Then
For Each element As HtmlElement In anchorCollection
myData.Add(element.InnerText)
Next
End If
Re: Get the data of Anchor tag.
Quote:
Originally Posted by
KGComputers
Use the InnerText property to obtain content values from anchor tags.
VB.NET Code:
Dim myData As New List(Of String)
Dim anchorCollection As HtmlElementCollection
anchorCollection = WebBrowser1.Document.GetElementsByTagName("a")
If (Not anchorCollection Is Nothing And anchorCollection.Count > 0) Then
For Each element As HtmlElement In anchorCollection
myData.Add(element.InnerText)
Next
End If
Thanks for your reply! The problem with this is that it is fetching all data with Anchor tag. I have specified some attributes to the Anchor tage like,
<a href="something" data-hovercard="something" data-gt="">My data</a>
Is it possible to detect data-hovercard or data-gt and only return those data having this tag ?
Anyway again thanks :)
Re: Get the data of Anchor tag.
Yes, just add condition to get the attribute value of a specific tag.
VB.NET Code:
For Each element As HtmlElement In anchorCollection
If element.GetAttribute("data-hovercard").Equals("something") Then
myData.Add(element.InnerText)
End If
Next
Re: Get the data of Anchor tag.
Actually, I cannot pretend the value of "data-hovercard" because it's dynamic. Just want to check if it is there. And if it exists then show the inner value.
Re: Get the data of Anchor tag.
Just revise the If condition to check whether data-hovercard has a value or empty. If it has value, then add it to myData list object.