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 以编程方式单击链接元素?
基础教程推荐
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
