alexchenco alexchenco - 4 years ago 288
Javascript Question

Why is Casperjs fails to submit this form from time to time?

This code opens Google, searches for

casperjs
and outputs the title of the page:

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


It submits the form most of the time:

[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


But it fails at time:

[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


Why does this happen? Is it normal?

EDIT:

This is what
this.echo(this.getHTML('#gs_lc0'))
returns:

enter image description here

No idea why
value
isn't being set at times.

EDIT 2:

The problem disappears if I only write:
userAgent: 'Chrome/22.0.1229.94 Safari/537.4'


Maybe the Gecko way is slower, hence producing some kind of bug/delay?

Answer Source

I suggest you to use this code:

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();
   });
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download