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?

Edwin
  • 141
  • 1
  • 1
  • 7
  • 1
    Can you provide a screenshot of the alert? – Saifur Dec 05 '14 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. – SiKing Dec 05 '14 at 21:25

7 Answers7

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()
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:password@example.com')

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

Alpha
  • 13,320
  • 27
  • 96
  • 163
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.

Edwin
  • 141
  • 1
  • 1
  • 7
  • 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 – gh4x Jan 25 '17 at 19:31
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()
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()
Daniel Butler
  • 3,239
  • 2
  • 24
  • 37
  • 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. – Tuhin Jul 24 '20 at 19:56
  • 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. – Daniel Butler Jul 24 '20 at 20:06
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:password@domain-name.org')
Ekkstein
  • 771
  • 11
  • 20
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+"')")
TechRookie
  • 171
  • 2
  • 4