Skip to content

đŸ–ŧī¸ How to handle iframes

How to handle iframes

đŸ–ŧī¸ iframes follow the same principle as new windows: You must first switch to the iframe if you want to perform actions in there:

self.switch_to_frame("iframe")
# ... Now perform actions inside the iframe
self.switch_to_parent_frame()  # Exit the current iframe

To exit from multiple iframes, use self.switch_to_default_content(). (If inside a single iframe, this has the same effect as self.switch_to_parent_frame().)

self.switch_to_frame('iframe[name="frame1"]')
self.switch_to_frame('iframe[name="frame2"]')
# ... Now perform actions inside the inner iframe
self.switch_to_default_content()  # Back to the main page

đŸ–ŧī¸ You can also use a context manager to act inside iframes:

with self.frame_switch("iframe"):
    # ... Now perform actions while inside the code block
# You have left the iframe

This also works with nested iframes:

with self.frame_switch('iframe[name="frame1"]'):
    with self.frame_switch('iframe[name="frame2"]'):
        # ... Now perform actions while inside the code block
    # You are now back inside the first iframe
# You have left all the iframes

đŸ–ŧī¸ In special cases, you may want to set the page to the content of an iframe:

self.set_content_to_frame("iframe")

To back out of one call of that, use:

self.set_content_to_parent()

To back out of all nested calls of that, use:

self.set_content_to_default()

đŸ–ŧī¸ See SeleniumBase/examples/iframe_tests.py for tests that use all available iframe commands.