我有一个代码,我需要点击数字链接,继续打开新窗口,断言并返回主窗口并单击下一个链接。 并非所有链接都具有与我确定新窗口相同的“目标” 截至目前,我有3个可能的目标可用于所有链接。
除了以下内容之外,是否有任何简单的方法来实现此需求:
try:
driver.switch_to_window("windowName1")
except:
pass
try:
driver.switch_to_window("windowName2")
except:
pass
try:
driver.switch_to_window("windowName3")
except:
pass
self.assertIn('info', self.driver.title)
#go back to the main window
答案 0 :(得分:1)
迭代(窗口,目标)列表:
targets = [
['windowName1', 'target1'],
['windowName2', 'target2'],
['windowName3', 'target3'],
]
for window_name, target_name in targets:
try:
driver.switch_to_window(window_name)
except: # OR except InvalidSwitchToTargetException:
continue
# Do something with window_name, target_name ...