Selenium 4.8.2中prompt类型alert的send_keys()方法失效无报错问题
问题:Selenium 4.8.2中Prompt弹窗send_keys()无响应且无异常
在使用Selenium 4.8.2版本时,调用send_keys()方法向网页的prompt类型alert输入文本时无任何反应,且未抛出任何异常,已在两个包含该类prompt的网站测试,测试代码如下:
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()
解决方案
- 确保Chrome与ChromeDriver版本匹配:Selenium 4.8.2要求ChromeDriver版本与本地Chrome浏览器版本完全一致,版本不匹配会直接导致alert交互逻辑异常。
- 替换
Alert(driver)为原生切换方式:部分场景下Alert类的封装存在兼容性问题,直接使用driver.switch_to.alert(或通过显式等待返回alert对象)更稳定。 - 移除冗余
time.sleep(),强化弹窗就绪检查:显式等待alert_is_present()后,弹窗可能仍未完全加载输入框,通过直接操作+异常捕获的方式替代固定等待,能适配不同加载速度的场景。 - 无头模式适配:若使用无头模式,需将参数改为
--headless=new(Chrome 112+版本),旧无头模式不支持alert交互。
修改后的测试代码
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 import sys 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: chrome_options.add_argument('--headless=new') # Chrome 112+无头模式需用此参数支持alert try: driver = webdriver.Chrome(service=chrome_service, options=chrome_options) except Exception as e: print(e) sys.exit() if url: 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, 5).until(ec.element_to_be_clickable(element)).click() # 等待alert出现并直接获取对象 alert = WebDriverWait(driver, 5).until(ec.alert_is_present()) try: alert.send_keys('Selenium') print("输入文本成功") except Exception as e: print(f"输入失败:{e}") print(f"Alert文本:{alert.text}") alert.accept() # 确认弹窗避免残留 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() # 等待alert出现并直接获取对象 alert = WebDriverWait(driver, 10).until(ec.alert_is_present()) try: alert.send_keys('name_of_person') print("输入文本成功") except Exception as e: print(f"输入失败:{e}") print(f"Alert文本:{alert.text}") alert.accept() driver.quit() if __name__ == '__main__': sample_01() sample_02()
内容的提问来源于stack exchange,提问作者AbhinavB
相关产品推荐
相关产品推荐

