codecowboy
codecowboy

Reputation: 10104

How can I retrieve the href element from a button in a casperJS script?

casper.then(function() {
this.test.assertExists({
    type: 'xpath',
    path: '//*[@id="wrapper"]/div[1]/a[2]'
}, "Found Multiviewer button");

});

The above button is found but I cannot access the href element by doing:

var element = x('//*[@id="wrapper"]/div[1]/a[2]');

console.log('element'+this.getElementAttribute(element,'href'));

results in:

TypeError: 'undefined' is not a function (evaluating 'this.getElementAttribute(element,'href')')

I have also tried:

this.getElementAttribute('//*[@id="wrapper"]/div[1]/a[2]','href')')

casperJS version is 1.1.0-beta3 (OSX 10.8.5)

Once I've retrieved the URL I then need to open it...

var baseTargetUrl;
casper.then(function() {
    var element = x('//*[@id="wrapper"]/div[1]/a[2]');
    console.log('url is: '+this.getElementAttribute(element,'href'));
    baseTargetUrl = this.getElementAttribute(element,'href');
});

casper.thenOpen(baseTargetUrl, function() {
    this.echo(this.getTitle());
});

Error:

FAIL TypeError: 'undefined' is not an object (evaluating 'url.toLowerCase')
#    type: uncaughtError
#    error: 'undefined' is not an object (evaluating 'url.toLowerCase')
#           TypeError: 'undefined' is not an object (evaluating 'url.toLowerCase')
#               at cleanUrl (/usr/local/lib/node_modules/casperjs/modules/utils.js:112)
#               at open (/usr/local/lib/node_modules/casperjs/modules/casper.js:1405)
#               at _step (/usr/local/lib/node_modules/casperjs/modules/casper.js:1832)
#               at runStep (/usr/local/lib/node_modules/casperjs/modules/casper.js:1553)
#               at checkStep (/usr/local/lib/node_modules/casperjs/modules/casper.js:399)
#    stack: not provided
TypeError: 'undefined' is not an object (evaluating 'url.toLowerCase')          
  /usr/local/lib/node_modules/casperjs/modules/utils.js:112 in cleanUrl
  /usr/local/lib/node_modules/casperjs/modules/casper.js:1405 in open
  /usr/local/lib/node_modules/casperjs/modules/casper.js:1832 in _step
  /usr/local/lib/node_modules/casperjs/modules/casper.js:1553 in runStep
  /usr/local/lib/node_modules/casperjs/modules/casper.js:399 in checkStep
FAIL TypeError: 'undefined' is not an object (evaluating 'url.toLowerCase')
#    type: error
#    subject: false
#    error: "TypeError: 'undefined' is not an object (evaluating 'url.toLowerCase')"
#    stack: in cleanUrl() in /usr/local/lib/node_modules/casperjs/modules/utils.js:112
in open() in /usr/local/lib/node_modules/casperjs/modules/casper.js:1405
in _step() in /usr/local/lib/node_modules/casperjs/modules/casper.js:1832
in runStep() in /usr/local/lib/node_modules/casperjs/modules/casper.js:1553
in checkStep() in /usr/local/lib/node_modules/casperjs/modules/casper.js:399

How should I pass the URL to the next function?

Upvotes: 0

Views: 1736

Answers (1)

hexid
hexid

Reputation: 3811

It looks like the issue is that you are trying to reference casper with this, but you aren't wrapping it in a casper function.

casper.then(function() {
    var element = x('//*[@id="wrapper"]/div[1]/a[2]');
    console.log('element'+this.getElementAttribute(element,'href'));
});

Upvotes: 1

Related Questions