我是新手使用Casperjs并且我试图让这个脚本复制一些文本,但即使我有正确的路径也无法找到搜索按钮。我之前已经能够单击脚本中的所有按钮,但它在行中显示错误:
casper.wait(2000, function () {
casper.click(x('//*[@id="SRCHBTN"]')); <----Error here
casper.capture('CurrentScreen.png');
});
CasperError: Cannot dispatch mousedown event on nonexistent selector:
以下是代码:
var casper = require('casper').create();
var x = require('casper').selectXPath;
casper.userAgent('Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)');
casper.start('url');
casper.then(function () {
this.sendKeys('user', 'user');
this.sendKeys('pswd', 'pass');
console.log('Entering Data');
});
casper.thenClick(x('//*[@id="login"]/table[1]/tbody/tr[3]/td[2]/input'), function () {
console.log('Logging in..');
});
casper.wait(2000, function () {
casper.click(x('//*[@id="SRCH_LINK"]/a'));
});
casper.wait(2000, function () {
casper.click(x('//*[@id="SRCHBTN"]'));
casper.capture('CurrentScreen.png');
});
casper.wait(4000, function () {
casper.click(x('//*[@id="TITLE_HL$0"]'));
casper.wait(2000, function() {
//this is pop up window section
casper.waitForPopup(/popup\.html$/, function () {
this.test.assertEquals(this.popups.length, 1);
});
// this will set the popup DOM as the main active one only for time the
// step closure being executed
casper.withPopup(/popup\.html$/, function () {
this.test.assertTitle('Job Details - Google Chrome');
var targetText = casper.fetchText('#DESCR');
console.log(targetText);
});
// next step will automatically revert the current page to the initial one
casper.then(function () {
this.test.assertTitle('Main page title');
});
});
});
casper.run();
答案 0 :(得分:4)
如果预期元素的存在依赖于异步调用,则等待2秒将不保证调用已完成。
您应该使用wait()
函数代替waitForSelector()
:
casper.waitForSelector(x('//*[@id="SRCHBTN"]'), function () {
casper.click(x('//*[@id="SRCHBTN"]'));
casper.capture('CurrentScreen.png'); });
});