Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
167
rated 0 times [  171] [ 4]  / answers: 1 / hits: 86114  / 11 Years ago, mon, april 1, 2013, 12:00:00

How do I click an element in PhantomJS?



page.evaluate(function() {
document.getElementById('idButtonSpan').click();
});


This gives me an error undefined is not a function...



If I instead



 return document.getElementById('idButtonSpan');


and then print it,



then it prints [object object], so the element does exist.



The element acts as a button, but it's actually just a span element, not a submit input.



I was able to get this button click to work with Casper, but Casper had other limitations so I'm back to PhantomJS.


More From » click

 Answers
61

.click() is not standard. You need to 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);
}

[#79202] Saturday, March 30, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
calicinthias

Total Points: 447
Total Questions: 101
Total Answers: 118

Location: Botswana
Member since Sat, Dec 31, 2022
1 Year ago
calicinthias questions
Sun, Jan 2, 22, 00:00, 2 Years ago
Wed, Jan 13, 21, 00:00, 3 Years ago
Mon, Aug 10, 20, 00:00, 4 Years ago
;