Results 1 to 6 of 6

Thread: Get the data of Anchor tag.

  1. #1

    Thread Starter
    Banned
    Join Date
    Jun 2014
    Location
    https://t.me/pump_upp
    Posts
    41

    Exclamation 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!

  2. #2
    Frenzied Member KGComputers's Avatar
    Join Date
    Dec 2005
    Location
    Cebu, PH
    Posts
    2,020

    Re: Get the data of Anchor tag.

    Use the InnerText property to obtain content values from anchor tags.

    VB.NET Code:
    1. Dim myData As New List(Of String)
    2. Dim anchorCollection As HtmlElementCollection
    3. anchorCollection = WebBrowser1.Document.GetElementsByTagName("a")
    4. If (Not anchorCollection Is Nothing And anchorCollection.Count > 0) Then
    5.     For Each element As HtmlElement In anchorCollection
    6.         myData.Add(element.InnerText)
    7.     Next
    8. End If
    CodeBank: VB.NET & C#.NET | ASP.NET
    Programming: C# | VB.NET
    Blogs: Personal | Programming
    Projects: GitHub | jsFiddle
    ___________________________________________________________________________________

    Rating someone's post is a way of saying Thanks...

  3. #3

    Thread Starter
    Banned
    Join Date
    Jun 2014
    Location
    https://t.me/pump_upp
    Posts
    41

    Re: Get the data of Anchor tag.

    Quote Originally Posted by KGComputers View Post
    Use the InnerText property to obtain content values from anchor tags.

    VB.NET Code:
    1. Dim myData As New List(Of String)
    2. Dim anchorCollection As HtmlElementCollection
    3. anchorCollection = WebBrowser1.Document.GetElementsByTagName("a")
    4. If (Not anchorCollection Is Nothing And anchorCollection.Count > 0) Then
    5.     For Each element As HtmlElement In anchorCollection
    6.         myData.Add(element.InnerText)
    7.     Next
    8. 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

  4. #4
    Frenzied Member KGComputers's Avatar
    Join Date
    Dec 2005
    Location
    Cebu, PH
    Posts
    2,020

    Re: Get the data of Anchor tag.

    Yes, just add condition to get the attribute value of a specific tag.
    VB.NET Code:
    1. For Each element As HtmlElement In anchorCollection
    2.       If element.GetAttribute("data-hovercard").Equals("something") Then
    3.                     myData.Add(element.InnerText)
    4.       End If
    5. Next
    CodeBank: VB.NET & C#.NET | ASP.NET
    Programming: C# | VB.NET
    Blogs: Personal | Programming
    Projects: GitHub | jsFiddle
    ___________________________________________________________________________________

    Rating someone's post is a way of saying Thanks...

  5. #5

    Thread Starter
    Banned
    Join Date
    Jun 2014
    Location
    https://t.me/pump_upp
    Posts
    41

    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.

  6. #6
    Frenzied Member KGComputers's Avatar
    Join Date
    Dec 2005
    Location
    Cebu, PH
    Posts
    2,020

    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.
    CodeBank: VB.NET & C#.NET | ASP.NET
    Programming: C# | VB.NET
    Blogs: Personal | Programming
    Projects: GitHub | jsFiddle
    ___________________________________________________________________________________

    Rating someone's post is a way of saying Thanks...

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
  •  



Click Here to Expand Forum to Full Width