如何点击一个链接元素programmatially与HTML元素? [英] How to click a link element programmatially with HTMLElement?

查看:172
本文介绍了如何点击一个链接元素programmatially与HTML元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做一个自动化程序。我的网页加载到我的窗口形式和WebBrowser控件加载它。然后,我需要点击从web浏览器编程中的链接。我怎样才能做到这一点?例如:

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:


  1. < A HREF =htt​​p://www.google.com>谷歌ME< / A>

< A HREF =htt​​p://www.facebook.comID =fbLink> Facebook的ME< / A>

以上是2个不同的条件。而第二个做的第一要素不具有 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;
        }
    }
}

这篇关于如何点击一个链接元素programmatially与HTML元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆