Search code examples
javascriptxpathcasperjsslimerjs

casperjs/slimerjs invalid selector in waitForSelector, which works in getElementByXPath?


I have this HTML file, test_xpath.htm:

<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
</head>

<body>
  <h1>Hello World!</h1>
  <div>
    <a href="http://stackoverflow.com">Click me!</a>
  </div>
</body>
</html>

In the folder of this file, I run PHP CLI > 5.4 as a server like this:

php -S localhost:8080

... so the HTML page is available at http://localhost:8080/test_xpath.htm.

Then I run this SlimerJS code:

// run with:
// SLIMERJSLAUNCHER=/usr/bin/firefox46 /home/USERNAME/.nvm/versions/node/v4.0.0/lib/node_modules/casperjs/bin/casperjs --engine=slimerjs test_xpath.js

var casper = require('casper').create({
  verbose: true,
  logLevel: 'debug',
  userAgent: 'Mozilla/5.0 (X11; Linux i686; rv:43.0) Gecko/20100101 Firefox/43.0',
  viewportSize: {width: 1024, height: 768},
  pageSettings: {
    loadImages: false,//The script is much faster when this field is set to false
    loadPlugins: false,
  }
});

casper.on("url.changed", function(){
  this.then(function(){
    this.echo("URL changed " + this.getCurrentUrl()); //getTitle());
  });
});

casper.on('remote.message', function(message) {
  this.echo('remote message caught: ' + message);
});

casper.start().thenOpen("http://localhost:8080/test_xpath.htm", function() {
  console.log("website opened");
});

// set this to true to run - causes "[error] [remote] findAll(): invalid selector provided "//*[text()="Click me!"]":SyntaxError: An invalid or illegal string was specified"
if (false) {
  casper.waitForSelector('//*[text()="Click me!"]', function() {
    this.echo("I'm sure //*[text()='Click me!'] is available in the DOM");
  });
}

casper.then(function(){
  this.evaluate(function(){
    var aelem = __utils__.getElementByXPath('//*[text()="Click me!"]');
    aelem.click();
  });
});


casper.then(function(){
  casper.capture('TestCapture.png');
});

casper.run();

If I run the JS code as is - that is, the waitForSelector part is disabled by if(false) - then everything runs fine.

However, if I enable that part by changing the if(false) to if(true), I get this:

$ SLIMERJSLAUNCHER=/usr/bin/firefox46 /home/USERNAME/.nvm/versions/node/v4.0.0/lib/node_modules/casperjs/bin/casperjs --engine=slimerjs test_xpath.js
[info] [phantom] Starting... 
[info] [phantom] Running suite: 5 steps 
[debug] [phantom] opening url: http://localhost:8080/test_xpath.htm, HTTP GET 
[debug] [phantom] Navigation requested: url=http://localhost:8080/test_xpath.htm, type=Undefined, willNavigate=true, isMainFrame=true 
[debug] [phantom] url changed to "http://localhost:8080/test_xpath.htm" 
[debug] [phantom] Successfully injected Casper client-side utilities 
[info] [phantom] Step anonymous 2/6 http://localhost:8080/test_xpath.htm (HTTP 200) 
URL changed http://localhost:8080/test_xpath.htm 
[info] [phantom] Step anonymous 2/6: done in 235ms. 
[info] [phantom] Step anonymous 3/6 http://localhost:8080/test_xpath.htm (HTTP 200) 
website opened 
[info] [phantom] Step anonymous 3/6: done in 258ms. 
[info] [phantom] Step _step 4/6 http://localhost:8080/test_xpath.htm (HTTP 200) 
[info] [phantom] Step _step 4/6: done in 276ms. 
[error] [remote] findAll(): invalid selector provided "//*[text()="Click me!"]":SyntaxError: An invalid or illegal string was specified 
[error] [remote] findAll(): invalid selector provided "//*[text()="Click me!"]":SyntaxError: An invalid or illegal string was specified 
[error] [remote] findAll(): invalid selector provided "//*[text()="Click me!"]":SyntaxError: An invalid or illegal string was specified 
[error] [remote] findAll(): invalid selector provided "//*[text()="Click me!"]":SyntaxError: An invalid or illegal string was specified 
[error] [remote] findAll(): invalid selector provided "//*[text()="Click me!"]":SyntaxError: An invalid or illegal string was specified 
....

I truly don't understand this, because http://docs.casperjs.org/en/latest/modules/casper.html#waitforselector says:

Waits until an element matching the provided selector expression exists in remote DOM to process any next step

... where http://docs.casperjs.org/en/latest/selectors.html says:

CasperJS makes a heavy use of selectors in order to work with the DOM, and can transparently use either CSS3 or XPath expressions.

So, XPath should be OK - what's more, I'm using THE EXACT SAME XPATH in:

var aelem = __utils__.getElementByXPath('//*[text()="Click me!"]');

... and there it works, but it fails in:

casper.waitForSelector('//*[text()="Click me!"]', function() { ....

Why does this happen - where am I going wrong? And how can I use this XPath in waitForSelector, if it's possible?


Solution

  • If you pass a string into the waitForSelector() function, it would be interpreted as a CSS selector. To have it work with an XPath expression pass in an object explicitly specifying the selector type:

    selectorObject = {
      type: 'xpath',
      path: '//*[text()="Click me!"]'
    }
    casper.waitForSelector(selectorObject, function() {
      // ...
    });
    

    You can solve it with waitForText() as well, sample here.