@@ -1061,7 +1061,7 @@ def is_element_in_an_iframe(self, selector, by=By.CSS_SELECTOR):
1061
1061
return False
1062
1062
1063
1063
def switch_to_frame_of_element (self , selector , by = By .CSS_SELECTOR ):
1064
- """ Set driver control to the iframe of the element (assuming the
1064
+ """ Set driver control to the iframe containing element (assuming the
1065
1065
element is in a single-nested iframe) and returns the iframe name.
1066
1066
If element is not in an iframe, returns None, and nothing happens.
1067
1067
May not work if multiple iframes are nested within each other. """
@@ -1076,13 +1076,26 @@ def switch_to_frame_of_element(self, selector, by=By.CSS_SELECTOR):
1076
1076
iframe_identifier = iframe ['name' ]
1077
1077
elif iframe .has_attr ('id' ) and len (iframe ['id' ]) > 0 :
1078
1078
iframe_identifier = iframe ['id' ]
1079
+ elif iframe .has_attr ('class' ) and len (iframe ['class' ]) > 0 :
1080
+ iframe_class = " " .join (iframe ["class" ])
1081
+ iframe_identifier = '[class="%s"]' % iframe_class
1079
1082
else :
1080
1083
continue
1081
- self .switch_to_frame (iframe_identifier )
1082
- if self .is_element_present (selector , by = by ):
1083
- return iframe_identifier
1084
+ try :
1085
+ self .switch_to_frame (iframe_identifier , timeout = 1 )
1086
+ if self .is_element_present (selector , by = by ):
1087
+ return iframe_identifier
1088
+ except Exception :
1089
+ pass
1084
1090
self .switch_to_default_content ()
1085
- return None
1091
+ try :
1092
+ self .switch_to_frame (selector , timeout = 1 )
1093
+ return selector
1094
+ except Exception :
1095
+ if self .is_element_present (selector , by = by ):
1096
+ return ""
1097
+ raise Exception ("Could not switch to iframe containing "
1098
+ "element {%s}!" % selector )
1086
1099
1087
1100
def hover_on_element (self , selector , by = By .CSS_SELECTOR ):
1088
1101
selector , by = self .__recalculate_selector (selector , by )
@@ -1310,7 +1323,13 @@ def maximize_window(self):
1310
1323
self .__demo_mode_pause_if_active ()
1311
1324
1312
1325
def switch_to_frame (self , frame , timeout = None ):
1313
- """ Sets driver control to the specified browser frame. """
1326
+ """
1327
+ Wait for an iframe to appear, and switch to it. This should be
1328
+ usable as a drop-in replacement for driver.switch_to.frame().
1329
+ @Params
1330
+ frame - the frame element, name, id, index, or selector
1331
+ timeout - the time to wait for the alert in seconds
1332
+ """
1314
1333
if not timeout :
1315
1334
timeout = settings .SMALL_TIMEOUT
1316
1335
if self .timeout_multiplier and timeout == settings .SMALL_TIMEOUT :
0 commit comments