无法通过Selenium Python 3在Mozilla Firefox中关闭下载通知(成功下载后)3

发布于 2025-02-06 19:26:21 字数 1891 浏览 2 评论 0原文

硒版= 3.141, Mozilla Firefox版本:101.0.1 自动化框架:

成功下载文件后,我们希望关闭通知。

我能够下载该文件,但由于出现在Mozilla Firefox浏览器右上角的通知,因此无法执行任何操作。我想通过硒代码关闭该通知,但似乎不起作用。

已经尝试了以下解决方案:

      options = webdriver.FirefoxOptions()
    # options = FirefoxProfile()
      options.set_preference("dom.webnotifications.enabled", False)
      options.set_preference("dom.push.enabled", False)
      options.set_preference("browser.helperApps.neverAsk.saveToDisk","application/pptx, application/csv, application/ris, text/csv, image/png, application/pdf, text/html, text/plain, application/zip, application/x-zip, application/x-zip-compressed, application/download, application/octet-stream")

      options.set_preference("browser.download.panel.shown", False)

      options.set_preference("browser.download.folderList", 2)
      options.set_preference("browser.download.manager.showWhenStarting", False)
      options.set_preference("browser.download.dir", os.path.join(parent_folder, 'Downloads'))
      options.set_preference("browser.download.manager.closeWhenDone", True)
      options.set_preference("browser.download.manager.showAlertOnComplete", False)
    # options.add_argument("--headless")
    # options.headless = True
      web_driver = webdriver.Firefox(firefox_options=options,executable_path=GeckoDriverManager().install())
  • 解决方案2:

成功下载文件后,我们正在尝试按ESC键删除通知,但这似乎也无法正常工作

self.driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + Keys.ENTER)

ActionChains(self.driver).send_keys(Keys.ESCAPE).perform()
  • 解决方案3

也试图刷新屏幕,但仍在那里。

图像附在下面:

文件已成功下载,但无法通过Selenium Python关闭此通知。

Selenium Version = 3.141,
Mozilla Firefox Version : 101.0.1
Automation Framework : Pytest

After successfully downloading the file, we want the notification to be closed.

I am able to download the file, but unable to perform any operation because of the notification that appears at the right top of the mozilla firefox browser. I want to close that notification through selenium code but it seems not working.

Have tried following solutions:

      options = webdriver.FirefoxOptions()
    # options = FirefoxProfile()
      options.set_preference("dom.webnotifications.enabled", False)
      options.set_preference("dom.push.enabled", False)
      options.set_preference("browser.helperApps.neverAsk.saveToDisk","application/pptx, application/csv, application/ris, text/csv, image/png, application/pdf, text/html, text/plain, application/zip, application/x-zip, application/x-zip-compressed, application/download, application/octet-stream")

      options.set_preference("browser.download.panel.shown", False)

      options.set_preference("browser.download.folderList", 2)
      options.set_preference("browser.download.manager.showWhenStarting", False)
      options.set_preference("browser.download.dir", os.path.join(parent_folder, 'Downloads'))
      options.set_preference("browser.download.manager.closeWhenDone", True)
      options.set_preference("browser.download.manager.showAlertOnComplete", False)
    # options.add_argument("--headless")
    # options.headless = True
      web_driver = webdriver.Firefox(firefox_options=options,executable_path=GeckoDriverManager().install())
  • Solution 2:

After successfully downloading the file, we are trying to press ESC key to dismiss the notification but this also seems not working

self.driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + Keys.ENTER)

OR

ActionChains(self.driver).send_keys(Keys.ESCAPE).perform()
  • Solution 3

Tried to Refresh the screen as well, but notification is still there.

Image is attached below:

File is being successfully downloaded but unable to close this notification through selenium python.

Firefox Download Notification

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

独夜无伴 2025-02-13 19:26:21

您正在为PPTX使用不正确的MIME类型,因此可能会导致通知挂起,因此请在更新之后查看它是否解决了问题:

来自 application/pptx
to application/vnd.openxmlformats ofcecument.presentationml.presentationml.presentation

可以在此处找到有关MIME类型的更多信息: common Mime类型-http | MDN

更新

Mozilla引入了Firefox 97+的更改,其中之一是下载面板,行为是您所经历的。出于硒的目的,您可以使用以下选项阻止面板打开:

options.set_preference("browser.download.alwaysOpenPanel", False)

You are using the incorrect MIME type for pptx, so that may be causing the notification to hang around, so do following update to see if it resolves the issue:

from application/pptx
to application/vnd.openxmlformats-officedocument.presentationml.presentation

More info on MIME types can be found here: Common MIME types - HTTP | MDN

UPDATE

Mozilla has introduced changes from Firefox 97+, one of them is to the download panel and the behaviour is what you experienced. For selenium purposes, you can stop the panel from opening using the following option:

options.set_preference("browser.download.alwaysOpenPanel", False)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文