从帖子:How to ng-click an A directive in a PhantomJS test我明白我需要创建自己的函数来点击元素,但是如何使用它?无法用Phantomjs点击元素
下面的代码给我错误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);
}
最好不要复制内容。如果解决方案为您工作,那么最好确认重复。 –