最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to retrieve attribute of element in CasperJS using an XPath expression - Stack Overflow

programmeradmin3浏览0评论

I have a webpage with this between lines:

<a href=".do?SID=3443132">...

I need to extract "href" attribute using XPath. In the API of CasperJS is wrote this information about this: 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__

I have a webpage with this between lines:

<a href="http://foo./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.

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__
Share Improve this question edited Oct 6, 2015 at 13:39 Artjom B. 62k26 gold badges135 silver badges230 bronze badges asked Jul 7, 2012 at 18:13 dlopezgonzalezdlopezgonzalez 4,2955 gold badges33 silver badges43 bronze badges 2
  • You can't use __utils__ directly within the casperjs environment. You have to use the Casper.evaluate() method. – NiKo Commented Jul 31, 2012 at 22:00
  • @videador Can you please indicate how you managed to solve this? – codecowboy Commented Jan 4, 2014 at 17:40
Add a ment  | 

2 Answers 2

Reset to default 3

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);    
});

As pointed out in the ments, 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:

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.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 plete info of the element including all the attributes (but only some properties).

发布评论

评论列表(0)

  1. 暂无评论