title: How to do basic authentication using Selenium Webdriver and PhantomJS?
body: <p>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.</p>
  
  <p>I setup the Webdriver using this:</p>
  
  <pre><code>return new Webdriver.Builder().
      usingServer('http://localhost:4444/wd/hub').
      withCapabilities({
          "browserName": "phantomjs",
          "phantomjs.page.settings.userName":user,
          "phantomjs.page.settings.password":password
      }).build();
  </code></pre>
  
  <p>I then see in the Selenium Server log this output:</p>
  
  <pre><code>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"}
  </code></pre>
  
  <p>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:</p>
  
  <pre><code>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 =&gt; 'Unable to find element with id 'auth-username-field-inputEl''
  </code></pre>
  
  <p>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.</p>
  
  <p>I've also tried running PhantomJS in Webdriver mode using "-w" and I'm getting a similar result.</p>
  
  <p>It works if I run this script with phantomjs:</p>
  
  <pre><code>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);
  </code></pre>
  
  <p>I copied this script from this <a href="https://stackoverflow.com/questions/10114925/http-authentication-in-phantomjs">posting</a>. 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.</p>
  
  <p>Has anyone been able to get this working?</p>