Reputation:
How come my function does not trigger the click event in phantomjs but it does in the chrome browser? On an li element specifically.
if(page.injectJs("./jquery-2.1.4.min.js") == false){
console.log("jquery Failed")
}
var sizePicked = page.evaluate(function(){
var Elements = document.getElementsByTagName("li");
for(i = 0; i < Elements.length; i++){
if (Elements[i].innerHTML.toLowerCase().indexOf("10") !== -1) {
$(Elements[i]).trigger("click");
break;
}
}
})
This is the site I'm working on.
Upvotes: 0
Views: 905
Reputation: 16838
In earlier PhantomJS versions (before 2.0.0) the underlying rendering engine does not support click() function on HTMLElement elements, but supports it on HTMLInputElement elements (inputs and buttons). That's why you can click() on input buttons but not on <li>s.
There are two possible solutions for your case:
A) Switch to PhantomJS 2
B) Use a shim to emulate click() function on HTMLElements. Just issue this evaluate
at the top of your script and it will work for later evaluates
.
page.evaluate(function(){
if (!HTMLElement.prototype.click) {
HTMLElement.prototype.click = function() {
var ev = document.createEvent('MouseEvent');
ev.initMouseEvent(
'click',
/*bubble*/true, /*cancelable*/true,
window, null,
0, 0, 0, 0, /*coordinates*/
false, false, false, false, /*modifier keys*/
0/*button=left*/, null
);
this.dispatchEvent(ev);
};
}
});
Code from this solution
Upvotes: 3