简体   繁体   English

点击 <img role=“button” …> 通过casperjs

[英]Clicking <img role=“button” …> via casperjs

Trying to click the below element via casperjs, but it's not working for me. 尝试通过casperjs单击以下元素,但对我不起作用。

<img alt="Pay Button" class="v-button" role="button" src="https://test/wallet-services-web/xo/button.png" tabindex="0" style="cursor: pointer; transition-property: filter; transition-duration: 0.25s; filter: brightness(1);">

I am searching for this element through a bunch of nested iframes, and I can find it correctly -- but I can't seem to click on it correctly. 我正在通过一堆嵌套的iframe搜索该元素,但可以正确找到它-但似乎无法正确单击它。

    if (casper.exists('img.v-button')) {
        console.log("Found button"); // the exists works - this is logged

        casper.click('img.v-button'); // Approach 1: nothing happens

        // Approach 2: nothing happens
//      var x = require('casper').selectXPath;
//      casper.click(x('(//img[@class="v-button"])'));

        return true;
    } else {
        var result = traverseTreeDown();
        if (result) {
            return true;
        } else {
            casper.page.switchToParentFrame();
        }
    }                

Both approach 1 and 2 doesn't work to click - even though I do get inside the "exists" block correctly. 方法1和2都无法点击-即使我确实正确进入了“存在”块。

You should try the approach suggested on the casperjs documentation : 您应该尝试casperjs文档中建议的方法:

casper.then(function() {
    casper.click('img.v-button');
});

casper.then(function() {
    // do next action
});

I recommend checking out the API documentation . 我建议您查阅API文档

For instance, check this out: you can wait for a specific selector to be present. 例如,检查一下:您可以等待特定的选择器出现。

casper.start('https://yourmom.gov');

casper.waitForSelector('img.v-button', function() {
    this.click('img.v-button');
});

casper.run(); 

or there's an alternative way-- I think better 或有另一种方法-我认为更好

casper.start('http://yourmom.gov/').thenClick('img.v-button', function() {
    this.echo("clicktastic.");
});

casper.run();

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

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