为什么Casperjs不时不提交此表格?

时间:2015-12-24 13:03:59

标签: javascript casperjs

此代码打开Goog​​le,搜索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'))返回的内容:

enter image description here

不知道为什么value有时没有设置。

编辑2:

如果我只写userAgent: 'Chrome/22.0.1229.94 Safari/537.4'

,问题就会消失

也许Gecko方式较慢,因此产生某种错误/延迟?

1 个答案:

答案 0 :(得分:1)

我建议你使用这段代码:

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();
   });