@@ -1572,14 +1572,17 @@ def wait_for_and_switch_to_alert(driver, timeout=settings.LARGE_TIMEOUT):
1572
1572
timeout_exception (Exception , message )
1573
1573
1574
1574
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
+ ):
1576
1578
"""
1577
1579
Wait for an iframe to appear, and switch to it. This should be
1578
1580
usable as a drop-in replacement for driver.switch_to.frame().
1579
1581
@Params
1580
1582
driver - the webdriver object (required)
1581
1583
frame - the frame element, name, id, index, or selector
1582
1584
timeout - the time to wait for the alert in seconds
1585
+ invisible - if True, the iframe can be invisible
1583
1586
"""
1584
1587
_reconnect_if_disconnected (driver )
1585
1588
start_ms = time .time () * 1000.0
@@ -1596,7 +1599,10 @@ def switch_to_frame(driver, frame, timeout=settings.SMALL_TIMEOUT):
1596
1599
by = "xpath"
1597
1600
else :
1598
1601
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
+ ):
1600
1606
with suppress (Exception ):
1601
1607
element = driver .find_element (by = by , value = frame )
1602
1608
driver .switch_to .frame (element )
@@ -1608,8 +1614,12 @@ def switch_to_frame(driver, frame, timeout=settings.SMALL_TIMEOUT):
1608
1614
plural = "s"
1609
1615
if timeout == 1 :
1610
1616
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!" % (
1612
1621
frame ,
1622
+ presence ,
1613
1623
timeout ,
1614
1624
plural ,
1615
1625
)
0 commit comments