Simulating a link click webbrowser control vb.net

Osprey picture Osprey · Jul 29, 2013 · Viewed 11.9k times · Source

I am trying to make my application click a link that is inside a page loaded in a web browser control but nothing seems to be happening. Clicking manually on the link works (it performs some javascript command to load an data using ajax). I cannot simply go to the URL since the HREF is "#"

So far I have tried these methods:

wb.Document.GetElementById("MyElement").InvokeMember("click") wb.Document.GetElementById("MyElement").RaiseEvent("onmousedown") wb.Document.GetElementById("MyElement").RaiseEvent("onclick")

Not sure if it will help, but: wb.Document.GetElementById("MyElement").RaiseEvent("onmouseover") Seems to partially simulate a mouseover on the link

Any other options I can try to simulate a mouse click?

Thanks!

Answer

StevieTimes picture StevieTimes · Apr 3, 2014

I had the same issue. Nothing would work; RaiseEvent, Document.GetElementById(oLink.Id).InvokeMember("click"), etc.

Finally I found the link by looping through the Document.Links HTMLElementCollection; then did a link.Focus, and a stupid SendKeys.Send("{ENTER}"). This worked! See below:

        Dim bFound As Boolean = False
        Dim oLink As HtmlElement = Nothing

        For Each oLink In wbExample.Document.Links
            If oLink.InnerText IsNot Nothing _
            AndAlso oLink.InnerText.ToString.Trim = "12345" Then
                bFound = True
                Exit For
            End If
        Next

        If bFound = False Then
            Throw New Exception("Big time lameness; can't find the link.")
        End If

        oLink.Focus()
        SendKeys.Send("{ENTER}")