我想要以下Casperjs脚本打开Google,搜索一个术语,并记录页面标题:
var title = ""
var casper = require('casper').create({
verbose: true,
logLevel: 'debug',
pageSettings: {
loadImages: false, // The WebPage instance used by Casper will
loadPlugins: false // use these settings
//userAgent: 'Mozilla/5.0 (Macintosh Intel Mac OS X 10_7_5) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.94 Safari/537.4'
}
})
casper.start('http://google.co.uk/', function() {
// search for 'casperjs' from google form
this.fill('form[action="/search"]', {
q: 'casperjs'
}, true)
})
casper.thenEvaluate(function() {
// aggregate results for the 'casperjs' search
title = document.title
})
casper.log(title, 'warning')
casper.run()
但不输出任何内容:
[警告] [幻影]
我做错了什么?
答案 0 :(得分:2)
您的代码存在两个问题。
这应该有效:
casper.then(function() {
var title = this.evaluate(function() {
return document.title
})
this.log(title, 'warning')
})
甚至是这样:
casper.then(function() {
var title = this.getTitle()
this.log(title, 'warning')
})