如何解决使用 python 在 Selenium 中关闭弹出窗口
我正在尝试关闭弹出窗口,并且处理程序值不是固定的,每次再次运行时它们都会更改。我想拉弹出标题并使用 for 循环来 close() 弹出窗口,但弹出窗口没有标题。
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
在列表中.请帮助我找到关闭弹出窗口的解决方案。
解决方法
您必须遍历窗口句柄列表,并关闭不是父窗口句柄的那个。
driver.get('https://www.naukri.com/')
parent = driver.current_window_handle
uselessWindows = driver.window_handles
for winId in uselessWindows:
if winId != parent:
driver.switch_to.window(winId)
driver.close()
,
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])
弹出窗口是列表中的最后一个元素,你可以使用上面的代码,你必须切换回父级才能继续执行
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。