PhantomJS使用基本auth返回null进行评估 [英] PhantomJS evaluate with basic auth returning null

查看:89
本文介绍了PhantomJS使用基本auth返回null进行评估的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在基本始终为auth的页面上使用PhantomJS,例如,此页

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

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

使用以下代码

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

我得到这个输出

$ phantomjs test.js
success
null

无论我尝试什么,评估将继续返回 null ,即使该页面似乎已被打开,因为状态包含成功

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

如果我决定开启没有基本身份验证的页面,如 http://alexturpin.net/noauth ,我仍然得到相同的结果。只有当我在打开页面之前完全删除身份验证设置时才能正常工作。

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.

使用身份验证设置似乎与评估冲突。这是PhantomJS中的一个错误,还是我错过了什么?

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

推荐答案

使用该语法,您将清除设置对象,替换只有 userName 密码。使用该代码进行设置:

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

可能PhantomJS应该更好地处理,即不依赖设置默认对象。

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

这篇关于PhantomJS使用基本auth返回null进行评估的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆