Skip to content

Handling invisible iframes #3875

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion help_docs/method_summary.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ self.set_window_position(x, y)
self.maximize_window()
self.minimize_window()
self.reset_window_size()
self.switch_to_frame(frame="iframe", timeout=None)
self.switch_to_frame(frame="iframe", timeout=None, invisible=False)
self.switch_to_default_content()
self.switch_to_parent_frame()
with self.frame_switch(frame, timeout=None):
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ setuptools~=70.2;python_version<"3.10"
setuptools>=80.9.0;python_version>="3.10"
wheel>=0.45.1
attrs>=25.3.0
certifi>=2025.6.15
certifi>=2025.7.9
exceptiongroup>=1.3.0
websockets~=13.1;python_version<"3.9"
websockets>=15.0.1;python_version>="3.9"
Expand Down
2 changes: 1 addition & 1 deletion seleniumbase/__version__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# seleniumbase package
__version__ = "4.40.2"
__version__ = "4.40.3"
9 changes: 6 additions & 3 deletions seleniumbase/fixtures/base_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -3591,15 +3591,16 @@ def reset_window_size(self):
self.set_window_rect(x, y, width, height)
self.__demo_mode_pause_if_active(tiny=True)

def switch_to_frame(self, frame="iframe", timeout=None):
def switch_to_frame(self, frame="iframe", timeout=None, invisible=False):
"""Wait for an iframe to appear, and switch to it. This should be
usable as a drop-in replacement for driver.switch_to.frame().
The iframe identifier can be a selector, an index, an id, a name,
or a web element, but scrolling to the iframe first will only occur
for visible iframes with a string selector.
@Params
frame - the frame element, name, id, index, or selector
timeout - the time to wait for the alert in seconds """
timeout - the time to wait for the alert in seconds
invisible - if True, the iframe can be invisible """
self.__check_scope()
if not timeout:
timeout = settings.LARGE_TIMEOUT
Expand Down Expand Up @@ -3649,7 +3650,9 @@ def switch_to_frame(self, frame="iframe", timeout=None):
time.sleep(0.035)
if self.undetectable:
time.sleep(0.035)
page_actions.switch_to_frame(self.driver, frame, timeout)
page_actions.switch_to_frame(
self.driver, frame, timeout, invisible=invisible
)
self.wait_for_ready_state_complete()
if self.__needs_minimum_wait():
time.sleep(0.015)
Expand Down
16 changes: 13 additions & 3 deletions seleniumbase/fixtures/page_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1572,14 +1572,17 @@ def wait_for_and_switch_to_alert(driver, timeout=settings.LARGE_TIMEOUT):
timeout_exception(Exception, message)


def switch_to_frame(driver, frame, timeout=settings.SMALL_TIMEOUT):
def switch_to_frame(
driver, frame, timeout=settings.SMALL_TIMEOUT, invisible=False
):
"""
Wait for an iframe to appear, and switch to it. This should be
usable as a drop-in replacement for driver.switch_to.frame().
@Params
driver - the webdriver object (required)
frame - the frame element, name, id, index, or selector
timeout - the time to wait for the alert in seconds
invisible - if True, the iframe can be invisible
"""
_reconnect_if_disconnected(driver)
start_ms = time.time() * 1000.0
Expand All @@ -1596,7 +1599,10 @@ def switch_to_frame(driver, frame, timeout=settings.SMALL_TIMEOUT):
by = "xpath"
else:
by = "css selector"
if is_element_visible(driver, frame, by=by):
if (
is_element_visible(driver, frame, by=by)
or (invisible and is_element_present(driver, frame, by=by))
):
with suppress(Exception):
element = driver.find_element(by=by, value=frame)
driver.switch_to.frame(element)
Expand All @@ -1608,8 +1614,12 @@ def switch_to_frame(driver, frame, timeout=settings.SMALL_TIMEOUT):
plural = "s"
if timeout == 1:
plural = ""
message = "Frame {%s} was not visible after %s second%s!" % (
presence = "visible"
if invisible:
presence = "present"
message = "Frame {%s} was not %s after %s second%s!" % (
frame,
presence,
timeout,
plural,
)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@
'setuptools>=80.9.0;python_version>="3.10"',
'wheel>=0.45.1',
'attrs>=25.3.0',
"certifi>=2025.6.15",
"certifi>=2025.7.9",
"exceptiongroup>=1.3.0",
'websockets~=13.1;python_version<"3.9"',
'websockets>=15.0.1;python_version>="3.9"',
Expand Down