如何单击PhantomJS中的元素?
page.evaluate(function() {
document.getElementById('idButtonSpan').click();
});
Run Code Online (Sandbox Code Playgroud)
这给了我一个错误"未定义不是一个函数......"
如果我改为
return document.getElementById('idButtonSpan');
Run Code Online (Sandbox Code Playgroud)
然后打印出来,
然后它打印[object object],因此元素确实存在.
该元素充当按钮,但它实际上只是一个span元素,而不是提交输入.
我能够点击这个按钮点击Casper,但Casper有其他限制,所以我回到了PhantomJS.
下一篇:如何在PhantomJS测试中ng单击A指令我知道我需要创建自己的函数来单击元素,但是如何使用它呢?
以下代码给我错误 TypeError: 'undefined' is not a function (evaluating 'click(obj)')
var page = require('webpage').create();
var url = 'http://somesite.com/document.html';
page.open(url, function(status) {
page.evaluate(function() {
var obj = document.querySelectorAll('button')[0];
click(obj);
});
console.log(page.content);
phantom.exit();
});
function click(el){
var ev = document.createEvent('MouseEvent');
ev.initMouseEvent(
'click',
true /* bubble */, true /* cancelable */,
window, null,
0, 0, 0, 0, /* coordinates */
false, false, false, false, /* modifier keys */
0 /*left*/, null
);
el.dispatchEvent(ev);
}
Run Code Online (Sandbox Code Playgroud)