【问题标题】:Casper.js Click on Button ID with Different NamesCasper.js 点击不同名称的按钮 ID
【发布时间】:2018-05-15 21:16:52
【问题描述】:

我正在尝试使用 casper.js 单击网页上的小项目符号(按钮)并制作屏幕截图。

<div> == $0
  <input type="image" name="ct100$body$uxDashboardList$ct100$dshButton" id="dshButton" title="Page 1" src="Images/bullet_green.png">
  <input type="image" name="ct101$body$uxDashboardList$ct100$dshButton" id="dshButton" title="Page 2" src="Images/bullet_green.png">
  <input type="image" name="ct102$body$uxDashboardList$ct100$dshButton" id="dshButton" title="Page 3" src="Images/bullet_green.png">
  <input type="image" name="ct103$body$uxDashboardList$ct100$dshButton" id="dshButton" title="Page 4" src="Images/bullet_green.png">
</div>

我可以使用以下方法在第一个着陆页上截屏:

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

casper.options.viewportSize = { width: 1920, height: 1080 };
casper.userAgent('Mozilla/5.0 (Windows NT 5.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36');
casper.start('https://websitenotdiscloseasitcontainsprivateinformationsandaspecialkey', function() {

    this.wait(10000, function() {
        this.click('ctl01$Body$uxDashboardList$ctl02$dshButton');
        casper.capture('page2.png');
   });
});

casper.run();

使用此代码 casper.js 不会截取任何屏幕截图(猜它没有找到按钮?)

找不到单击具有相同 id 但名称不同的按钮的方法。

【问题讨论】:

  • 我将 this.click 更改为 casper.click('input[type="submit"][name="ctl00$Body$uxDashboardList$ctl01$dshButton"]');但得到一个错误:CasperError: Cannot dispatch mousedown event on nonexistent selector: input[type="submit"][name="ctl01$Body$uxDashboardList$ctl00$dshButton"]
  • 更改了输入类型:'input[type="image"] 并截取了屏幕截图,但仅用于第一页
  • 不希望您遇到更多问题,但我建议您启用 casper.js 的调试。它可以为您提供一些很好的见解。

标签: javascript casperjs


【解决方案1】:

所以我在点击链接后并没有等待页面加载:

this.wait(5000, function() {
    this.click('input[type="image"][name="ctl00$Body$uxDashboardList$ctl02$dshButton"]');
   });
     this.wait(5000, function() {
         this.capture('test2.png');
  });

可能不优雅,但它有效

【讨论】:

  • 感谢分享。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-02
  • 1970-01-01
  • 1970-01-01
  • 2021-10-15
相关资源
最近更新 更多