问题描述
我正在做一个自动化程序.我将一个网页加载到我的 Windows 窗体中并将它加载到 WebBrowser 控件中.然后,我需要以编程方式单击来自 WebBrowser 的链接.我怎样才能做到这一点?例如:
I'm doing an automation program. I load a webpage into my windows form and load it in WebBrowser control. Then, I need to click on a link from the WebBrowser programatically. How can I do this? for example:
<a href="http://www.google.com">Google Me</a>
<a href="http://www.facebook.com" id="fbLink">Facebook Me</a>
以上是两种不同的情况.第一个元素没有 id
属性,而第二个元素有.关于如何以编程方式点击每一个的想法?
The above are 2 different conditions. The first element does not have an id
attribute while the second one does. Any idea on how to click each programmatically?
推荐答案
您必须首先通过 ID 或其他过滤器找到您的元素:
You have to find your element first, by its ID or other filters:
HtmlElement fbLink = webBrowser.Document.GetElementByID("fbLink");
并模拟点击":
fbLink.InvokeMember("click");
通过内部文本查找链接的示例:
An example for finding your link by inner text:
HtmlElement FindLink(string innerText)
{
foreach (HtmlElement link in webBrowser.Document.GetElementsByTagName("a"))
{
if (link.InnerText.Equals("Google Me"))
{
return link;
}
}
}
这篇关于如何使用 HTMLElement 以编程方式单击链接元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!