pyprism
pyprism

Reputation: 3008

Returning wrong URL

I am using phantomejs-node for facebook login . Here is my nodejs code :

 var phantom = require('phantom');

 phantom.create(function(ph) {
   ph.createPage(function(page) {
     page.open("https://facebook.com", function(status) {
       setTimeout(function () {
       page.evaluate((function(URL) {
         document.getElementById("email").value = "wrong username";
         document.getElementById("pass").value = "wrong password";
         document.getElementById("u_0_1").click();  
         return document.URL;
       }), function(result) {
         console.log('Page url is ' + result);
         ph.exit();
         }, 5000);
       });

     });
     //page.render("page2.png");
   });
 });

Instead of returning https://www.facebook.com/login.php?login_attempt=1 , its returning https://www.facebook.com/ . By the way here is Phantomjs code that I am following :

var page = require('webpage').create();

page.open("http://www.facebook.com/login.php", function(status) {

  if (status === "success") {
    page.evaluate(function() {
        document.getElementById("email").value = "@gmail.com";
        document.getElementById("pass").value = "";
        document.getElementById("u_0_1").click();
    });
    window.setTimeout(function() {
    var url = page.evaluate(
    function () {
        return document.URL;
        }
    );
console.log( "- current url is " + url );
       page.render("page.png");
       phantom.exit();
    }, 5000);
  }
});             

Upvotes: 0

Views: 293

Answers (2)

Johannes Ewald
Johannes Ewald

Reputation: 17805

If you're tired of callback hell you could also give phridge a try. I've written this bridge because I didn't want to wrap all assignments and function calls with callbacks. It stringifies the given function and runs it inside PhantomJS.

A-0-'s solution would look like:

var page;

// creates a new PhantomJS process
phridge.spawn()
    .then(function (phantom) {
        return phantom.openPage("https://facebook.com");
    })
    .then(function (p) {
        page = p;
        return page.run(function (resolve) {
            // this function runs inside PhantomJS
            var page = this;

            page.evaluate(function () {
                document.getElementById("email").value = "@gmail.com";
                document.getElementById("pass").value = "password";
                document.getElementById("login_form").submit(); 
            });
            setTimeout(function () {
                page.render("page2.png");
                resolve();
            }, 6000);
        });
    })
    .then(function () {
        // page2.png rendered
    });

Upvotes: 1

azero0
azero0

Reputation: 2310

Try this code:

var phantom = require('phantom');
    phantom.create(function(ph) {
        ph.createPage(function(page) {
            page.open("https://facebook.com", function(status) {
            page.evaluate((function() {
                document.getElementById("email").value = "@gmail.com";
                document.getElementById("pass").value = "password";
                document.getElementById("login_form").submit(); 
                return;  
            }), function() {
                    console.log("loaded");
                    setTimeout(function(){
                        page.evaluate(function () {
                        return document.URL;
                        },function(result){
                             page.render("page2.png",function(){
                                 console.log("done rendering");
                             });
                             console.log("Page url is "+result);
                             ph.exit();  
                         });
                     },6000)
                  });

            });
          });
        });

Hope this is helpfull :)

Upvotes: 2

Related Questions