vb.net 模拟一个链接点击webbrowser控件vb.net

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17921351/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 14:26:38  来源:igfitidea点击:

Simulating a link click webbrowser control vb.net

vb.netwebbrowser-controlraiseeventinvokemember

提问by Osprey

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 "#"

我试图让我的应用程序单击 Web 浏览器控件中加载的页面内的链接,但似乎没有发生任何事情。手动单击链接有效(它执行一些 javascript 命令以使用 ajax 加载数据)。我不能简单地转到 URL,因为 HREF 是“#”

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")

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

不确定它是否会有所帮助,但是: wb.Document.GetElementById("MyElement").RaiseEvent("onmouseover")似乎部分模拟了链接上的鼠标悬停

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

我可以尝试模拟鼠标单击的任何其他选项吗?

Thanks!

谢谢!

回答by StevieTimes

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

我遇到过同样的问题。什么都行不通;RaiseEvent、Document.GetElementById(oLink.Id).InvokeMember("click") 等

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:

最后我通过循环遍历 Document.Links HTMLElementCollection 找到了链接;然后做了一个 link.Focus 和一个愚蠢的 SendKeys.Send("{ENTER}")。这有效!见下文:

        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}")

回答by John

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

回答by GrooveGeek

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:

首先,这是我对任何网站上提交的问题的第一个回答帖子,永远。我遇到了同样的问题,并根据之前的帖子提出了以下建议,至少在我的情况下有效并且避免使用 sendkey:

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.

如果我试图访问的链接有一个关联的 ID,我认为解决方案会更简单,不需要循环,但由于它没有,它就是这样。希望这对其他人有帮助。