Iframe assertions¶
Iframe assertions are available via expect(driver) (inherited by ExpectDriver). They operate on driver.find_elements(By.TAG_NAME, "iframe") and frame switching.
Availability¶
to_have_frame_available(index)¶
Asserts that switching to the iframe at index succeeds.
Selenium API: driver.switch_to.frame(index) (no exception)
Parameters:
index(int): 0-based iframe index.
Example:
Negation:
Count¶
to_have_frame_count(count)¶
Asserts that the number of iframes on the page equals count.
Selenium API: len(driver.find_elements(By.TAG_NAME, "iframe"))
Parameters:
count(int): Expected number of iframes.
Example:
to_have_frame_count_greater_than(n)¶
Asserts that the number of iframes is greater than n.
Selenium API: len(driver.find_elements(By.TAG_NAME, "iframe")) > n
Parameters:
n(int): Minimum exclusive count.
Example:
Content¶
to_have_frame_text(index, text)¶
Asserts that the text content of the iframe at index equals text.
Selenium API: switch to frame, read driver.page_source or body text
Parameters:
index(int): 0-based iframe index.text(str): Expected text content.
Example:
Frame context¶
to_be_in_frame()¶
Asserts that the driver is currently inside a frame (not in default content).
Selenium API: driver.switch_to.default_content() then check
Example:
to_be_in_default_content()¶
Asserts that the driver is in the default content (not inside a frame).
Selenium API: driver.switch_to.default_content()
Example:
Tips and common patterns¶
Working with iframes¶
from selenium.webdriver.common.by import By
# Verify an iframe is available on the page
expect(driver).to_have_frame_available(By.ID, "content-frame", timeout=10)
# Verify the number of iframes
expect(driver).to_have_frame_count(3)
expect(driver).to_have_frame_count_greater_than(0)
# Verify iframe text content
expect(driver).to_have_frame_text(By.ID, "content-frame", "Welcome")
Switching to an iframe and back¶
# Switch to an iframe
driver.switch_to.frame("content-frame")
# Verify we're in the iframe context
expect(driver).to_be_in_frame()
# Perform assertions inside the iframe
heading = driver.find_element(By.TAG_NAME, "h1")
expect(heading).to_have_text("Welcome")
# Switch back to the main document
driver.switch_to.default_content()
# Verify we're back in the default content
expect(driver).to_be_in_default_content()