【问题标题】:Casperjs click button doesn't do anythingCasperjs点击按钮不做任何事情
【发布时间】:2017-07-24 10:19:20
【问题描述】:

所以我有一个包含段落和按钮的 html 页面。单击该按钮时,该段落将隐藏。我正在尝试在 Casperjs 中自动执行此操作。到目前为止,我加载页面,截屏,然后单击按钮并截屏。不过截图是一样的

var casper = require('casper').create();

casper.start('http://localhost:3000/example.html', function() {
    this.echo("Loaded successfully.");
    casper.capture("screenshot1.png");
});

casper.then(function() {
    this.evaluate(function() {
        this.click('//*[@id="hide"]')
    });
});

casper.then(function(){
    casper.capture("screenshot2.png");
});

casper.run();

有什么想法吗?

【问题讨论】:

    标签: node.js phantomjs casperjs


    【解决方案1】:

    您不能在 evaluate() 中使用 this.click(),因为 evaluate() 中的代码会像使用浏览器控制台一样执行代码。您可以使用 javascript 获取元素并使用它的 click( ) 事件,或者你可以直接使用 this.click()。无论如何,不​​要在 evaluate() 中使用 this.click()。

    【讨论】:

      【解决方案2】:

      如果您描述问题时按钮 id='hide',这可能是代码:

      var casper = require('casper').create();
      
      casper.start('http://localhost:3000/example.html', function() {
          this.echo("Loaded successfully.");
          casper.capture("screenshot1.png");
      
          casper.click('#hide'); // Clicking button with Id='hide'
      
          casper.capture("screenshot2.png"); // capture after clicking button
      });
      
      // Execute the whole process
      casper.run();
      

      希望这对您有所帮助!发送

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-07-04
        • 2017-12-31
        • 2014-10-07
        • 1970-01-01
        • 2016-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多