kevincm23
kevincm23

Reputation: 3

CasperJS Cannot get informations from xpath selector element not found

I am newbie here trying to get some help about casperJS using xpath selector.

I want to select the XPath from Google site and looking for the input box are to be exact, and here is the HTML :

<input id="gbqfq" class="gbqfif" type="text" value="" autocomplete="off" name="q" style="border: medium none; padding: 0px; margin: 0px; height: auto; width: 100%; background: url("data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw%3D%3D") repeat scroll 0% 0% transparent; position: absolute; z-index: 6; left: 0px; outline: medium none;" dir="ltr" spellcheck="false">
<div id="gs_sc0" class="gbqfif" style="background: none repeat scroll 0% 0% transparent; color: transparent; padding: 0px; position: absolute; z-index: 2; white-space: pre; visibility: hidden;"></div>

<input id="gs_taif0" class="gbqfif" disabled="" autocomplete="off" aria-hidden="true" style="border: medium none; padding: 0px; margin: 0px; height: auto; width: 100%; position: absolute; z-index: 1; background-color: transparent; color: silver; left: 0px;" dir="ltr">
<input id="gs_htif0" class="gbqfif" disabled="" autocomplete="off" aria-hidden="true" style="border: medium none; padding: 0px; margin: 0px; height: auto; width: 100%; position: absolute; z-index: 1; background-color: transparent; color: silver; transition: all 0.218s ease 0s; opacity: 0; left: 0px; text-align: left;" dir="ltr">

my simple code here doesn't work :

var casper = require('casper').create();

var x = require('casper').selectXPath;

casper.userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.72 Safari/537.36");

casper.start('www.google.com');

casper.wait(3000,function(){
    this.echo(this.getTitle());

});

casper.then(function(){
    casper.sendKeys(x('//*[@id="gbqfq"]'),"newegg.com" );
});

casper.then(function(){
   casper.click(x('//*[@id="gbqfq"]'));
});

casper.run();

when i run the script using : casperjs search.js

the result is ended up to be like this :

E:\Projects\casperjs\mytestcase>casperjs search.js

CasperError: Cannot get informations from xpath selector: //*[@id="gbqfq"]: elem
ent not found.
  E:/projects/casperjs/modules/casper.js:1058 in getElementInfo
  E:/projects/casperjs/modules/casper.js:1589
  E:/Projects/casperjs/mytestcase/search.js:21
  E:/projects/casperjs/modules/casper.js:1553 in runStep
  E:/projects/casperjs/modules/casper.js:399 in checkStep

I am always stuck at getting the element from xpath there, I am using casperJS v1.1.0-beta3 and latest phantomJS version.

any help would be appreciated!

Upvotes: 0

Views: 1886

Answers (1)

Jens Erat
Jens Erat

Reputation: 38682

The problem is your URL, which does not point to a web resource, but a local file. By dumping the status after loading the web page, you'll easily realize that:

casper.start('www.google.com', function() {
    this.echo(this.status(true));
});

The output contains – among others – this line (I executed the script from /tmp):

"requestUrl": "file:///tmp/www.google.com",

To fix the problem, add http:// (or https:// if you prefer encryption):

casper.start('http://www.google.com');

Upvotes: 1

Related Questions