I have a Windows aplication where i want to click a link in a website automatically after an automatic login.Now i am done with automatic log in.but unable to click the link which is enclosed inside <a> tag
eg:
<a>to be clicked link</a>
any help will be appriciated

Dani AI

Generated

Building on 's idea, the key difference is how you "click." If the link has a normal URL in href, navigating to that URL works. But if the link is javascript:void(0) or relies on an onclick handler, you need to fire the element's click event. Also, , that NullReferenceException almost always means the DOM was not fully loaded (or the element was not found) when your code ran.

Private Sub WebBrowser1_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs) _
    Handles WebBrowser1.DocumentCompleted

    ' Fire only for the top-level document, not frames
    If WebBrowser1.Url Is Nothing OrElse e.Url Is Nothing _
       OrElse WebBrowser1.Url.ToString() <> e.Url.ToString() Then Return

    Dim doc = WebBrowser1.Document
    If doc Is Nothing Then Return

    Dim link = doc.GetElementById("My-Link")  ' works even with hyphens in the id
    If link IsNot Nothing Then
        link.InvokeMember("click")            ' triggers onclick for javascript:void(0)
    End If
End Sub

No id? You can still target by inner text or other attributes. This avoids NullReference and handles dynamic pages after login.

Dim doc = WebBrowser1.Document
If doc Is Nothing Then Return

For Each a As HtmlElement In doc.GetElementsByTagName("a")
    If a.InnerText IsNot Nothing AndAlso
       a.InnerText.Trim().Equals("to be clicked link", StringComparison.OrdinalIgnoreCase) Then
        a.InvokeMember("click")
        Exit For
    End If
Next

Extra tips:

  • If the link lives inside an iframe, handle DocumentCompleted for that frame or access it via Document.Window.Frames(index).
  • Some pages build links via AJAX. If DocumentCompleted fires before the link exists, retry with a short Timer until the element appears.
  • , for your example with href="javascript:void(0)", InvokeMember("click") is the correct approach; navigating to href will not do anything.

Recommended Answers

All 4 Replies

Assuming that you are using the vb.net webbrowser, read on.
If this is for a login to a website, the link should have an Id. For example, using the following html page to navigate to,

<html><head><title>Untitled Page</title></head>
<body>
<a href="http://www.daniweb.com" id="myLink">to be clicked link</a>
</body>
</html>

and using the following code to get the proper link by id,

Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
        For Each link As HtmlElement In WebBrowser1.Document.Links
            If link.Id = "myLink" Then
                WebBrowser1.Navigate(link.GetAttribute("href"))
                Exit For
            End If
        Next
    End Sub

my webbrowser navigates to http://www.daniweb.com.

ya....that worked out.thank you...and sorry for the late reply.

When I run this, I get the following error: NullReferenceException was unhandled by user code - Object reference not set to an instance of an object. Do I need to declare something to make it work?

How about something like this how do you get the ID to be CLICK

<span style="display: inline;" class="v2"><a href="javascript:void(0)" id="My-Link">CLICK ME</a> <span style="font-weight: bold;" class="blpz">2.5</span> ETC </span>

Thanks :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.