MEAN Developer
MEAN Developer

Reputation: 277

CasperJS cannot find element even though I can CTRL-F it in Developer Tools

'use strict';

var casper = require('casper').create({verbose: true, logLevel: 'debug'});
var element = '/html/body/div[7]/nav/div[5]/div/div/div[2]/div[1]/form';

casper.start('http://store.nike.com/us/en_us/', function () {
    if(this.exists(element)) this.echo('Found it!');
});

casper.run(function() {
    this.exit();
});

Ok, so this code snippet is as simple as it can be. I spin up a new CasperJS instance, GET http://store.nike.com/us/en_us/, echo if an element is found, then exit. Now, what I cannot seem to grasp is why in the world the above code doesn't find that element (XPath) but if I load up Chrome Developer Tools, I can CTRL-F it. Same results when I substitute CSS Path for XPath. Can anyone please enlighten me? What am I missing?

Upvotes: 0

Views: 789

Answers (1)

Artjom B.
Artjom B.

Reputation: 61902

You need to use the XPath utility of CasperJS for that since element is an XPath expression and not a CSS selector.

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

casper.start('http://store.nike.com/us/en_us/', function () {
    if(this.exists(x(element))) this.echo('Found it!');
});

Upvotes: 2

Related Questions