CasperJS equivalent to RSelenium for filling a form

193 views Asked by At

I have an Rselenium script to fill in a form, but am trying to use CasperJS as I am finding Rselenium too slow. The following code will navigate the form as I expect.

remote.driver$navigate("http://news.ceek.jp/search.cgi?kind=sports")
search.form <- remote.driver$findElement(using = "xpath", "//*[@id='query']")
search.form$sendKeysToElement(list("SearchTerm",key = "enter")) 

The equivalent CasperJS code I have tried is the following;

var casper = require("casper").create();
casper.start("http://news.ceek.jp/search.cgi?kind=sports", function() {
    this.test.assertExists({
        type: 'xpath',
        path: '//*[@id="query"]'
    }, 'the element exists');
});

casper.then(function() {
    this.fill('input[name="q"]', {q:'SearchTerm'}, true);
});

Output from casper;

PASS the element exists
CasperError: Errors encountered while filling form: no field named "q" in form

RSelenium has the advantage that the form parameter does not need to be specified, but presumably casperJS requires this. What should i be using instead?

I am inspecting the elements, but having trouble identifying the form parameter in a such a case. How does one go about this in general?

1

There are 1 answers

0
Artjom B. On BEST ANSWER

The first argument to casper.fill() is the selector of the form. If you don't have a form surrounding the input element, then you cannot use the casper.fill* functions.

The workaround would be to use

casper.then(function() {
    this.sendKeys('input[name="q"]', 'SearchTerm', {keepFocus: true})
    this.page.sendEvent("keypress", this.page.events.Enter);
});

Also, you should use the XPath helper:

var x = require('casper').selectXPath;
...
this.test.assertExists(x('//*[@id="query"]'), 'the element exists');