PhantomJS evaluate with basic auth returning null

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-02 07:14:34

问题


I am trying to use PhantomJS on a page with basic always auth, for example, this page

http://alexturpin.net/auth (test:rosebud)

Using the following code

var webpage = require('webpage');

page = webpage.create();
page.settings = {
    userName: "test",
    password: "rosebud"
};

page.open("http://alexturpin.net/auth/", function(status) {
    console.log(status);
    var retval = page.evaluate(function() {
        return "test";
    });
    console.log(retval);
});

I get this output

$ phantomjs test.js
success
null

Whatever I try, evaluate will keep returning null, even though the page seems to have been opened fine because status contains "success".

If I decide to open a page with no basic auth, like http://alexturpin.net/noauth, I still get the same results. Only when I finally remove the authentication settings altogether before opening the page does it work.

The use of authentication settings seem to be conflicting with the evaluate. Is this a bug in PhantomJS, or did I miss something?


回答1:


With that syntax you are wiping out the settings object, replacing it with only userName and password. Use that code to set them:

page.settings.userName = "test";
page.settings.password = "rosebud";

Probably PhantomJS should handle that better, i.e. not rely on the settings object for defaults.




回答2:


This is a closure issue, try to put your console.log in a callback



来源:https://stackoverflow.com/questions/10542361/phantomjs-evaluate-with-basic-auth-returning-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!