How can you handle pop-ups and alerts using Selenium in Python?
IHUB Talent: The Best Selenium with Python Training in Hyderabad with Live Internship
IHUB Talent offers the best Selenium with Python training in Hyderabad, designed to equip students with practical skills and industry-relevant knowledge. Our comprehensive program covers everything from the basics of Selenium and Python to advanced automation techniques, ensuring that students gain a solid understanding of test automation in real-world scenarios.
With a strong focus on hands-on learning, IHUB Talent provides students with opportunities to work on live projects and gain experience in automating tests for web applications. The training includes step-by-step guidance on setting up Selenium WebDriver, integrating it with Python, and using frameworks like Pay test and Unit test for efficient test management.
What sets IHUB Talent apart is our live internship program. Students not only learn from industry experts but also get the chance to apply their knowledge on live projects, making them job-ready from day one. Our trainers, who are professionals with years of experience, provide personalized mentorship to ensure that every student gets the attention they need to succeed.
In Selenium, XPath is one of the most powerful locators to find elements on a webpage. XPath (XML Path Language) lets you navigate through elements and attributes in an XML/HTML document.
Great question ๐ Pop-ups and alerts are very common in web apps, and Selenium provides a clean way to handle them.
๐น Types of Pop-ups in Selenium
-
JavaScript Alerts/Pop-ups → generated by
alert(),confirm(),prompt()in JS. -
Browser-based Pop-ups → like authentication dialogs (username/password).
-
Web-element-based Pop-ups (HTML modals) → just regular HTML (divs), handled like normal elements.
๐น Handling JavaScript Alerts (Most Common)
Selenium provides the switch_to.alert interface to interact with alerts.
Example:
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
driver = webdriver.Chrome()
driver.get("https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_alert")
# Switch to iframe (because W3Schools runs code inside one)
driver.switch_to.frame("iframeResult")
# Click button that triggers alert
driver.find_element(By.TAG_NAME, "button").click()
time.sleep(2) # wait for alert
# Switch to alert
alert = driver.switch_to.alert
# Get alert text
print("Alert says:", alert.text)
# Accept the alert (OK button)
alert.accept()
driver.quit()
๐น Handling Confirmation Pop-ups (confirm())
alert = driver.switch_to.alert
alert.dismiss() # Clicks "Cancel"
๐น Handling Prompt Pop-ups (prompt())
alert = driver.switch_to.alert
alert.send_keys("Selenium Test") # Type into the prompt
alert.accept() # Click "OK"
๐น Handling Authentication Pop-ups (Browser-based)
You can pass credentials directly in the URL:
driver.get("https://username:password@the-site.com")
๐น Handling HTML Pop-ups (Modal Dialogs)
Since these are just HTML elements (like <div>), you handle them normally:
popup = driver.find_element(By.ID, "popupModal")
close_button = popup.find_element(By.CLASS_NAME, "close")
close_button.click()
✅ Summary:
-
Use
driver.switch_to.alertfor JavaScript pop-ups. -
Use
.accept(),.dismiss(),.send_keys()to interact. -
For authentication → pass credentials in URL.
-
For HTML modals → locate them like normal elements.
๐ Do you want me to also create a ready-to-run demo script that shows handling all three types (alert, confirm, prompt) so you can test it directly?
Read More
Comments
Post a Comment