【问题标题】:Disable download pop up Firefox Selenium禁用下载弹出Firefox Selenium
【发布时间】:2021-11-21 03:34:53
【问题描述】:

我是 Selenium 的新手,我正在输入我的第一个脚本以从网页下载 .csv 文件。

问题是,当我单击按钮下载 .csv 文件时,会弹出一个下载窗口。如何自动将文件保存到文件夹? 我尝试了各种配置文件,但似乎无法让它们正常工作。

【问题讨论】:

  • 找到了解决方案fp = webdriver.FirefoxProfile() fp.set_preference("browser.preferences.instantApply",True) fp.set_preference("browser.helperApps.neverAsk.saveToDisk", "text/plain, application/octet-stream, application/binary, text/csv, application/csv, application/excel, text/comma-separated-values, text/xml, application/xml") fp.set_preference("browser.helperApps.alwaysAsk.force",False) fp.set_preference("browser.download.manager.showWhenStarting",False) fp.set_preference("browser.download.folderList",0) driver = webdriver.Firefox(firefox_profile=fp)

标签: python selenium firefox


【解决方案1】:

我在我的一个项目中使用以下配置

你可以拥有这个 firefox 配置文件,在 Python 中你可以这样:

profile = FirefoxProfile()
profile.set_preference("browser.download.panel.shown", False)
profile.set_preference("browser.helperApps.neverAsk.openFile","text/csv,application/vnd.ms-excel")
profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/msword, 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");
profile.set_preference("browser.download.manager.showWhenStarting", False);
profile.set_preference("browser.download.manager.alertOnEXEOpen", False);
profile.set_preference("browser.download.manager.focusWhenStarting", False);
profile.set_preference("browser.download.folderList", 2);
profile.set_preference("browser.download.useDownloadDir", True);
profile.set_preference("browser.helperApps.alwaysAsk.force", False);
profile.set_preference("browser.download.manager.alertOnEXEOpen", False);
profile.set_preference("browser.download.manager.closeWhenDone", True);
profile.set_preference("browser.download.manager.showAlertOnComplete", False);
profile.set_preference("browser.download.manager.useWindow", False);
profile.set_preference("services.sync.prefs.sync.browser.download.manager.showWhenStarting", False);
profile.set_preference("pdfjs.disabled", True);
profile.set_preference("browser.download.dir", "C:\\Users\\***\\****\\Desktop\\Automation")
driver = webdriver.Firefox(firefox_profile = profile, executable_path = "Full file path to gecko driver.exe")

【讨论】:

  • 我在 profile = FirefoxProfile() 处收到错误,这是一个未定义的名称。我应该怎么做才能让你的工作发挥作用?
  • 导入这个from selenium.webdriver import DesiredCapabilities, FirefoxProfile
  • 你看起来很博学,所以我不妨在这里问你。你知道我如何在无头模式下运行应用程序吗?每次我尝试,它仍然会打开浏览器。
  • 想通了,options = webdriver.FirefoxOptions() options.add_argument('-headless') driver = webdriver.Firefox(firefox_profile = profile, executable_path = 'C:\geckodriver.exe', options=options)
猜你喜欢
  • 2018-01-20
  • 2017-09-27
  • 1970-01-01
  • 2019-03-25
  • 1970-01-01
  • 2016-07-21
  • 1970-01-01
  • 2018-06-21
  • 1970-01-01
相关资源
最近更新 更多