Reputation: 1
<a class="button" style="letter-spacing: -1px" href="/home" data-executing="0">Go Home</a>
I am trying to get Awesomium to click a certain button, Take the example above there is no ID only a tag
<a - and attribute being Go Home
I have been reading on how to do this and they say to use this:
web.ExecuteJavascript(@"$('a').trigger('click');");
This doesn't work for me, it doesn't produce the click. It could be because my test site has many tags using
<a
Is there a way to click this button by using the attribute of "a" being "Go Home" in Awesomium? I have also tried this and it also did not work:
private void timer_Tick(object sender, EventArgs e)
{
if (web.IsDocumentReady)
{
dynamic document = web.ExecuteJavascriptWithResult("document");
dynamic submit = document.getElementsByTagName('a');
submit.Invoke("click");
}
}
Upvotes: 0
Views: 3613
Reputation: 3787
If this element is not a <button>
it probably doesn't have the click()
method.
You can try this to check it:
JSObject btn = web.ExecuteJavascriptWithResult("document.getElementsByTagName('a')[0]");
if (btn.HasMethod("click"))
btn.Invoke("click");
else
// no such method
or using dynamic (will throw exception if the method doesn't exist):
dynamic btn = (JSObject) web.ExecuteJavascriptWithResult("document.getElementsByTagName('a')[0]");
btn.click();
You can use this for clicking such elements:
public void JsFireEvent(string getElementQuery, string eventName)
{
web.ExecuteJavascript(@"
function fireEvent(element,event) {
var evt = document.createEvent('HTMLEvents');
evt.initEvent(event, true, false ); // event type,bubbling,cancelable
element.dispatchEvent(evt);
}
" + String.Format("fireEvent({0}, '{1}');", getElementQuery, eventName));
}
Examples:
JsFireEvent("document.getElementsByTagName('a')[0]", "click");
JsFireEvent("document.getElementsByTagName('a')[0]", "mouseup");
Also you may find these two simple helper classes useful: https://gist.github.com/AlexP11223/8286153
The first one is extension methods for WebView/WebControl and the second one has some static methods to generate JS code for retrieving elements (JSObject) by XPath + getting coordinates of JSObject)
Upvotes: 2