【问题标题】:Casperjs checking if xpath exists or notCasperjs 检查 xpath 是否存在
【发布时间】:2014-01-27 18:00:19
【问题描述】:

好的,有人可以帮助我吗?我是 casperjs 的新手,到目前为止,stackoverflow 非常棒,这里的人非常乐于助人。

在最终让我的脚本正常工作并使其回显 html 之后,我想添加一个新函数并需要有关如何执行此操作的帮助。

基本上不是将 html 回显出来。

我想检查一个 xpath 是否存在,如果它确实我希望它回显

you still have minutes 

如果不存在则回显

no minutes left 

这是我要检查的 xpath

 /html/body/div/div[3]/div[2]/div/div/table/tbody/tr[2]/td[2]

谁能告诉我一个简单的函数可以做到这一点

我的功能

// Wait 2 sec then write to txt file
casper.wait(9000, function() {
//this.echo(this.getHTML());
//this.echo(this.getCurrentUrl());
this.fill('form[name="LoginForm"]', {
'username': 'test',
'password': 'test'
}, true);
});

【问题讨论】:

    标签: javascript xpath phantomjs casperjs


    【解决方案1】:

    好吧,所以我恢复为 css 选择器,而不是 xpath,这就是我如何解决我的问题

     if (this.exists('div.box_pink div.inner p')) {
     this.echo('the css selector exists'); } else { 
     this.echo('the css selector dont exist'); 
     } 
     });
    

    【讨论】:

      【解决方案2】:

      这样的?我根据您提供的代码进行了修改。

      casper.start('http://yoursite.tld/', function() {
          // Wait 2 sec then write to txt file
          this.wait(9000, function() {
              //this.echo(this.getHTML());
              //this.echo(this.getCurrentUrl());
              this.fill('form[name="LoginForm"]', {
                  'username': 'test',
                  'password': 'test'
              }, true);
      
              if(this.exists('/html/body/div/div[3]/div[2]/div/div/table/tbody/tr[2]/td[2]')) {
                  console.log('you still have minutes');
              }
              else {
                  console.log('no minutes left');
              }
          });
      
      });
      
      casper.run();
      

      【讨论】:

      • 嘿,谢谢你的回答,但我真的很难让它在我的脚本中工作,如果你有机会将它添加到我的填写表单示例中,我将把它放在我上面的帖子中
      • jason 查看我帖子中的功能再次感谢
      猜你喜欢
      • 2018-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-02
      相关资源
      最近更新 更多