5

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?

avs099
  • 10,937
  • 6
  • 60
  • 110
Rafik Bari
  • 4,867
  • 18
  • 73
  • 123
  • I don't really understand your question, but do you mean holding ctrl and clicking the link? – Max Apr 22 '13 at 14:31
  • I found some interesting information here: http://webkitdotnet.sourceforge.net/docs/ – Max Apr 22 '13 at 14:40
  • Could you simply navigate to the link href after loading the page? – jamesthollowell Mar 01 '14 at 04:46
  • A click can be different than navigate based on the page. There can be javascript event handlers which won't be executed if you use navigate. If href is pure javascript, then navigate call may not work at all. – user871199 Mar 03 '14 at 18:53
  • 1
    Have you tried something like: webKitBrowser1.StringByEvaluatingJavaScriptFromString(document.getElementById('lnkId').click()) – Paul Zahra Mar 06 '14 at 11:59

3 Answers3

5

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.

Wasif Hossain
  • 3,900
  • 1
  • 18
  • 20
  • I'm the one with the bounty, but I didn't want to ask another question that would be pretty much a duplicate. I am setting the text in a textbox, and I have to do it the way you posted, but now there is not a option to `SetAttribute`. How do I set the attribute? – Dozer789 Mar 03 '14 at 16:48
  • Would you please check the methods what a `WebKit.DOM.Element` class offers? You can use your required method like this: `((WebKit.DOM.Element) el).SetAttribute("NAME", "VALUE");` – Wasif Hossain Mar 04 '14 at 06:33
  • I figured that part out myself, but I forgot to tell you that. – Dozer789 Mar 04 '14 at 17:59
  • Please be aware that navigate call will not work in many cases depending upon how the page is developed. If you have control over the page, there are no problems as you can adjust href. If you don't have control over the page, then above method will not generate "onclick" event and many sites use it to change functionality. Moreover some hrefs can be javascript code. In other words, navigate is not always same is click on the link – user871199 Mar 07 '14 at 19:25
1

I don't work with Windows and all my experience is on Webkit GTK. Following comments are based on that experience.

I am not sure which webkit .NET version you are using. Looks like there are multiple implementations. Assuming you are using the one mentioned by Wasif, you can evaluate javascript as mentioned in the example https://code.google.com/p/open-webkit-sharp/source/browse/JavaScriptExample/Form1.cs.

Actually if implementation is supporting javascript execution then you can do most, if not all the DOM operations. The API functions are usually same as javascript functions and most of the time call exact same functions internally despite of origination. Communication between your application and javascript can be little challenging, but if you can read alert messages, that also can be solved. It looks like this library does support alert handling mechanism. A tool I wrote at https://github.com/nhrdl/notesMD will show some examples of achieving this communication though it uses GTK version and is written in python.

Incidentally if you know the id of the element, then Document.GetElementById will save you the loop.

user871199
  • 1,420
  • 19
  • 28
-1
webKitBrowser1.StringByEvaluatingJavaScriptFromString("var inpt = document.createElement(\"input\");    inpt.setAttribute(\"type\", \"submit\");    inpt.setAttribute(\"id\", \"nut\"); inpt.setAttribute(\"type\", \"submit\");    inpt.setAttribute(\"name\", \"tmp\");   inpt.setAttribute(\"value\", \"tmp\");  var element = document.getElementById(\"lnk\"); element.appendChild(inpt);");
                    webKitBrowser1.StringByEvaluatingJavaScriptFromString("document.getElementById('nut').click();");
serenesat
  • 4,611
  • 10
  • 37
  • 53