我有这个HTML文件,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>


在此文件的文件夹中,我将PHP CLI> 5.4作为服务器运行,如下所示:

php -S localhost:8080


...因此HTML页面可从http://localhost:8080/test_xpath.htm获得。

然后运行以下SlimerJS代码:

// 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();


如果我按原样运行JS代码-即waitForSelector部分被if(false)禁用-那么一切运行正常。

但是,如果通过将if(false)更改为if(true)来启用该部分,则会得到以下信息:

$ 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
....


我真的不明白这一点,因为http://docs.casperjs.org/en/latest/modules/casper.html#waitforselector说:


  等到与提供的选择器表达式匹配的元素存在于远程DOM中以处理任何下一步


...其中http://docs.casperjs.org/en/latest/selectors.html说:


  CasperJS大量使用选择器以使用DOM,并且可以透明地使用CSS3或XPath表达式。


因此,XPath应该没问题-而且,我在以下位置使用了EXACT SAME XAME:

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


...并且在那里工作,但是失败了:

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


为什么会发生-我哪里出问题了?如果可能,如何在waitForSelector中使用此XPath?

最佳答案

如果将字符串传递给waitForSelector()函数,它将被解释为CSS选择器。要使其与XPath表达式一起使用,请在object中明确指定选择器type

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


您也可以使用waitForText()(示例here)解决该问题。

09-11 09:03