为什么普通的JavaScript在CasperJS中不起作用? [英] Why doesn't plain JavaScript work in CasperJS?
本文介绍了为什么普通的JavaScript在CasperJS中不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是CasperJS的新手。 this.echo(this.getTitle());
如何工作,但 console.log( Page Title,document.title); 不是吗?另外,为什么我的document.querySelector无法正常工作?有人有很好的解释吗?我在CasperJS文档中的哪里找到答案?
I'm new to CasperJS. How come this.echo(this.getTitle());
works but console.log("Page Title ", document.title);
doesn't? Also why isn't my document.querySelector working? Does anyone have a good explanation? Where in the CasperJS documentation can I find the answer?
这是我的代码:
var casper = require('casper').create();
var url = 'http://www.example.com/';
casper.start(url, function() {
this.echo(this.getTitle()); // works
this.echo(this.getCurrentUrl()); // works
});
casper.then(function(){
this.echo(this.getCurrentUrl()); // works
console.log("this is URL: ", document.URL); // doesn't work
console.log("Page Title ", document.title); // doesn't work
var paragraph = document.querySelectorAll('p')[0].innerHTML;
console.log(paragraph); // doesn't work
});
casper.run();
编辑:
我正在使用casper.then casper.evaluate现在,它仍然无法正常工作。有什么想法吗?
I'm using casper.thenEvaluate and casper.evaluate now and it's still not working. Any ideas?
var casper = require('casper').create();
var url = 'http://www.example.com/';
casper.start(url, function() {
this.echo(this.getTitle()); // works
this.echo(this.getCurrentUrl()); // works
console.log('page loaded: '); // works
});
casper.thenEvaluate(function(){
var paragraph = document.querySelectorAll('p')[0].innerHTML; // doesn't work
console.log(paragraph); // doesn't work
console.log("Page Title ", document.title); // doesn't work
});
casper.run();
推荐答案
您必须调用依赖于<$ c的函数$ c>文档和 this.evaluate
:
var paragraph = this.evaluate(function() {
return document.querySelector('p').innerHtml;
});
如有疑问,请咨询文档。
这篇关于为什么普通的JavaScript在CasperJS中不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文