如何点击使用WebKit浏览器链接?浏览器、链接、WebKit

由网友(月亮杂货铺)分享简介:我想浏览一个网站后,点击链接I want to click on link after navigating to a website webKitBrowser1.Navigate("http://www.somesite.com");如何点击一个链接,这个网站假设链接的id是 lnkId ?我想浏览一个网站后,点击链接

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.

阅读全文

相关推荐

最新文章