如何点击使用WebKit浏览器链接? [英] How to click on a link using Webkit Browser?

查看:2505
本文介绍了如何点击使用WebKit浏览器链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想浏览一个网站后,点击链接

I want to click on link after navigating to a website

 webKitBrowser1.Navigate("http://www.somesite.com");

如何点击一个链接,这个网站假设链接的id是 lnkId

<a href="http://www.google.com" id="lnkId"> Go to Google </a>

在随Visual Studio中的默认浏览器控制,我可以做到这一点使用下面的code:

In the default browser control that comes with Visual Studio, I can do that using the code below :

 foreach (HtmlElement el in webBrowser1.Document.GetElementTagName("a")) {
 if (el.GetAttribute("id") == "lnkId") {
 el.InvokeMember("click");
 }
 }

什么是的code,当我使用WebkitDotNet控制上面的相同呢?

What is the equivalent of the code above when I'm using WebkitDotNet control?

推荐答案

随着的WebKit 不提供点击()事件(看到此处查看详情),你不能这样做,以上述方式。但是,一个小窍门可以工作作为原的WinForms 如下方式等效:

As the WebKit doesn't provide a Click() event (see here for details), you cannot do that in the above way. But a small trick may work as an equivalent of the original winforms way as below:

foreach (Node el in webKitBrowser1.Document.GetElementsByTagName("a"))
{
    if (((Element) el).GetAttribute("id") == "lnkId")
    {
        string urlString = ((Element) el).Attributes["href"].NodeValue;
        webKitBrowser1.Navigate(urlString);
    }
}

下面我做投射 WebKit.DOM.Node 对象到其子 WebKit.DOM.Element 以获得其属性。然后提供的href 的NamedNodeMap ,即属性作为节点名,你可以很容易地提取的nodeValue ,这是目标网址在这种情况下。然后,您可以简单地调用上的 WebKitBrowser 实例导航(urlString)的方法来复制点击事件。

Here what I am doing is casting the WebKit.DOM.Node object to its subclass WebKit.DOM.Element to get its Attributes. Then providing href to the NamedNodeMap, i.e. Attributes as the NodeName, you can easily extract the NodeValue, which is the target url in this case. You can then simply invoke the Navigate(urlString) method on the WebKitBrowser instance to replicate the click event.

这篇关于如何点击使用WebKit浏览器链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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