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 以编程方式单击链接元素?
基础教程推荐
- 禁止输入少量字符,例如'<'、'&a 2022-01-01
- 如何使用 .Net 检查 Active Directory 服务器是否已启动并正在运行? 2022-01-01
- 如果有人提交恶意软件Nuget包怎么办? 2022-01-01
- 当值可以是对象或空数组时反序列化 JSON 2022-01-01
- Moq It.Is<>不匹配 2022-01-01
- 我应该在后面的代码中直接使用 Linq To SQL 还是使 2022-01-01
- .NET SerialPort DataReceived 事件未触发 2022-01-01
- WPF 模态进度窗口 2022-01-01
- Azure Functions:CosmosDBTrigger 未在 Visual Studio 中触发 2022-01-01
- C# 从 List<List<int>> 中删除重 2022-01-01
