首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PhantomJS/CasperJS AssertExists()正在失败

PhantomJS/CasperJS AssertExists()正在失败
EN

Stack Overflow用户
提问于 2014-05-12 13:14:32
回答 1查看 1.5K关注 0票数 3

我正在试着检查我的网页中是否存在一个选择器,但是casperjs一直没有找到它。

我试过两种方法:

1.无需等待

代码语言:javascript
复制
casper.then(function() {
    // search for 'casperjs' from google form
    this.test.assertExists('#search-form', 'the element exists');
    // this.test.assertExists('.glyphicon.glyphicon-plus', 'the element exists');
});

2.等待选择器

代码语言:javascript
复制
casper.waitForSelector('#search-form', function() {
    this.echo("I'm sure #search-form is available in the DOM");
});

3.另一种等待方法

代码语言:javascript
复制
casper.waitFor(function check() {
     return this.evaluate(function() {
         return $('#search-form').length == 1;
     });
 }, function then() {    // step to execute when check() is ok
     test.assertExists('#search-form', 'tabs area is found');
 }, function timeout() { // step to execute if check has failed
     this.echo("Timeout: page did not load in time...").exit();
 });

到目前为止,他们中没有人能找到选择者。并且这个选择器是从html加载的,它不是由javascript插入的。

有什么想法吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-05-12 14:46:45

好吧,我想你应该检查一下你的选择器是否在你的网页上:

代码语言:javascript
复制
casper.then(function(){
    console.log(this.getPageContent());
});

命令-pipe输出-:casperjs test yourFile.js > seePageContent.html

或者您可能更喜欢这种方式(如果需要,可以等待):

代码语言:javascript
复制
casper.then(function(){
    this.wait(3000,function(){
        var fs = require('fs');
        fs.write("seePageContent.html", this.getPageContent(), 'w');
    });
});

这样你就可以知道你的元素是否存在。如果没有,那么您的前一步是错误的。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23610083

复制
相关文章

相似问题

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