0

In my python project, the login process is handled by the HttpBasicAuth handler. After getting the response I should load the main page via selenium.

How it is supposed to be done?

It has the baseurl. When the base url is loaded by python-selenium, the pop-up box asks for the username and password for authentication. Then it enters into main page.

Here are the two steps:

  • Step 1: Login (using HttpBasicAuth handler)

The reason I am using the HttpBasicAuth handler is that when I open my url it opens a popup window for login. Since, we didn't know the control of the popup-modal dialog window to be handled by Selenium, we switched to HttpAuthentication.

(This is the part I need suggestions for.)

  • Step 2: After getting the Response, process the remaining steps in selenium.

In this intermediate part I use the response from HttpBasicAuth handler to authenticate so that Selenium can continue with the other steps in the test.

How can I make the site authenticated in Selenium? Do I have to create a cookie for Selenium webdriver by using HttpBasicAuthentication Response so as to skip the login box?

shamp00
  • 11,106
  • 4
  • 38
  • 81
Nava
  • 6,276
  • 6
  • 44
  • 68
  • Possible duplicate of [this question](http://stackoverflow.com/questions/5672407/basic-authentication-in-selenium-2-set-up-for-firefoxdriver-chromedriver-and). See the [Selenium FAQ](http://wiki.openqa.org/display/SEL/Selenium+Core+FAQ#SeleniumCoreFAQ-HowdoIuseSeleniumtologintositesthatrequireHTTPbasicauthentication%28wherethebrowsermakesamodaldialogaskingforcredentials%29%3F) for an answer. – shamp00 Feb 14 '12 at 09:18

1 Answers1

5

This is a problem that could easily be solved by adding header to your HTTP response, unfortunately Selenium2 does not support this feature.

You could try using it like this by embeddeing the username and password in the URL:

browser = webdriver.Firefox() # Get local session of firefox
browser.get("http://myusername:mypassword@www.yourpage.com") # Load page

Note this does not work for IE (due to security issues), but you can make it work by modifying the registry a bit.

As per your comment, I do not really understand your question. If you do the first request as I specified, the browser will automatically store a special Authorization: header with the password and username concatenated and base64-encoded. All the other requests will be now authenticated until you close the browser.

This method has little to do with Selenium 1 or Selenium 2, but what the specific browser supports. IE is the only browser that I know that restricts this.

Uku Loskit
  • 40,868
  • 9
  • 92
  • 93
  • Is this method supported in selenium1 and selenium2? also, In the Header Should we have to add the details of login credentials or what about that? please let me know briefly dude. – Nava Feb 13 '12 at 15:03
  • Thanks very much for the quick response.It is fine. – Nava Feb 13 '12 at 15:16
  • 1
    BTW you have to update the Firefox profile by overriding one variable: `FirefoxProfile ffProfile = new FirefoxProfile(); ffProfile.setPreference("browser.safebrowsing.malware.enabled", false);` – Pavel Janicek Feb 14 '12 at 09:31
  • The Alert.class in Selenium has a Beta method called .authenticateUsing(Credentials cred) that supposedly will handle a .htaccess auth popup. I haven't tried it yet but I will. – djangofan Apr 14 '15 at 20:28