0

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!

Osprey
  • 1,523
  • 8
  • 27
  • 44
  • your first try, wb.Document.GetElementById("MyElement").InvokeMember("click") should work, provided that your ID is correct. Post the html for the link – Riv Jul 29 '13 at 11:07
  • Yes, it should. But for some reason it doesn't. :) – Osprey Jul 29 '13 at 11:17
  • Have to tried to get the link as HtmlElementCollection? Dim links As HtmlElementCollection = wb.Document.GetElementsByTagName("a")... for each link -> link.InvokeMember("click") – varocarbas Jul 29 '13 at 11:19
  • Getting this message when I try to declare "links" that way: `Unable to cast object of type 'System.Windows.Forms.HtmlElementCollection' to type 'mshtml.HTMLElementCollection'.` – Osprey Jul 29 '13 at 11:38
  • Tried `For Each link In wb.Document.GetElementsByTagName("a")` without declaring the "links" object and bypassed the error. But still, the click event does not trigger. – Osprey Jul 29 '13 at 11:53
  • Sorry I didn't know you have replied (next time write @myusername). You should update the types on account of the error message you got; but what is clear is that we are not relying on the same webbrowser; I guess that you are using ASP.NET. Do it as the error message is telling you to do: Dim links As 'mshtml.HTMLElementCollection = wb.Document.GetElementsByTagName("a")... not sure if it will work because your browser belongs to a different namespace on which I don't have any experience; but look for the equivalence to HtmlElementCollection. – varocarbas Jul 29 '13 at 13:21
  • PS: and next time be more descriptive on the tags (include an ASP.NET one) or/and in the code: show how you declare the variables (in this way, anyone will know where this code belongs exactly to). – varocarbas Jul 29 '13 at 13:23
  • @varocarbas : I am not using ASP.Net and that is why I did not put that tag. I am using VB.Net in a desktop application with Visual Studio 2010. As pointed out in a later comment, I went around the casting problem (which is not the real problem). Still, the InvokeMember command is not working on that particular link (although it works on other links in the same page). The only way I could make things work was to simulate a mouse cursor move (after calculating the location of the button) and simulate a mouse button click. The problem with this is that the app needs to remain the active window. – Osprey Jul 30 '13 at 13:13
  • OK. Then perhaps you are using WPF or Silverlight or any other alternative. With conventional VB.NET winforms (what anyone understands from the tag VB.NET without any other addition) you don't get this error; this mshtml namespace does not come by default. – varocarbas Jul 30 '13 at 13:21
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/34450/discussion-between-osprey-and-varocarbas) – Osprey Jul 30 '13 at 13:22

3 Answers3

2

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}")
StevieTimes
  • 389
  • 3
  • 9
0

I had the same issue... This works.

For Each Mylink As HtmlElement In WebBrowser1.Document.Links
    If Mylink.InnerText.Contains("SomeTextToSearchFor") Then
        WebBrowser1.Navigate(Mylink.GetAttribute("href"))
    End If
Next
John
  • 21
  • 2
0

First of all, this is my first answer post to a submitted issue on any site, ever. I had this same issue, and came up with the following based on earlier posts, which worked, at least in my situation and avoided the use of sendkeys:

Dim oLink As HtmlElement = Nothing

For Each oLink In WebBrowser1.Document.Links
    If oLink.InnerText IsNot Nothing _
       AndAlso oLink.InnerText.ToString.Trim = "TextToSearchFor" Then
        oLink.InvokeMember("click")
        Exit For
    End If
Next

If the link I was trying to access had an ID associated with it, I think the solution would have been even simpler, not requiring the loop, but since it didn't, it is what it is. Hope this helps someone else.