首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Casperjs检查xpath是否存在

Casperjs检查xpath是否存在
EN

Stack Overflow用户
提问于 2014-01-27 18:00:19
回答 2查看 3.3K关注 0票数 0

好的,有人能帮我吗?我是刚接触过casperjs的人,到目前为止,堆积如山真是太棒了,这里的人都很有帮助。

在最终使我的脚本工作并使它回显html之后,我想添加一个新的函数,并需要关于如何实现它的帮助。

基本上,而不是回显出html。

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

代码语言:javascript
复制
you still have minutes 

如果它不存在回声

代码语言:javascript
复制
no minutes left 

这里是我想检查的xpath

代码语言:javascript
复制
 /html/body/div/div[3]/div[2]/div/div/table/tbody/tr[2]/td[2]

有人能给我看一个简单的函数吗?

我的职能

代码语言:javascript
复制
// 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);
});
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-01-28 10:14:25

好的,我不再使用xpath,而是返回到css选择器,这里介绍了我是如何解决问题的

代码语言:javascript
复制
 if (this.exists('div.box_pink div.inner p')) {
 this.echo('the css selector exists'); } else { 
 this.echo('the css selector dont exist'); 
 } 
 });
票数 0
EN

Stack Overflow用户

发布于 2014-01-27 18:04:30

像这样吗?我根据你提供的代码做了修改。

代码语言:javascript
复制
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();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21388206

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档