Reputation: 1545
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!
Upvotes: 0
Views: 12495
Reputation: 1
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.
Upvotes: 0
Reputation: 21
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
Upvotes: 0
Reputation: 465
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}")
Upvotes: 2