1

I'm using CasperJS (with the slimerjs engine) to fill a form. This is however not working.

I tried it with a simple google.com script:

var casper = require("casper").create({
    verbose: true,
    logLevel: "debug"
});

var fs = require("fs");

phantom.cookiesEnabled = true;

casper.options.viewportSize = { width: 1024, height: 768 };

casper.start();

casper.userAgent("Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");

casper.thenOpen("http://www.google.com", function(response) {
    casper.fill("form[id='gbqf']", {
        q: "test"
    }, true);
}).run();

The form is not filled, nothing happens after the page is loaded.. The verbose log states:

[info] [remote] attempting to fetch form element from selector: 'form[id='gbqf']'
[info] [phantom] Done 2 steps in 1086ms

(and then ends)

What am I doing wrong? The form ID is 100% correct..

Artjom B.
  • 61,146
  • 24
  • 125
  • 222
  • would it make any difference if you use `#gbqf` selector instead? thanks. – alecxe Feb 10 '15 at 19:37
  • @alecxe: the difference is now that it its stuck at the first [info] remote attempting to fetch.. and never reaches the second "done 2 steps" but otherwise this also fails –  Feb 10 '15 at 19:39

1 Answers1

2

Google serves different pages based on user agent, viewport size, cookies and other metrics. You have to make sure that the element is actually there. You can check it with the casper.exists() function.

I found a good selector for google search is "form[action='/search']" regardless of engine (PhantomJS or SlimerJS) and user agent string (with or without declaration as IE).

Artjom B.
  • 61,146
  • 24
  • 125
  • 222