I am trying to Login into website using Phantomjs. When I click loginButton after filling form elements, I was trying to find some element id inside setInterval function as I want page to load first (gave 2 seconds) and then only search for the element but the problem here is : The statements inside my setInterval are not getting executed. Here is the phantomjs code :
var url = "http://www.someUrl.com";
var page = require('webpage').create();
page.onConsoleMessage = function(msg, lineNo, sourceId){
console.log('console: ' + msg);
};
page.onLoadFinished = function(status){
console.log("Loading finished");
page.render('webpage.png');
};
page.open(url, function(status){
page.evaluate(function(){
arr = document.getElementsByClassName('formLabel');
if (typeof arr !== 'undefined'){
arr[0].value = 'Login-id';
arr[1].value = 'Password';
}
document.getElementById('loginButton').click();
setInterval(function(){
console.log("Downloading the file");
document.getElementById('downloadButton').click();
}, 2000);
});
});
When I execute the above code, I get the output as :-
Loading finished
Loading finished
For debugging purposes, I wrote console.log('Downloading the file')
inside the setInterval function but this statement is not getting executed.
I also render the webpage as image page.render('webpage.png')
to check whether I am able to successfully login or not. And I see that Login is successful. So, I have few questions here :-
- Why those statements inside
setInterval
function are not getting executed? - Where should I write those statements to make them work correctly?
In fact, when I wrote those statements just after the loginButton.click()
statement, I got this error - TypeError: 'null' is not an object (near 'downloadButton').click();...'), that means page did not load by that time. Hence, I wrote them inside setInterval function and gave the waiting time as 2 seconds.
PS : There is no problem in my click
statement because my loginButton.click
is working.
Thanks in Advance.