Awesomium Getting and clicking a Tags attribute

邮差的信 提交于 2019-12-13 04:44:09

问题


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");
}        

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.


回答1:


Answered here http://answers.awesomium.com/questions/5826/get-attribute-of-tag-and-invoke-click.html

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

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

Use 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");
    }


来源:https://stackoverflow.com/questions/26758565/awesomium-getting-and-clicking-a-tags-attribute

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!