0

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 :-

  1. Why those statements inside setInterval function are not getting executed?
  2. 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.

Ishank Jain
  • 309
  • 5
  • 8
  • Register to the page.onError event. element.click is probably undefined. – Artjom B. Jul 23 '15 at 06:48
  • @Artjom B. In that case I would have got error - undefined element. But I did not get any error and moreover, statement console.log('Downloading the file') was not executed. This shows that it didn't enter inside setInterval function. – Ishank Jain Jul 23 '15 at 06:57
  • In that case, I can't reproduce your problem with either PhantomJS 1.9.8 or v2.0.0 when commenting out the clicking and changing `typeof arr !== 'undefined'` to `arr.length > 0`. `arr` is defined and it is always a NodeList, but it might be empty. – Artjom B. Jul 23 '15 at 07:21
  • See related SO question about using the `click` method in Phantom. http://stackoverflow.com/questions/15739263/phantomjs-click-an-element/15948355#15948355 –  Jul 23 '15 at 07:41
  • @Artjom B. Assuming Login to be successful, why doesn't it execute statements inside setInterval function? I think when it calls onLoadFinished after clicking to Login button, it doesn't come back to page.evaluate within 2 seconds. I am not sure what is the reason? – Ishank Jain Jul 23 '15 at 08:51
  • 1
    Well, *this* script doesn't have a problem that you describe. Please provide a [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). Last guess: you're exiting too early. – Artjom B. Jul 23 '15 at 08:54
  • @torazaburo, `click()` is not a problem because anyways login is working and secondly statement `console.log('Downloading the file')` is not working. – Ishank Jain Jul 23 '15 at 08:56

1 Answers1

1

I had run into the same problem when I started experimenting with PhantomJS. I realized that the issue could be that the page.evaluate() is running in the context of the opened page. So when you click on the login button, the page is redirected, so the statements after the click() are not being executed or the output is not visible anymore.

I solved this by creating an another page.run() block and running it after the previous one is finished (ie. in a timeout) - because I wanted to execute that part of the script after the login.

rendes
  • 117
  • 8