javascript 如何使用 XPath 表达式在 CasperJS 中检索元素的属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11377487/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to retrieve attribute of element in CasperJS using an XPath expression
提问by dlopezgonzalez
I have a webpage with this between lines:
我有一个网页,两行之间:
<a href="http://foo.com/home.do?SID=3443132">...
I need to extract "href" attribute using XPath. In the API of CasperJS is wrote this information about this: clientutils.getElementByXPath.
我需要使用 XPath 提取“href”属性。在 CasperJS 的 API 中写了这样的信息:clientutils.getElementByXPath。
Here is my code:
这是我的代码:
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=")]');
});
But it fails. it returns this:
但它失败了。它返回这个:
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__
回答by John
Try this:
试试这个:
phantom.casperPath = '..n1k0-casperjs-5428865';
phantom.injectJs(phantom.casperPath + '\bin\bootstrap.js');
var url = "...";
var casper = require('casper').create();
var x = require('casper').selectXPath;
casper.start(url, function() {
casper.echo("started");
});
casper.then(function() {
casper.echo("getsid");
var xpath = '//a[contains(@href, "home.do?SID=")]';
var xpath_arr = { type: 'xpath', path: xpath};
this.test.assertExists(xpath_arr, 'the element exists');
var element = x(xpath);
});
回答by Artjom B.
As pointed out in the comments, you would have to use __utils__
inside the evaluate
callback, because it is injected into the page. Since you want(ed) the href
, you could use:
正如评论中所指出的,您必须__utils__
在evaluate
回调中使用,因为它被注入到页面中。由于您想要(编辑)href
,您可以使用:
casper.then(function(){
casper.echo("getsid");
this.test.assertExists(x('//a[contains(@href, "home.do?SID=")]'), 'the element exists');
var href = this.evaluate(function(){
var element = __utils__.getElementByXPath('//a[contains(@href, "home.do?SID=")]');
return element.href;
});
});
This can be shortened with the usage of casper.getElementAttribute
:
这可以通过使用来缩短casper.getElementAttribute
:
casper.then(function(){
casper.echo("getsid");
this.test.assertExists(x('//a[contains(@href, "home.do?SID=")]'), 'the element exists');
var href = this.getElementAttribute(x('//a[contains(@href, "home.do?SID=")]'), "href");
});
You can also use casper.getElementInfo
to get the complete info of the element including all the attributes (but only some properties).
您还可以使用casper.getElementInfo
获取元素的完整信息,包括所有属性(但只有一些属性)。