In this Python Selenium article we want to learn about Handling Pop-ups with Python Selenium, so Pop-ups or modal dialogs are common elements encountered during web browsing that require special attention when performing browser automation using Python Selenium. These pop-ups may include cookie consent notices, subscription prompts, alerts or other types of dialog boxes. In this tutorial we want to talk about Python Selenium Pop-ups.
For working the examples of this tutorial you need some requirements, first you should have installed Python in your system, then we need to install Python Selenium and you can use pip for that like this.
1 |
pip install selenium |
Note: You can download the drivers from here.
Python Selenium Handling Basic JavaScript Alert Pop-ups
JavaScript alert pop-ups are simple dialog boxes that display a message and an OK button. For handling this kind of pop-ups, we can use the switch_to.alert method provided by Selenium.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
from selenium import webdriver # Launch the browser driver = webdriver.Chrome() # Open web page with a JavaScript alert pop-up driver.get("https://example.com") # Switch to the alert pop-up alert = driver.switch_to.alert # Accept the alert alert.accept() # Close the browser driver.close() |
Python Selenium Confirmation Pop-ups
Confirmation pop-ups are similar to alert pop-ups but it includes an additional Cancel button. We can handle confirmation pop-ups using switch_to.alert method like this.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
from selenium import webdriver # Launch the browser driver = webdriver.Chrome() # Open web page with a confirmation pop-up driver.get("https://example.com") # Switch to confirmation pop-up confirmation = driver.switch_to.alert # Accept or dismiss the confirmation pop-up confirmation.accept() # To accept # or confirmation.dismiss() # To dismiss # Close the browser driver.close() |
Python Selenium Handling Pop-ups with Multiple Windows
In scenarios where clicking a link or button on a web page opens a new window or pop-up, we need to switch the driver focus to the new window or pop-up using the switch_to.window method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
from selenium import webdriver # Launch the browser driver = webdriver.Chrome() # Open web page with a link that opens new window driver.get("https://example.com") # Click the link to open the new window link = driver.find_element_by_link_text("Open Popup") link.click() # Switch the driver focus to the new window driver.switch_to.window(driver.window_handles[1]) # Perform actions on the new window or pop-up # ... # Close the new window or pop-up driver.close() # Switch back to the main window driver.switch_to.window(driver.window_handles[0]) # Close the main window driver.close() |
Python Selenium Handling Pop-ups within iframes
If a pop-up is embedded within an iframe, we need to switch the driver focus to the iframe using the switch_to.frame method before interacting with the elements inside it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
from selenium import webdriver # Launch browser driver = webdriver.Chrome() # Open web page with an iframe containing a pop-up driver.get("https://example.com") # Switch the driver focus to the iframe iframe = driver.find_element_by_css_selector("iframe[name='popup-iframe']") driver.switch_to.frame(iframe) # Perform actions within the iframe (pop-up) # ... # Switch back to the main content driver.switch_to.default_content() # Close the browser driver.close() |
So we can say that Handling pop-ups is an important skill when automating web browsing tasks using Python Selenium. By using above techniques that we have already mentioned in the article, you can effectively manage and interact with different types of pop-ups encountered during your automation workflow. Remember to customize the code snippets based on your specific requirements and web page structure.
Learn More on Python Selenium
- Web Automation with Python Selenium
- Page Navigation with Python Selenium
- Python Selenium and CSS Selectors
- Python Selenium and XPath Usage
- Working with Python Selenium Cookies
- Implicit and Explicit Waits in Python Selenium