When trying to use the send_keys() method to enter the text in a webpage's prompt type alerts, it is doing nothing, nor any exceptoin is being thrown so that I can correct the issue. I have tried this on two different website where I get such prompts. I am using the Selenium version 4.8.2. The complete code is as below:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.common.by import By
from selenium.webdriver.common.alert import Alert
import sys
import time
def launch_browser(url:str = None, is_hidden = False) -> webdriver.Chrome:
chromedriver_path = r"D:\Drivers\chromedriver.exe"
chrome_service = Service(chromedriver_path)
browser_path = r"C:\Program Files\Google\Chrome\Application\chrome.exe"
chrome_options = webdriver.ChromeOptions()
chrome_options.binary_location = browser_path
chrome_options.add_argument('--start-maximized')
chrome_options.add_argument('--log-level=3')
if is_hidden != False: chrome_options.add_argument('--headless')
try:
driver = webdriver.Chrome(service=chrome_service, options=chrome_options)
except Exception as e:
print(e)
sys.exit()
if url != None: driver.get(url)
return(driver)
def sample_01():
url = 'https://www.selenium.dev/documentation/webdriver/interactions/alerts/'
driver = launch_browser(url)
element = driver.find_element(By.LINK_TEXT, 'See a sample prompt')
WebDriverWait(driver, timeout=5, poll_frequency=1).until(ec.element_to_be_clickable(element)).click()
WebDriverWait(driver, timeout=5, poll_frequency=0.5).until(ec.alert_is_present())
alert = Alert(driver)
time.sleep(2)
try:
alert.send_keys('Selenium')
except Exception as e:
print(e)
print(alert.text)
time.sleep(5)
driver.quit()
def sample_02():
url = 'https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_prompt'
driver = launch_browser(url)
driver.switch_to.frame("iframeResult")
driver.find_element(By.XPATH, '//button[@onclick="myFunction()"]').click()
WebDriverWait(driver, 10, 1).until(ec.alert_is_present())
alert = Alert(driver)
try:
alert.send_keys('name_of_person')
except Exception as e:
print(e)
print(alert.text)
time.sleep(5)
if __name__ == '__main__':
sample_01()
sample_02()
Your code works, keys are being sent, however the input text is not displayed in Chrome. See this chrome bug:
https://bugs.chromium.org/p/chromedriver/issues/detail?id=1120#c11