2

I am having issues with handling Authentication pop up in Chrome via Selenium.

from selenium import webdriver
from selenium.webdriver.common.keys import Keys 
import time

driver = webdriver.Chrome()
driver.get('URL')
time.sleep(5)
alert = driver.switch_to_alert()
alert.send_keys('Username')
alert.send_keys(Keys.TAB)
alert.send_keys('Password')

This returns an error--

"selenium.common.exceptions.NoAlertPresentException: Message: no alert open"

Alternatively, I also tried the following code:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time

driver = webdriver.Chrome()
driver.get('https://Username:Password@URL')

The second code works partially-

In Chrome the user is logged in but the page does not load. Only a blank page is displayed. Once the blank page is loaded, i passed only the URL(without user credentials) and it works fine.

In Firefox, the webpage loads perfectly.

Basically, the issue is with Chrome.

Any help is appreciated.

Thanks!

Sameem
  • 831
  • 1
  • 8
  • 23
  • Try sending the username and password along with the url. – Lalindra Kawshika Dec 08 '17 at 05:27
  • I tried that. In the alternative code that I have posted in my question, I'm sending the username and password along with the url. But that doesn't load the webpage properly. – Sameem Dec 08 '17 at 05:31
  • 1
    Possible duplicate of [Python Selenium Alert -- Prompt username and password is not working](https://stackoverflow.com/questions/45328654/python-selenium-alert-prompt-username-and-password-is-not-working) – undetected Selenium Dec 08 '17 at 05:34
  • can you share the url in which you are getting the alert or popup – Hiten Dec 08 '17 at 07:50
  • @Hiten, I cannot share the url due to confidentiality issues and even if I did, you wouldn't be able to access it unless you have an authorised VPN connection. – Sameem Dec 08 '17 at 12:37
  • Okay. Then check the url's source code. Is there any iframe ? If yes, then use that url for login and it will work. – Hiten Dec 13 '17 at 07:37
  • I believe the alert pop up is invoked by JS. I cannot see source code before logging in and I'm very sure there's no iframe. I did find a workaround though, edited the question to highlight that. – Sameem Dec 13 '17 at 13:15
  • Hey , did you find the solution. I got the same issue while adding an extension. WebdriverWait never detects the alert, when it is there. URL : https://chrome.google.com/webstore/detail/browsec-vpn-free-and-unli/omghfjlpggmjjaagoclmmobgdodcjboh?hl=en When I click add extension, the alert popped is never detected by selenium. – Ahsan Roy Feb 21 '19 at 11:43
  • 1
    @AhsanRoy Nope i did not. I did find a partial solution though. It is the second part of the post. Or you can check another question tagged to this post by DebanjanB. This might help you as well - [Handling “Authentication Required” alert box with Python 2.7 + Selenium Webdriver](https://stackoverflow.com/questions/27322871/handling-authentication-required-alert-box-with-python-2-7-selenium-webdrive) – Sameem Feb 21 '19 at 13:19

2 Answers2

0

The second code you have tried is correct with a slight mistake.

public void loginToSystem(String username,String password, String url){
driver = webdriver.Chrome()

driver.get("https://"+username+":"+password+"@ "+URL);
}
  • Hi, Thanks for your input. The code you have suggested doesn't look very different from mine. Except that you are using variable and I was passing them directly. Kindly point out what exactly is wrong in my code. Also in your code, have you given a space character after '@'? When I tried that, the space inserted %20 after https:// eventually making the url incorrect. My issue with the second code was that the web page was not loading properly. A blank screen was displayed. As a temporary workaround I passed the url again after a wait time of 5 seconds and it worked fine. – Sameem Dec 08 '17 at 07:15
0

First try to wait for alert present.

wait = WebDriverWait(driver, 10)
wait.until(EC.alert_is_present())

if alert is present then only move forword to next step. And also can you please check the screenshot if alert is actually present when testcase is failing.

  • The time.sleep() provides sufficient delay until the pop up occurs. Yes, the alert is present when the script fails. – Sameem Dec 08 '17 at 12:31
  • If it is giving NoAlertPresentException, it means there is not alert present. – Maninder Jeet Singh Dec 08 '17 at 19:23
  • You can just check this approach , Inspect the elements and find their xpath or id's and try with following code element = driver.find_element_by_id('userName') element.send_keys('Username') follow the same approach for password and submit button. Hope this will work for you. – Maninder Jeet Singh Dec 08 '17 at 19:33