-
Jul 7th, 2016, 11:58 PM
#1
Thread Starter
Banned
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!
-
Jul 8th, 2016, 01:08 AM
#2
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
-
Jul 8th, 2016, 01:31 AM
#3
Thread Starter
Banned
Re: Get the data of Anchor tag.
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
-
Jul 8th, 2016, 01:37 AM
#4
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
-
Jul 8th, 2016, 01:58 AM
#5
Thread Starter
Banned
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.
-
Jul 8th, 2016, 04:04 AM
#6
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.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|