如何使用Selenium Webdriver和PhantomJS进行基本身份验证?

时间:2013-08-14 00:34:38

标签: selenium-webdriver phantomjs ghostdriver

我在使用Selenium Server 2.33.0,Selenium Webdriver JS绑定2.34.0(npm包" selenium-webdriver")和Mac 10.6.8上的PhantomJS 1.9.1时遇到问题。我还尝试过其他JS绑定" webdriverjs"和" wd"具有类似的结果,所以我不认为这是绑定的问题。

我使用以下方法设置了Webdriver:

return new Webdriver.Builder().
    usingServer('http://localhost:4444/wd/hub').
    withCapabilities({
        "browserName": "phantomjs",
        "phantomjs.page.settings.userName":user,
        "phantomjs.page.settings.password":password
    }).build();

然后我在Selenium Server日志中看到这个输出:

PhantomJS is launching GhostDriver...
[INFO  - 2013-08-13T21:52:40.240Z] GhostDriver - Main - running on port 28904
[INFO  - 2013-08-13T21:52:40.394Z] Session [acd0ad70-0462-11e3-95df-4b230b17334d] - CONSTRUCTOR - Desired Capabilities:{"phantomjs.page.settings.password":"xxx","browserName":"phantomjs","phantomjs.page.settings.userName":"xxx"}
[INFO  - 2013-08-13T21:52:40.394Z] Session [acd0ad70-0462-11e3-95df-4b230b17334d] - CONSTRUCTOR - Negotiated Capabilities: {"browserName":"phantomjs","version":"1.9.1","driverName":"ghostdriver","driverVersion":"1.0.3","platform":"mac-10.6 (Snow Leopard)-32bit","javascriptEnabled":true,"takesScreenshot":true,"handlesAlerts":false,"databaseEnabled":false,"locationContextEnabled":false,"applicationCacheEnabled":false,"browserConnectionEnabled":false,"cssSelectorsEnabled":true,"webStorageEnabled":false,"rotatable":false,"acceptSslCerts":false,"nativeEvents":true,"proxy":{"proxyType":"direct"},"phantomjs.page.settings.password":"xxx","phantomjs.page.settings.userName":"xxx"}

我可以看到协商的功能包含我指定的用户和密码。但是当我尝试在使用http基本身份验证的网站中打开页面时,身份验证失败:

14:43:25.504 INFO - Executing: [find element: By.id: auth-username-field-inputEl] at URL: /session/04627dc2-a16c-42b3-b3dc-48e502f7cfec/element)
14:43:29.410 WARN - Exception thrown org.openqa.selenium.NoSuchElementException: Error Message => 'Unable to find element with id 'auth-username-field-inputEl''

如果我对没有身份验证的localhost网站运行我的脚本,它可以正常工作。如果我使用Firefox(不是通过Selenium)查看远程网站,我可以进行身份​​验证并查看主页。

我还尝试使用" -w"在Webdriver模式下运行PhantomJS。我得到了类似的结果。

如果我使用phantomjs运行此脚本,它会起作用:

var page=require('webpage').create();
//page.customHeaders={'Authorization': 'Basic '+btoa('xxx:xxx')};
page.settings.userName="xxx";
page.settings.password="xxx";
var callback=function(status){
    if(timer)window.clearTimeout(timer);
    if (status=='success' || status=='timedout') {
        console.log(status);
        console.log(page.plainText);
    }else{
        console.log('Failed to load.');
    }
    phantom.exit();
    };
var timer=window.setTimeout(callback,60000,'timedout');
var url="http://xxx.com";
page.open(url,callback);

我从此posting复制了此脚本。我发现使用1.9.1用户名和密码工作正常,我不需要设置customHeader。我在Selenium脚本中尝试了一个customHeader,但它并没有什么区别。这可能是GhostDriver的一个问题。

有没有人能够让这个工作?

1 个答案:

答案 0 :(得分:1)

我处于同样的情况。

我的理论是,我们使用NTLM(Windows身份验证)发出请求,该请求将获取401(未授权)并要求使用用户名/密码组合重新发送请求。 GhostDriver非常清楚设置是否适用only during the initial call,这可能会消除传入的凭据。

令人非常沮丧的是,JavaScript版本运行得非常好,但C#版本却失败了。

如果有任何建议,我很乐意听到。