12. Switching Contexts: iFrames, Tabs & Alerts

Context Switching in Selenium

# 1. iFrames:
driver.switch_to.frame("payment-frame")
driver.find_element(By.ID, "card-number").send_keys("4242 4242 4242 4242")
driver.switch_to.default_content() # Switch back!

# 2. Tabs:
original_window = driver.current_window_handle
for handle in driver.window_handles:
if handle != original_window:
driver.switch_to.window(handle)
break

# 3. JavaScript Alerts:
alert = driver.switch_to.alert
print("Alert text:", alert.text)
alert.accept()

Task

  • Implement is_popup_handled:
  • 1. Accept total_handles int.
  • 2. Return total_handles > 1.
💡Common doubts
Did you like the problem?
Lesson 12 of 16
Python
Loading code editor…
Next