'''新开一个窗口'''
def open_new_window(self, url, t=4):
"""
:param url: 要访问的地址
:t: 等待时间
:return:
"""
bef_windows = self.dr.window_handles
js = 'window.open("%s");' % url
self.dr.execute_script(js)
self.switch_to_new_window(bef_windows) # 进入新打开的窗口
self.dr.maximize_window()
sleep(t)
'''切换到新窗口'''
def switch_to_new_window(self, new_wndow):
"""
:param bef_wndows: 新窗口的句柄
:return: 返回窗口句柄
"""
# 判断新窗口打开
WebDriverWait(self.dr, 15, 1).until(EC.new_window_is_opened(new_wndow))
all_windows = self.dr.window_handles
for handle in all_windows:
if handle not in self.window_list: # window_list 为新窗口打开之前已存在的窗口句柄列表
self.window_list.append(handle)
self.dr.switch_to.window(handle)
sleep(2)
return handle
原文:https://www.cnblogs.com/chenri/p/11605562.html