@@ -1014,10 +1014,51 @@ def reset_window_size(self):
10141014 self .set_window_rect (x , y , width , height )
10151015 self .__add_light_pause ()
10161016
1017+ def switch_to_window (self , window ):
1018+ self .switch_to_tab (window )
1019+
1020+ def switch_to_newest_window (self ):
1021+ self .switch_to_tab (- 1 )
1022+
1023+ def switch_to_tab (self , tab ):
1024+ driver = self .driver
1025+ if hasattr (driver , "cdp_base" ):
1026+ driver = driver .cdp_base
1027+ if isinstance (tab , int ):
1028+ self .page = driver .tabs [tab ]
1029+ elif isinstance (tab , cdp_util .Tab ):
1030+ self .page = tab
1031+ else :
1032+ raise Exception ("`tab` must be an int or a Tab type!" )
1033+ self .bring_active_window_to_front ()
1034+
1035+ def switch_to_newest_tab (self ):
1036+ self .switch_to_tab (- 1 )
1037+
1038+ def close_active_tab (self ):
1039+ """Close the active tab.
1040+ The active tab is the one currenly controlled by CDP.
1041+ The active tab MIGHT NOT be the currently visible tab!
1042+ (If a page opens a new tab, the new tab WON'T be active)
1043+ To switch the active tab, call: sb.switch_to_tab(tab)"""
1044+ return self .loop .run_until_complete (self .page .close ())
1045+
1046+ def get_active_tab (self ):
1047+ """Return the active tab.
1048+ The active tab is the one currenly controlled by CDP.
1049+ The active tab MIGHT NOT be the currently visible tab!
1050+ (If a page opens a new tab, the new tab WON'T be active)
1051+ To switch the active tab, call: sb.switch_to_tab(tab)"""
1052+ return self .page
1053+
1054+ def get_tabs (self ):
1055+ driver = self .driver
1056+ if hasattr (driver , "cdp_base" ):
1057+ driver = driver .cdp_base
1058+ return driver .tabs
1059+
10171060 def get_window (self ):
1018- return self .loop .run_until_complete (
1019- self .page .get_window ()
1020- )
1061+ return self .loop .run_until_complete (self .page .get_window ())
10211062
10221063 def get_text (self , selector ):
10231064 return self .find_element (selector ).text_all
@@ -1211,14 +1252,10 @@ def get_gui_element_center(self, selector, timeout=None):
12111252 return ((e_x + e_width / 2.0 ) + 0.5 , (e_y + e_height / 2.0 ) + 0.5 )
12121253
12131254 def get_document (self ):
1214- return self .loop .run_until_complete (
1215- self .page .get_document ()
1216- )
1255+ return self .loop .run_until_complete (self .page .get_document ())
12171256
12181257 def get_flattened_document (self ):
1219- return self .loop .run_until_complete (
1220- self .page .get_flattened_document ()
1221- )
1258+ return self .loop .run_until_complete (self .page .get_flattened_document ())
12221259
12231260 def get_element_attributes (self , selector ):
12241261 selector = self .__convert_to_css_if_xpath (selector )
0 commit comments