我有一个网页与该行之间以检索CasperJS元素的属性:如何使用XPath表达式
<a href="http://foo.com/home.do?SID=3443132">...
我需要使用XPath提取“href”属性。在CasperJS的API中写下了这个信息:clientutils.getElementByXPath。
这里是我的代码:
phantom.casperPath = '..n1k0-casperjs-5428865';
phantom.injectJs(phantom.casperPath + '\\bin\\bootstrap.js');
var casper = require('casper').create();
var url = "...";
casper.start(url, function() {
casper.echo("started");
});
var x = require('casper').selectXPath;
casper.then(function()
{
casper.echo("getsid");
this.test.assertExists(x('//a[contains(@href, "home.do?SID=")]'), 'the element exists');
var element = __utils__.getElementByXPath('//a[contains(@href, "home.do?SID=")]');
});
但它失败。它返回:
false
undefined
started
getsid
PASS the element exists <== XPATH WORKS
FAIL ReferenceError: Can't find variable: __utils__
# type: uncaughtError
# error: "ReferenceError: Can't find variable: __utils__"
ReferenceError: Can't find variable: __utils__
您不能在casperjs环境中直接使用__utils__。您必须使用[Casper.evaluate()](http://casperjs.org/api.html#casper.evaluate)方法。 – NiKo 2012-07-31 22:00:19
@videador你能指出你如何设法解决这个问题吗? – codecowboy 2014-01-04 17:40:06