繁体   English   中英

如何使用 Selenium Webdriver 在 Chrome 中关闭下载弹出窗口?

[英]How to close download popup in Chrome using Selenium Webdriver?

当您在 Chrome 中下载文件时,屏幕底部会显示一个弹出窗口,其中包含已下载文件的名称。 下载文件后,如何使用 Selenium Webdriver 和 Python 关闭它?

这应该可以解决问题:

from selenium import webdriver

driver = webdriver.Firefox() # or Chrome/Opera/Safari/Edge
# after
# your
# code
# to
# download
# file


### if the pop-up is a new window ###
driver.switch_to.window(driver.window_handles[1])
driver.close() # closes currently active window
driver.switch_to.window(driver.window_handles[0]) # switch back to main window

### if the pop-up is more like a text box overlaid on top of the page ###
driver.switch_to.alert.accept()

Note that if run driver.close() and you only have one window or tab open with Selenium, your Selenium session is discarded and if you try to run driver.close() again you will get an error like the following:

selenium.common.exceptions.NoSuchWindowException: Message: no such window: target window already closed
from unknown error: web view not found

为避免此问题,您可以使用driver.quit() ,即使您已经关闭了 Selenium session,它也不会引发异常。 更好的方法是使用with关键字的上下文管理器,因此即使在使用 Selenium 时遇到一些错误,您也不需要自己手动关闭它。

希望这有帮助!

巴西人! 对不起,谷歌翻译的英语我有同样的问题!

下载后我需要点击一个按钮,但我不能,因为按钮在我下载时显示的下载栏后面

为此我需要关闭它但使用driver.switch_to.alert.accept()我收到错误消息:没有这样的警报

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM