我正在做一个自动化程序。我将网页加载到我的 Windows 窗体中并将其加载到 WebBrowser 控件中。然后,我需要以编程方式单击来自 WebBrowser 的链接。我怎样才能做到这一点?例如:
<a href="http://www.google.com">Google Me</a>
<a href="http://www.facebook.com" id="fbLink">Facebook Me</a>
以上是两种不同的情况。第一个元素没有 id
属性,而第二个属性。关于如何以编程方式单击每个的任何想法?
最佳答案
您必须首先通过其 ID 或其他过滤器找到您的元素:
HtmlElement fbLink = webBrowser.Document.GetElementByID("fbLink");
并模拟“点击”:
fbLink.InvokeMember("click");
通过内部文本查找链接的示例:
HtmlElement FindLink(string innerText)
{
foreach (HtmlElement link in webBrowser.Document.GetElementsByTagName("a"))
{
if (link.InnerText.Equals("Google Me"))
{
return link;
}
}
}
关于c# - 如何使用 HTMLElement 以编程方式单击链接元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5309337/