使用 python 在 Selenium 中关闭弹出窗口 [英] Closing Popup windows in Selenium using python
问题描述
我正在尝试关闭弹出窗口,并且处理程序值不是固定的,每次再次运行时它们都会更改.我想拉弹出标题并使用 for 循环来 close() 弹出窗口,但弹出窗口没有标题.
I am trying to close popup windows, and the handler values are not fixed, they change every time when run again. I thought of pulling the pop title and using for loop to close() the popups but the popup didn't have a title.
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
在列表中.请帮助我找到关闭弹出窗口的解决方案.
Every time the value after "CDwindow -" changes every single time and I am unable to use set difference -
because the current_window_handle
is in the string and the window_handles
is in List. Kindly help me with a solution to close the popups.
推荐答案
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])
弹出窗口是列表中的最后一个元素,你可以使用上面的代码,你必须切换回父级才能继续执行
the pop up window is the last element in the list you can use above code , you have to switch back to parent to continue execution
这篇关于使用 python 在 Selenium 中关闭弹出窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!