Cross_Column

Friday, 30 January 2026

Handling Different Components



Advanced Element Handling in Selenium with Python

🧠 Advanced Element Handling in Selenium with Python

Modern web applications use dynamic UI components like alerts, frames, windows, dropdowns, and dynamic suggestions. Handling these properly is essential for building reliable automation frameworks.


🚨 Handling Alerts (Simple, Confirmation, Prompt)

alert = driver.switch_to.alert

alert.accept()     # OK
alert.dismiss()    # Cancel
alert.send_keys("Hello")  # Prompt input
text = alert.text
Types:
  • Simple Alert
  • Confirmation Alert
  • Prompt Alert
---

🪟 Handling Frames / iFrames

Frames are embedded HTML documents inside a page.

Switch to Frame

driver.switch_to.frame("frameName")
driver.switch_to.frame(0)
driver.switch_to.frame(driver.find_element(By.ID, "frame1"))

Switch Back to Default Content

driver.switch_to.default_content()
---

🧭 Handling Multiple Windows / Tabs

Window Handles Concept

handles = driver.window_handles
current = driver.current_window_handle

for handle in handles:
    if handle != current:
        driver.switch_to.window(handle)
---

📤 File Upload

Send file path directly to input type file.

driver.find_element(By.ID, "upload").send_keys("C:/files/test.pdf")
---

📥 File Download

Handled using browser preferences.

from selenium.webdriver.chrome.options import Options

options = Options()
prefs = {"download.default_directory": "C:/downloads"}
options.add_experimental_option("prefs", prefs)

driver = webdriver.Chrome(options=options)
---

📋 Handling Dropdowns (Select Class)

from selenium.webdriver.support.ui import Select

dropdown = Select(driver.find_element(By.ID, "country"))
dropdown.select_by_visible_text("India")
dropdown.select_by_value("IN")
dropdown.select_by_index(1)
---

🔍 Handling Auto-Suggestions

driver.find_element(By.ID, "search").send_keys("Selen")

suggestions = driver.find_elements(By.XPATH, "//li")

for item in suggestions:
    if item.text == "Selenium Python":
        item.click()
        break
---

☑ Handling Checkboxes

checkbox = driver.find_element(By.ID, "terms")

if not checkbox.is_selected():
    checkbox.click()
---

🔘 Handling Radio Buttons

radio = driver.find_element(By.ID, "male")

if not radio.is_selected():
    radio.click()
---
🎯 Best Practices:
  • Always verify element state before action
  • Use explicit waits
  • Handle windows carefully
  • Use stable locators
  • Validate before upload/download
  • Handle dynamic elements properly
---
🎯 Conclusion:

Advanced handling is what separates basic automation from professional automation frameworks. Mastering these concepts enables you to automate complex, real-world applications confidently.

No comments:

Post a Comment

Few More

Interview & Real-Time Examples

Advanced Selenium Automation Guide with Python 🚀 Advanced Selenium Automation with Python...