我正在尝试关闭弹出窗口,并且处理程序值不固定,每次重新运行时它们都会更改。我想到了拉弹出标题并使用for循环来关闭弹出窗口,但是弹出窗口没有标题。
driver = webdriver.Chrome()
wait = WebDriverWait(driver, 10)
driver.get('https://www.naukri.com/')
# driver.maximize_window()
parent = driver.current_window_handle
print(f"This is parent window : {parent}")
uselessWindows = driver.window_handles
print(f"This has the parent window and also the two popup windows : {uselessWindows}")
输出是
This is parent window : CDwindow-196D8EFD5DD167AUTHE8935233FE0140 #String Value
This has the parent window and also the two popup window : ['CDwindow-196D8EFD5DD167AUTHE8935233FE0140', 'CDwindow-9E2058C9AADEWDHUIO4758B2F378AF577', 'CDwindow-94B59B8JGUTJ46578DHKDLNM24658C7C'] #List Value
每次“ CDwindow-”之后的值每次都更改时,-
由于current_window_handle
字符串中的和window_handles
列表中的,我无法使用设置差异。请帮助我提供关闭弹出窗口的解决方案。
parent = driver.current_window_handle
print(f"This is parent window : {parent}")
uselessWindows = driver.window_handles
print(
f"This has the parent window and also the two popup windows : {uselessWindows}")
driver.switch_to.window(uselessWindows[-1])
driver.close()
driver.switch_to.window(uselessWindows[0])
弹出窗口是您可以在上面的代码中使用的列表中的最后一个元素,您必须切换回父级才能继续执行
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句