3
votes

I'm having trouble authenticating using Selenium Server 2.33.0, Selenium Webdriver JS binding 2.34.0 (npm package "selenium-webdriver") and PhantomJS 1.9.1 on Mac 10.6.8. I've also tried the other JS bindings "webdriverjs" and "wd" with the similar results so I don't think it's a problem with this binding.

I setup the Webdriver using this:

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

I then see in the Selenium Server log this output:

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

I can see that the negotiated capabilities contain the user and password I specified. But the authentication fails when I try to open a page in a website that uses http basic authentication:

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''

If I run my script against the localhost website that doesn't have authentication, it works fine. If I view the remote website using Firefox (not via Selenium), I can authenticate and see the homepage.

I've also tried running PhantomJS in Webdriver mode using "-w" and I'm getting a similar result.

It works if I run this script with 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);

I copied this script from this posting. I found that with 1.9.1 the userName and password work fine, I didn't need to set a customHeader. I tried a customHeader in my Selenium script but it didn't make a difference. This could be an issue with GhostDriver.

Has anyone been able to get this working?

1
It looks like the page isn't loading properly. Have you tried taking a screenshot/grabbing the html source to see what page you are trying to run on looks like?Nathan Merrill
Yes, the screenshot is blank and the source is an empty html page.ybendana

1 Answers

1
votes

I am in this exact same situation.

My theory is that we're making the request using NTLM (Windows Authentication), which will solicit a 401 (Unauthorized) and require resending the request with the username/password combination. GhostDriver is quite clear that settings apply only during the initial call, which would likely obviate the credentials passed in.

It is pretty frustrating that the JavaScript version works so well but the C# version fails so spectacularly.

If there are any suggestions, I would love to hear them.