简体   繁体   English

如何从casperJS脚本中的按钮检索href元素?

[英]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: 找到以上按钮,但我无法通过执行以下操作访问href元素:

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) casperJS版本是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? 如何将URL传递给下一个函数?

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. 它看起来像问题是,你正在尝试引用卡斯珀与this ,但你是不是在卡斯帕尔函数进行包装。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM