Hey Brilliant Stack overflow community,
I encountered an interesting scenarios regarding dealing with alert like pop up user authentication box.
1: website used (a practice website): https://the-internet.herokuapp.com/
2: When I clicked in to Basic Auth, there was a alert like pop up window show up (see below).
3: What I tried so far.
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
chrome_path = '.\chromedriver.exe'
chrome_service = Service(chrome_path)
chrome = webdriver.Chrome(service=chrome_service)
chrome.maximize_window()
chrome.get('https://the-internet.herokuapp.com/')
basic_auth = WebDriverWait(chrome,timeout=30).until(expected_conditions.element_to_be_clickable((By.PARTIAL_LINK_TEXT,'Basic Auth')))
basic_auth.click()
3-1: Treat it as alert (switch_to.alert) & use the send keys to insert user name and pwd. But Got the NoAlertPresentException.
chrome.switch_to.alert.send_keys('user\npass')
# get selenium.common.exceptions.NoAlertPresentException
3-2: Treat it as active element & use send keys. This time no error, but nothing being sent to input box too. And this pop up cannot be an element in my perspective.
chrome.switch_to.active_element.send_keys('user\npass')
# No error msg, but nothing show up in the pop up input field too.
3-3: Treat it as a new window, but when checking how many windows active (driver.window_handles), there was only one window. So this pop up is not a window neither.
print(len(chrome.window_handles)) # return 1, so the pop up is not a window too
3-4: I stumbled upon this post, but I am not sure I understand what the solution is. Python Selenium Alert Authentication Trouble
I will be great if someone can help me out regarding how to navigate through non web element like this one.
Thank you so much!
=======
Question Update
Thank you so much for the help from @Tyzeron and @Nic Laforge!
I tried both methods mentioned in the post, buy wondering if I am doing it correctly or not?
FYI: The website itself is a practice website, so each time you input username and password then sign in, the website will generate a new basic authentication pop up. Which hard to tell if the methods I tried worked or not.
Method 1: Put Basic Authentication in the URL when using get method.
chrome_path = '.\chromedriver.exe'
chrome_service = Service(chrome_path)
chrome = webdriver.Chrome(service=chrome_service)
chrome.maximize_window()
chrome.get('https://username:pwd123@the-internet.herokuapp.com/basic_auth')
Method 2: Using Selenium Wire (I am not sure if my code is correct)
from seleniumwire import webdriver as wire_driver
import base64
def request_interceptor(req):
# add Authentication Here.
# VXNlck5hbWU6UHNkMTIz is the base64 encoded str for "UserName:Psd123"
req.headers['Authorization'] = 'Basic VXNlck5hbWU6UHNkMTIz=='
print(req.headers)
chrome_path = '.\chromedriver.exe'
chrome_service = Service(chrome_path)
chrome = wire_driver.Chrome(service=chrome_service)
chrome.maximize_window()
chrome.request_interceptor = request_interceptor
chrome.get('https://the-internet.herokuapp.com/basic_auth')
The second method with selenium-wire did not generate an error too but no visible clue on the page indicates the success or not.
In addition, the printed result for req object in the function looks as follow:
Now, Both methods did not generate the error nor visible clues to indicate the success of the code. So I am wondering how I can tell it worked or not?