Javascript 如何从casperJS脚本中的按钮检索href元素?
})) 找到上面的按钮,但我无法通过执行以下操作访问href元素:Javascript 如何从casperJS脚本中的按钮检索href元素?,javascript,casperjs,Javascript,Casperjs,})) 找到上面的按钮,但我无法通过执行以下操作访问href元素: casper.then(function() { this.test.assertExists({ type: 'xpath', path: '//*[@id="wrapper"]/div[1]/a[2]' }, "Found Multiviewer button"); 结果: var element = x('//*[@id="wrapper"]/div[1]/a[2]'); console.log('el
casper.then(function() {
this.test.assertExists({
type: 'xpath',
path: '//*[@id="wrapper"]/div[1]/a[2]'
}, "Found Multiviewer button");
结果:
var element = x('//*[@id="wrapper"]/div[1]/a[2]');
console.log('element'+this.getElementAttribute(element,'href'));
我也尝试过:
TypeError: 'undefined' is not a function (evaluating 'this.getElementAttribute(element,'href')')
casperJS版本是1.1.0-beta3(OSX 10.8.5)
一旦检索到URL,我就需要打开它
this.getElementAttribute('//*[@id="wrapper"]/div[1]/a[2]','href')')
错误:
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());
});
我应该如何将URL传递给下一个函数?问题似乎是您试图使用
this
引用casper,但您没有将其包装到casper函数中
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
谢谢上面的代码片段成功了(第二个没有)。好的,我将把它去掉。谢谢。然后我还需要将URL传递给casper.thenOpen()。正确的方法是什么?请删除问题的第二部分,因为你已经有了一个明确的答案。您还可以修复第一个代码段的缩进。
casper.then(function() {
var element = x('//*[@id="wrapper"]/div[1]/a[2]');
console.log('element'+this.getElementAttribute(element,'href'));
});