To click an element in PhantomJS, the standard .click() method is not supported. Instead, an event must be created and dispatched.
Consider the following code:
page.evaluate(function() { document.getElementById('idButtonSpan').click(); });
This code will result in an error because .click() is not a function. To resolve this, create an event and dispatch it:
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); }
Then, you can use this function to click an element:
page.evaluate(function() { click(document.getElementById('idButtonSpan')); });
The above is the detailed content of How to Simulate a Click in PhantomJS?. For more information, please follow other related articles on the PHP Chinese website!