12

I am having an issue with a secure URL:

Opening the URL creates an "Authentication Required" alert box with username and password fields.

I am fairly new to Selenium Webdriver and Python. I am not familiar with handling alerts and am currently manually typing in credentials until I can get this figured out. I have already tried adding my username/password into the URL. This does not work for me.

Could someone please point me in the direction of entering keys into username and password fields in an alertbox?

2
  • 1
    Can you provide a screenshot of the alert? Commented Dec 5, 2014 at 19:43
  • Find out from your devs what form of authentication this is using. Passing the username:password in the URL should work for most cases, unless you got the syntax wrong. Commented Dec 5, 2014 at 21:25

7 Answers 7

6

Could you try using Keys to tab within the alert?

from selenium import webdriver from selenium.webdriver.common.keys import Keys from selenium.webdriver.common.alert import Alert from selenium.webdriver.support.ui import WebDriverWait as wait from selenium.webdriver.support import expected_conditions as EC driver = webdriver.Firefox() driver.get('http://www.url.com/') wait(driver, 5).until(EC.alert_is_present()) alert = driver.switch_to_alert() alert.send_keys('username') alert.send_keys(Keys.TAB) alert.send_keys('password') alert.accept() 
Sign up to request clarification or add additional context in comments.

3 Comments

The line: 'driver.get('url.com/')' will effectively block, this method will not work.
using a concatenated string fixes the issue on my side : alert.send_keys('username' + Keys.TAB + 'password')
This is the only solution that has worked for me but I had to send keyes with one go as mentioned by @random_clyde
6

In case of such authentication, you need pass username and password to server while accessing page to avoid authentication window(which is out of reach of selenium)

Suppose the url you're trying to access is: http://example.com

you'll have to access this url with credentials like following:

driver.get('http://username:[email protected]') 

where username is your username and password is your password for the site.

3 Comments

I have tested this with the Chrome driver, and the page didn't load.
This does not work. Selenium/Firefox/Chrome strip out the username/password and then prompt you with an alert.
This works for me. Thanks for the solution @Alpha !!
2

Thanks for all of the responses. Unfortunately, none of these solutions worked for me. I suspect it may have something to do with the creation of a new profile every time firefox was opened by webdriver.

My workaround: I changed the driver from Firefox to IE, after installing the 32bit IE driver(http://selenium-release.storage.googleapis.com/index.html?path=2.44/). This solved my issue by no longer creating the alertbox, and allowing me to continue with my unittest.

1 Comment

My password has an @-sign in it so this was my solution as well. Not to mention that http gets with a plain-text password could be sniffed
1

I was having similar issues where adding my username/password into the URL did not work for me. This was because Firefox was alerting me with a confirmation box requiring me to confirm that I wanted to log into the site with the username provided. The following solved this issue:

from selenium import webdriver driver = webdriver.Firefox() driver.get('https://<username>:<password>@<site-needing-auth>.com') alert = driver.switch_to_alert() alert.accept() 

Comments

1

None of the answer before helped with my situation. The website I am authenticating to uses single sign on which was posing issues when using username:password@website.com.

In the Authentication window two fields needed to be entered both User Name and Password. Authentication Window

To solve this send the user name and password at one time to the alert box.

from selenium import webdriver from selenium.webdriver.common.keys import Keys from selenium.webdriver.common.alert import Alert def login(self): self.browser = webdriver.Firefox() self.browser.get(r'websitelogin.com') wait(self.browser, 1).until(expected_conditions.alert_is_present()) # "Switch" to the Alert browser alert = Alert(self.browser) # Send the username, TAB then password all in one go using a python f string alert.send_keys(f'username{Keys.TAB}password') alert.accept() 

2 Comments

Hey, where do you call this function? And what parameter do you pass to this function? I need the solution for this exact same thing.
You'll want to call this this function first since it sets up the webdriver and goes to the site. There are no parameters to set in this function specifically, but you will want to update the websitelogin.com, username, and password for your specific use case. Make sense? When I was doing this amost 2 years ago now, it only worked for the firefox webdriver. That could have changed by now though.
0

I was having the exact same problems as you until I noticed, that I simply forgot to write: 'https' instead of just http. If you add the 's', for me that did it!

So in code maybe you want to try:

driver.get('https://username:[email protected]') 

Comments

0

The below Python code can help to load such a URL prompting for authentication within a JavaScript Popup alert, I was also stuck here for a long time. It's because of Chrome driver will not allow such authentication techniques after the update 59 (probably). There are still backdoors via Selenium using the JavaScript engine in the browser to load such URLs.

driver.get("https://www.google.com") URL = "https://{username}:{password}@www.example.com" driver.executeScript("window.open('"+URL+"')") 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.