2

我有这个 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)但是,如果我通过更改to 来启用该部分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() { ....

为什么会发生这种情况 - 我哪里出错了?如果可能的话,我如何在 中使用这个 XPath waitForSelector

4

1 回答 1

3

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

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

你也可以解决它,在这里waitForText()采样。

于 2016-06-29T13:41:15.927 回答