How to click a link element programmatially with HTMLElement?(如何使用 HTMLElement 以编程方式单击链接元素?)
问题描述
我正在做一个自动化程序.我将一个网页加载到我的 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 以编程方式单击链接元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使用 HTMLElement 以编程方式单击链接元素?
基础教程推荐
- SonarQube C# 分析失败“不是指针的有效行偏移" 2022-01-01
- C# 9 新特性——record的相关总结 2023-04-03
- 将数据集转换为列表 2022-01-01
- 更新 Visual Studio 中的 DataSet 结构以匹配新的 SQL 数据库结构 2022-01-01
- 重新排序 WPF TabControl 中的选项卡 2022-01-01
- Mono https webrequest 失败并显示“身份验证或解密失败" 2022-01-01
- 从 C# 控制相机设备 2022-01-01
- 获取C#保存对话框的文件路径 2022-01-01
- 在 VB6 或经典 ASP 中使用 .NET 2022-01-01
- 如果条件可以为空 2022-01-01
