2014-05-12 45 views
3

我想检查选择器是否存在于我的网页中,但casperjs从来没有找到它。PhantomJS/CasperJS AssertExists()失败

我已经试过两种方法:

1.无需等待

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.等待选择

casper.waitForSelector('#search-form', function() { 
    this.echo("I'm sure #search-form is available in the DOM"); 
}); 

3.另一个等待的做法

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插入的。

任何想法?

回答

3

嗯,我想你应该检查,如果你的选择是在您的网页这样的:

casper.then(function(){ 
    console.log(this.getPageContent()); 
}); 

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

或者你可能更喜欢这种方式(与等待如果需要的话):

casper.then(function(){ 
    this.wait(3000,function(){ 
     var fs = require('fs'); 
     fs.write("seePageContent.html", this.getPageContent(), 'w'); 
    }); 
}); 

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

+1

+1有人可能认为这可能已过时,但有时一个页面将具有不同的DOM元素,具体取决于userAgent或在浏览器或phantomjs中分解。 –

+0

你说得对,我有一个问题,默认情况下,CasperJs用Linux **推出了移动版本的网站**。 userAgent对设备奇怪地未知,导致casper切换到移动版本。我注意到它只是通过检查DOM(并用'this.capture()'确认它)。 Marcelosalloum,你可以等待一个元素出现,如果它不存在,它将无法工作。最好知道什么是现在,而不是什么不是。 – Fanch