Keanu Lorenzo Keanu Lorenzo - 5 months ago 29 Question

How to get the value of 'href' without id VB

I know this is a silly question but i'm having a hard time with this. I want to get the value of 'href' without an id but i can't.

Here is the HTML

<p class="CLASS">
<a href="URL" target="TARGET">
<img src="IMGURL" title="TITLE" border="BORDER">

Here is what i am using(that doesn't work):

For Each WPE As HtmlElement In WebBrowser1.Document.GetElementsByTagName("a")
If WPE.GetAttribute("target").Equals("TARGET") Then
HREFVALUE = WPE.Getattribute("href")
Exit For
End If

So how to get the value of 'href'?


You code works properly. But you didn't get expected result because you probably execute the code in an incorrect place. The code should be run after the document completed. A good place to know the document has been completed is DocumentCompleted event of the WebBrowser control:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub WebBrowser1_DocumentCompleted(sender As Object, e As _
    WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted

    Dim tag = Me.WebBrowser1.Document.GetElementsByTagName("a").Cast(Of HtmlElement) _
                .Where(Function(a) a.GetAttribute("target") = "TARGET") _
    Dim href = tag.GetAttribute("href")
End Sub

I used linq in above code just for learning purpose. Your own code also works fine.