Junior — Middle
Is it possible to automate working with pop-up windows in Selenium?
sobes.tech AI
Answer from AI
Yes, Selenium can automate interactions with pop-up windows (alert, confirm, prompt).
This is done using the Alert class, which allows switching to the pop-up window and interacting with it:
from selenium import webdriver
from selenium.webdriver.common.alert import Alert
driver = webdriver.Chrome()
driver.get('http://example.com')
# Wait for the alert to appear
alert = Alert(driver)
# Accept the alert (click OK)
alert.accept()
# Or dismiss the alert (click Cancel)
# alert.dismiss()
# If it's a prompt, you can enter text
# alert.send_keys('text')
It is important to switch to the alert before interacting with it, otherwise Selenium will not be able to control the pop-up window.