@@ -1572,14 +1572,17 @@ def wait_for_and_switch_to_alert(driver, timeout=settings.LARGE_TIMEOUT):
15721572 timeout_exception (Exception , message )
15731573
15741574
1575- def switch_to_frame (driver , frame , timeout = settings .SMALL_TIMEOUT ):
1575+ def switch_to_frame (
1576+ driver , frame , timeout = settings .SMALL_TIMEOUT , invisible = False
1577+ ):
15761578 """
15771579 Wait for an iframe to appear, and switch to it. This should be
15781580 usable as a drop-in replacement for driver.switch_to.frame().
15791581 @Params
15801582 driver - the webdriver object (required)
15811583 frame - the frame element, name, id, index, or selector
15821584 timeout - the time to wait for the alert in seconds
1585+ invisible - if True, the iframe can be invisible
15831586 """
15841587 _reconnect_if_disconnected (driver )
15851588 start_ms = time .time () * 1000.0
@@ -1596,7 +1599,10 @@ def switch_to_frame(driver, frame, timeout=settings.SMALL_TIMEOUT):
15961599 by = "xpath"
15971600 else :
15981601 by = "css selector"
1599- if is_element_visible (driver , frame , by = by ):
1602+ if (
1603+ is_element_visible (driver , frame , by = by )
1604+ or (invisible and is_element_present (driver , frame , by = by ))
1605+ ):
16001606 with suppress (Exception ):
16011607 element = driver .find_element (by = by , value = frame )
16021608 driver .switch_to .frame (element )
@@ -1608,8 +1614,12 @@ def switch_to_frame(driver, frame, timeout=settings.SMALL_TIMEOUT):
16081614 plural = "s"
16091615 if timeout == 1 :
16101616 plural = ""
1611- message = "Frame {%s} was not visible after %s second%s!" % (
1617+ presence = "visible"
1618+ if invisible :
1619+ presence = "present"
1620+ message = "Frame {%s} was not %s after %s second%s!" % (
16121621 frame ,
1622+ presence ,
16131623 timeout ,
16141624 plural ,
16151625 )
0 commit comments