Viv*_*ndi 5 javascript casperjs
我正在使用CasperJS来阅读某个网页.我想要做的是在CasperJS中加载一个网页.然后,等待某个HTML元素具有特定文本.
所以我喜欢这样做:
var casper = require('casper').create();
casper.start('http://www.example.com/somepage', function() {
this.echo('Home page opened');
});
// wait for text based on a CSS selector
casper.waitForText('.someCssClass', 'dolor sit', function() {
this.echo('found title!');
});
// when text is eventually found, then continue with this
casper.then(function() { ... } );
casper.run();
Run Code Online (Sandbox Code Playgroud)
所以我想使用waitForText,但使用CSS选择器.这样它就可以监视certaim HTML元素中的一段文本.这对我来说并不是很明显是否以及如何做到这一点.
这可以在CasperJS中完成吗?如果是这样,我该怎么做?
下面的函数需要从一个比特逻辑的waitForText()功能,并与成对它waitForSelector():
var utils = require("utils");
casper.waitForSelectorText = function(selector, text, then, onTimeout, timeout){
this.waitForSelector(selector, function _then(){
this.waitFor(function _check(){
var content = this.fetchText(selector);
if (utils.isRegExp(text)) {
return text.test(content);
}
return content.indexOf(text) !== -1;
}, then, onTimeout, timeout);
}, onTimeout, timeout);
return this;
};
Run Code Online (Sandbox Code Playgroud)
将此代码放在脚本的乞讨处,并像使用任何其他CasperJS函数一样使用该函数.text可以是字符串或正则表达式,选择器也可以是XPath表达式(使用辅助函数).
| 归档时间: |
|
| 查看次数: |
3553 次 |
| 最近记录: |