代码之家  ›  专栏  ›  技术社区  ›  wyc

为什么Casperjs不时未能提交此表单?

  •  0
  • wyc  · 技术社区  · 9 年前

    此代码打开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'

    也许壁虎的速度较慢,因此会产生某种错误/延迟?

    1 回复  |  直到 9 年前
        1
  •  1
  •   Amine Mohamed    9 年前

    我建议您使用以下代码:

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