Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to click on a link using Webkit Browser?

Tags:

c#

.net

webkit

I want to click on link after navigating to a website

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

How to click on a link on this website assuming that the link's id is lnkId ?

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

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");
 }
 }

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

like image 747
Rafik Bari Avatar asked Apr 22 '13 14:04

Rafik Bari


1 Answers

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);
    }
}

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.

like image 170
Wasif Hossain Avatar answered Sep 24 '22 17:09

Wasif Hossain