I use PhantomJS 1.9.7 on windows 8.1 and I am going to click on Login button after typing username and password. I can write username and password but, when I want PhantomJS to click on the button, It can find the element but is not able of clicking on that. I found in previous posts that I need to create event and use "dispatchEvent". I did that but I got an error as follows:
TypeError: 'undefined' is not a function (evaluating 'elm.dispatchEvent(event)')
I also tried to get help from EventListener but I got the same error for that.
How can I click on an element?
var page = require('webpage').create();
page.open('URL', function() {
var submitButton = enterUserAndPassAndLogin();
click(submitButton);
phantom.exit();
});
function enterUserAndPassAndLogin() {
var element = page.evaluate(function() {
document.querySelector('input[name="username"]').value = "*******";
document.querySelector('input[name="password"]').value = "*******";
return document.getElementsByTagName("Button");
});
return element;
}
function click(elm) {
var event = document.createEvent("MouseEvent");
event.initMouseEvent("click", true, true, window,
0, 0, 0, 0, 0, false, false, false, false, 0, null);
elm.dispatchEvent(event);
}