SeleniumBase/help_docs/handling_iframes.md

2.2 KiB

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.