3

I am trying to access a website that requires a login thru an alert box such as the one below:

enter image description here

I have tried to look up many different ways to do this and they dont seem to work. what i have tried are listed below:

  1. Didnt work and gave me the same login alert.

    start_url = 'http://username:[email protected]' agent.get(start_url) 
  2. Keep getting an error message saying "NoAlertPresentException: Message: no alert open"

    start_url = 'http://www.example.com' alert = agent.switch_to_alert() alert.send_keys("username") alert.send_keys("password") 
  3. Get an error saying webdriver has no attribute "switchTo"

    start_url = 'http://www.example.com' agent.switchTo().alert().sendKeys("username") 

I have to use Chrome because of the versions of IE and Firefox I have and can get, do not support the functions in the site

1
  • Hello @harry-singh Were you able to solve this problem? Commented May 11, 2018 at 15:38

2 Answers 2

1

I have been having this exact same issue for some time now - with my end goal being to do this headless (in the background without visually launching an instance of Chromedriver).

Non-Ideal Solution 1: I first used a library called pynput to automatically type the credentials in to the alert box and click the ok button, it was pretty simple to get working but:

  • still didn't work headlessly
  • I had to be focused on the browser or it would type the credentials elsewhere

This worked great in the meantime as everywhere I looked online it seemed like there was nothing I could do to overcome authentication alerts headlessly...

I'm a relative beginner (started programming <1 year ago) so perhaps I just wasn't looking in the right places!

I've now solved this issue though like so:

First I logged in to the alert as normal on Chrome while monitoring the Network section of devtools to get a good look at the GET request for the protected page screencap here:

Upon seeing that the Authorization was Basic (this will work for Bearer too) I tested just copying the same request in Postman with this header and it worked! Now if only there was a way to make http requests from Selenium???

I first tried the library selenium-requests (which didn't work for me: I got the same error as this person https://github.com/cryzed/Selenium-Requests/issues/33 This library seems absolutely excellent and exactly what I needed, I just don't currently have the know-how to get past firewalls/whatever was stopping me at this stage...

What eventually worked for me was the library selenium-wire. I followed this guide https://pypi.org/project/selenium-wire/#intercepting-requests-and-responses to have the webdriver navigate to the protected page as normal, but intercept the request and add the Authorization header before sending it :) now this works for me totally headlessly. Granted, this won't work on more secure websites but I hope it helps someone having the same issue.

Sign up to request clarification or add additional context in comments.

Comments

0

This is Pythoncode
Problem with alert boxes (especially sweet-alerts is that they have a delay and Selenium is pretty much too fast)

An Option that worked for me is: (just exchange the button click in the end with whatever action you want to have)

while True: try: driver.find_element_by_xpath('//div[@class="sweet-alert showSweetAlert visible"]') break except: wait = WebDriverWait(driver, 1000) confirm_button = driver.find_element_by_xpath('//button[@class="confirm"]') confirm_button.click() 

Note to 2: Here is probably the error due to the alert taking more time to load than the single elements (such as username, etc.) Note to 3.: I think it should be switch_to

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.