Awesomium获取并单击“标签"属性 [英] Awesomium Getting and clicking a Tags attribute

查看:85
本文介绍了Awesomium获取并单击“标签"属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

按标签名称和属性单击按钮时遇到问题.我可以使用以下按钮类来单击它:

I'm having an issue clicking a button by the tag name and attribute. I can click it using the buttons class with the following:

public void Event(string getElementQuery, string eventName)
{
Control.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));
}

private void Reload_Tick(object sender, EventArgs e)
{
    Event("document.getElementsByClassName('Reload')[0]", "click");
}        

这很好,但是有时候我有一个按钮可以重新加载类.我想使用标记a和属性RELOAD更好地定义它,我尝试了以下方法,但是它什么也没做:

That's fine and all But sometimes I have more then one button with the class reload. I would like to define it better using the tag being a and the attribute being RELOAD I have tried it with the following but it does nothing at all:

Event("document.getElementsByTagName('a').getAttribute('RELOAD')[0]", "click");

这有效,但是单击了错误的按钮,这就是为什么我也需要该属性的原因.

This works but it clicks the wrong button, which is why i need the attribute also.

Event("document.getElementsByTagName('a')[0]", "click");

但是当我尝试获取属性时,什么也没有发生.有人可以帮我吗.

But when I try getting the attribute nothing happens. Could someone help me out please.

推荐答案

在此处回答

我已经尝试了以下方法,但是它什么也没做 document.getElementsByTagName('a').getAttribute('RELOAD')[0]

I have tried it with the following but it does nothing at all document.getElementsByTagName('a').getAttribute('RELOAD')[0]

您应该在浏览器(Chrome)控制台中检查类似的代码.它甚至无法运行.

You should check code like this in browser (Chrome) console. It would not even run.

使用XPath.

    /// <summary>
    /// Returns JavaScript XPath query string for getting a single element 
    /// </summary>
    /// <param name="xpath">Any valid XPath string, for example "//input[@id='myid']"</param>
    /// <returns>JS code to perform the XPath query (document.evaluate(...)</returns>
    public static string GetJsSingleXpathString(string xpath)
    {
        return
            String.Format(
                "document.evaluate(\"{0}\", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue", xpath);
    }

    private void Reload_Tick(object sender, EventArgs e)
    {
        string jsXpath = GetJsSingleXpathString("//a[contains(@class, 'Reload')]");

        Event(jsXpath, "click");
    }

这篇关于Awesomium获取并单击“标签"属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆