casperjs / slimerjs waitForSelector中的无效选择器,它在getElementByXPath中有效吗?

时间:2016-06-29 13:37:21

标签: javascript xpath casperjs slimerjs

我有这个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&gt; 5.4作为这样的服务器:

php -S localhost:8080

...因此,http://localhost:8080/test_xpath.htm可以使用HTML页面。

然后我运行这个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应该没问题 - 还有什么,我在完全相同的XPATH 中使用

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

......并且它有效,但它失败了:

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

为什么会这样 - 我哪里出错了?如果可能,我如何在waitForSelector中使用此XPath?

1 个答案:

答案 0 :(得分:3)

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

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

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