0
0
Selenium Pythontesting~20 mins

iFrame switching (switch_to.frame) in Selenium Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
iFrame Switching Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Selenium Python code snippet?
Given the following code snippet that switches to an iframe and tries to find an element, what will be printed?
Selenium Python
from selenium import webdriver
from selenium.webdriver.common.by import By

# Assume driver is already initialized and page loaded
iframe = driver.find_element(By.ID, "frame1")
driver.switch_to.frame(iframe)
element = driver.find_element(By.ID, "inside")
print(element.text)
ARaises NoSuchElementException because element is outside the iframe
BPrints empty string because element has no text
CRaises NoSuchFrameException because frame ID is incorrect
DPrints the text inside the element with ID 'inside' within the iframe
Attempts:
2 left
💡 Hint
Remember that switching to the iframe context allows access to elements inside it.
assertion
intermediate
1:30remaining
Which assertion correctly verifies the iframe switch?
You want to assert that after switching to an iframe, the current frame is the one with ID 'frame2'. Which assertion is correct?
Selenium Python
iframe = driver.find_element(By.ID, "frame2")
driver.switch_to.frame(iframe)
Aassert driver.execute_script('return window.frameElement.id') == 'frame2'
Bassert driver.current_url == 'frame2'
Cassert driver.switch_to.frame('frame2')
Dassert driver.find_element(By.ID, 'frame2') is not None
Attempts:
2 left
💡 Hint
Use JavaScript to check the current frame element's ID.
🔧 Debug
advanced
2:00remaining
Why does this code raise NoSuchFrameException?
This code tries to switch to an iframe by name but raises NoSuchFrameException. What is the likely cause?
Selenium Python
driver.switch_to.frame('frame3')
AThe iframe is inside another iframe and needs nested switching
BThere is no iframe with name or ID 'frame3' on the page
CThe driver has not loaded the page yet
DThe iframe is hidden and cannot be switched to
Attempts:
2 left
💡 Hint
Check the iframe's name or ID attributes carefully.
locator
advanced
1:30remaining
Which locator is best to find an iframe for switching?
You want to switch to an iframe that has a unique attribute data-test='login-frame'. Which locator is best?
Adriver.find_element(By.ID, 'login-frame')
Bdriver.find_element(By.NAME, 'login-frame')
Cdriver.find_element(By.CSS_SELECTOR, 'iframe[data-test="login-frame"]')
Ddriver.find_element(By.TAG_NAME, 'iframe')
Attempts:
2 left
💡 Hint
Use attribute selectors for unique custom attributes.
framework
expert
2:00remaining
In a test framework, how to safely switch back from an iframe?
After switching to an iframe and performing actions, which is the correct way to return to the main page context?
Adriver.switch_to.default_content()
Bdriver.switch_to.parent_frame()
Cdriver.switch_to.frame(None)
Ddriver.switch_to.top_frame()
Attempts:
2 left
💡 Hint
Think about returning to the top-level page, not just one level up.