返回错误的网址

时间:2014-05-20 04:58:30

标签: node.js phantomjs

我正在使用phantomejs-node进行Facebook登录。这是我的nodejs代码:

 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");
   });
 });

而不是返回https://www.facebook.com/login.php?login_attempt=1,而是返回https://www.facebook.com/。顺便说一句,这是我关注的Phantomjs代码:

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);
  }
});             

2 个答案:

答案 0 :(得分:2)

试试这段代码:

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)
                  });

            });
          });
        });

希望这有用:)

答案 1 :(得分:1)

如果你厌倦了回调地狱,你也可以试试phridge。我写过这个桥,因为我不想用回调包装所有赋值和函数调用。它将给定函数字符串化并在PhantomJS中运行。

A-0的解决方案如下:

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
    });