javascript - 为什么Casperjs有时无法提交此表单?

标签 javascript casperjs

此代码打开 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')) 返回的内容:

enter image description here

不知道为什么有时没有设置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/

相关文章:

javascript - Bootstrap 4 记住选项卡

javascript - 从 RSS 提要中提取 XML 元素值

javascript - 如何使用 Angular Material 从图标创建下拉菜单?

javascript - 添加获取图片URL; JavaScript 变量内

javascript - 复选框作为 yajra 数据表中的标题

javascript - 无法使用 jQuery 发送帖子数据

google-chrome - 如何使用 Chrome 远程调试 CasperJS?

javascript - 使用CasperJS将法语字符写入CSV文件,但显示不正确

node.js - NodeJS 加载网站并在渲染之前从页面中删除元素

javascript - 使用 CasperJS 选择下拉选项