此代码打开 Google,搜索 casperjs
并输出页面标题:
var results = []
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.on("page.error", function(err, trace) {
this.die("Page Error: " + err, "ERROR")
})
casper.on('complete.error', function(err) {
this.die("Complete Error: " + err)
})
casper.start('http://google.co.uk/', function() {
this.evaluate(function() {
document.querySelector('input[name="q"]').value = "casperjs"
document.querySelector('input[name="btnK"]').click()
})
})
casper.then(function() {
//this.echo(this.getHTML('form[action="/search"]'))
results = this.evaluate(function() {
return document.title
})
})
casper.run(function() {
this.echo(results).exit()
})
大多数时候它提交表单:
[info] [phantom] Starting...
[info] [phantom] Running suite: 3 steps
[debug] [phantom] opening url: http://google.co.uk/, HTTP GET
[debug] [phantom] Navigation requested: url=http://google.co.uk/, type=Other, willNavigate=true, isMainFrame=true
[debug] [phantom] Navigation requested: url=http://www.google.co.uk/, type=Other, willNavigate=true, isMainFrame=true
[debug] [phantom] Navigation requested: url=https://www.google.co.uk/?gws_rd=ssl, type=Other, willNavigate=true, isMainFrame=true
[debug] [phantom] url changed to "https://www.google.co.uk/?gws_rd=ssl"
[debug] [phantom] Successfully injected Casper client-side utilities
[info] [phantom] Step anonymous 2/3 https://www.google.co.uk/?gws_rd=ssl (HTTP 200)
[info] [phantom] Step anonymous 2/3: done in 1139ms.
[debug] [phantom] Navigation requested: url=https://www.google.co.uk/search?sclient=psy-ab&site=&source=hp&q=casperjs&btnK=Google+Search, type=FormSubmitted, willNavigate=true, isMainFrame=true
[debug] [phantom] url changed to "https://www.google.co.uk/search?sclient=psy-ab&site=&source=hp&q=casperjs&btnK=Google+Search"
[debug] [phantom] Successfully injected Casper client-side utilities
[info] [phantom] Step anonymous 3/3 https://www.google.co.uk/search?sclient=psy-ab&site=&source=hp&q=casperjs&btnK=Google+Search (HTTP 200)
[info] [phantom] Step anonymous 3/3: done in 3605ms.
[info] [phantom] Done 3 steps in 3641ms
casperjs - Google Search
但有时会失败:
[info] [phantom] Starting...
[info] [phantom] Running suite: 3 steps
[debug] [phantom] opening url: http://google.co.uk/, HTTP GET
[debug] [phantom] Navigation requested: url=http://google.co.uk/, type=Other, willNavigate=true, isMainFrame=true
[debug] [phantom] Navigation requested: url=http://www.google.co.uk/, type=Other, willNavigate=true, isMainFrame=true
[debug] [phantom] Navigation requested: url=https://www.google.co.uk/?gws_rd=ssl, type=Other, willNavigate=true, isMainFrame=true
[debug] [phantom] url changed to "https://www.google.co.uk/?gws_rd=ssl"
[debug] [phantom] Successfully injected Casper client-side utilities
[info] [phantom] Step anonymous 2/3 https://www.google.co.uk/?gws_rd=ssl (HTTP 200)
[info] [phantom] Step anonymous 2/3: done in 2003ms.
[info] [phantom] Step anonymous 3/3 https://www.google.co.uk/?gws_rd=ssl (HTTP 200)
[info] [phantom] Step anonymous 3/3: done in 2014ms.
[info] [phantom] Done 3 steps in 2033ms
Google
为什么会发生这种情况?正常吗?
编辑:
这就是 this.echo(this.getHTML('#gs_lc0'))
返回的内容:
不知道为什么有时没有设置value
。
编辑2:
如果我只写:userAgent: 'Chrome/22.0.1229.94 Safari/537.4'
也许 Gecko 方式速度较慢,因此会产生某种错误/延迟?
最佳答案
我建议您使用此代码:
var results = [];
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(){});
casper.then(function(){
casper.click('input[name="q"]');
casper.sendKeys('input[name="q"]', 'casperjs');
});
casper.then(function(){
casper.click('input[name="btnK"]');
});
casper.wait(5000, function() {
//this.echo(this.getHTML('form[action="/search"]'))
results = casper.evaluate(function() {
return document.title;
});
});
casper.run(function() {
this.echo(results).exit();
});
关于javascript - 为什么Casperjs有时无法提交此表单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34453254/