Reputation: 5008
I am using casperjs and following is the code
var casper = require('casper').create();
var x = require('casper').selectXPath;
casper.start('http://google.co.in/', function() {
// search for 'casperjs' from google form
this.test.assertExists(x('//*[@type="text"]'), 'the element exists');
});
casper.run(function() {
// echo results in some pretty fashion
this.echo('').exit();
});
it wasn't able to find any element with attribute type as text though there are plenty.
This is the output I get
FAIL the element exists
# type: assertExists
# subject: false
# selector: {"type":"xpath","path":"//*[@type=\"text\"]"}
Upvotes: 2
Views: 2551
Reputation: 3811
Google has been known to not be bot-friendly. In order for this to work, you must set the UserAgent.
This was tested under CasperJS-1.0.0 and PhantomJS-1.8.0
var casper = require('casper').create({
pageSettings: {
userAgent: 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.21 (KHTML, like Gecko) Chrome/25.0.1349.2 Safari/537.21'
}
});
var x = require('casper').selectXPath;
casper.start('http://google.co.in/', function() {
// search for 'casperjs' from google form
this.test.assertExists(x('//*[@type="text"]'), 'the element exists');
this.test.assertExists({
type: 'xpath',
path: '//*[@type="text"]'
}, 'the element exists');
});
casper.run(function() {
// echo results in some pretty fashion
this.echo('').exit();
});
You can visit http://whatsmyuseragent.com/ to see what your current UserAgent is.
Update: Removed CasperJS-1.0.0 code in favor of backwards-compatible code.
Upvotes: 4
Reputation: 35829
Similar answer as hexid, but to set the user agent, you need to start casper first (not the other way around):
var casper = require('casper').create({verbose: true});
var x = require('casper').selectXPath;
casper.start();
casper.userAgent('Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.21 (KHTML, like Gecko) Chrome/25.0.1349.2 Safari/537.21');
casper.thenOpen('http://google.co.in/', function() {
// search for 'casperjs' from google form
this.test.assertExists(x('//input[@type="text"]'), 'the element exists');
});
casper.run(function() {
// echo results in some pretty fashion
this.echo('').exit();
});
Tested with PhantomJs 1.7.0 and Casper 1.0.0-RC4
Upvotes: 2