我试着点击这个按钮:
<input class="right boutonActionTableau nouveau" onclick="addPeriode()" style="margin:0 0 10px 0 !important;" type="button" value="Période">
我的脚本:
var casper = require('casper').create({
verbose: true,
logLevel: 'debug',
pageSettings: {
loadImages: false,
loadPlugins: false
},
clientScripts: ["jquery.min.js"]
});
casper.start(url, function() {
this.userAgent('Mozilla/5.0 (Macintosh; Intel Mac OS X)');
this.fill('form[action="/Login/DoLogin"]', {
'Login': 'login',
'Password': 'password'
}, true);
this.click('form[action="/Login/DoLogin"]');
});
casper.thenOpen(url, function() {
this.echo('ok');
this.mouseEvent('click','input[class="right boutonActionTableau nouveau"]');
this.capture('test-screen1.png');
});
casper.then(function() {
this.capture('test-screen4.png');
});
casper.run();
我没有错误,但是当我截屏时,我的页面没有改变...... 在我的网络浏览器中,单击此按钮会在上一个表单之后添加一个表单(如果我单击 3 次,我还有 3 个其他表单)
如果在我的控制台 chrome 中输入:$(function(){addPeriode();});
没关系...
最佳答案
当您在 casper.evaluate()
中调用它时,您可以运行与 Chrome 控制台中相同的 JavaScript。 , 因为它是进入页面的沙盒窗口。
你可以调用
casper.evaluate(function(){
addPeriode();
});
或
casper.evaluate(function(){
var el = document.querySelector('input.right.boutonActionTableau.nouveau');
el.click();
// or
el.onclick();
});
但正常的点击应该是有效的。点击后可以尝试等待。
casper.thenOpen(url, function() {
this.echo('ok');
this.click('input.right.boutonActionTableau.nouveau');
this.capture('test-screen1.png');
}).wait(5000).then(function() {
this.capture('test-screen4.png');
});
关于javascript - 点击 CasperJS 并没有真正起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27801258/