简体   繁体   English

Awesomium点击标签C#

[英]Awesomium click by tag C#

<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 我正在尝试让Awesomium单击某个按钮,以上面的示例为例,没有ID只是一个标签

<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? 有没有一种方法可以通过使用Awesomium中的“回家”属性“ a”来单击此按钮? 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");
        }
    }    

If this element is not a <button> it probably doesn't have the click() method. 如果此元素不是<button>则可能没有click()方法。

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 另外,您可能会发现这两个简单的帮助程序类很有用: 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) 第一个是WebView / WebControl的扩展方法 ,第二个是一些静态方法来生成JS代码,以通过XPath检索元素(JSObject)+获取JSObject的坐标)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM