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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With