python - 使用 Selenium 和 Firefox 版本 40,如何下载文件?

标签 python selenium firefox download rselenium

通过 Selenium 下载文件的旧方法似乎不再有效。

我的代码是:

    fp = webdriver.FirefoxProfile()
    fp.set_preference("browser.download.dir", os.getcwd())
    fp.set_preference("browser.download.folderList", 2)
    fp.set_preference("browser.download.manager.showWhenStarting", False)
    fp.set_preference("browser.helperApps.neverAsk.saveToDisk",
                      "application/pdf")

    self.driver = webdriver.Firefox(firefox_profile=fp)
    self.longMessage = True

但是,文件对话框仍然出现。我已经做了很多打开和关闭字段的切换,但是经过一些挖掘我发现 Selenium 生成的默认 Firefox 配置文件的 prefs.js 文件和prefs.js 文件,其中我在下载对话框中手动选中了“从现在开始自动为此类文件执行此操作”。

不过,mimeTypes.rdf 文件确实发生了变化——具体而言,添加了以下行:

<RDF:Description RDF:about="urn:mimetype:handler:application/pdf"
               NC:alwaysAsk="false"
               NC:saveToDisk="true"
               NC:handleInternal="false">
<NC:externalApplication RDF:resource="urn:mimetype:externalApplication:application/pdf"/>

不过,我不知道在创建新的 Firefox 配置文件时设置自定义 mimeTypes.rdf 文件的方法。有人知道吗?

为了防止有人建议我只是 cURL 下载 URL,该文件是为用户生成的,我需要专门验证 .pdf 文件是否已下载到驱动器。

最佳答案

我是 R 用户,所以只需使用 RSelenium 在 R 中发布我的解决方案.如果您无法在 python 中进行相同的转换,请告诉我,我会提示相同的内容。

known_formats <- c("application/vnd.ms-excel","application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")


firefox_profile.me <- makeFirefoxProfile(list(marionette = TRUE,
                                              # this is for certificate issues [may be ignored]
                                              webdriver_accept_untrusted_certs = TRUE,
                                              webdriver_assume_untrusted_issuer = TRUE,
                                              # download related settings
                                              browser.download.folderList = 2L,
                                              browser.download.manager.showWhenStarting = FALSE,
                                              # put your path here but remember to give path like C:\\DirOfYourChoice and not C:\\DirOfYourChoice\\ [last \\ is not going to work]
                                              browser.download.dir = normalizePath("TestDL"),
                                              browser.helperApps.alwaysAsk.force = FALSE,
                                              browser.helperApps.neverAsk.openFile = paste0(known_formats, collapse = ","),
                                              browser.helperApps.neverAsk.saveToDisk = paste0(known_formats, collapse = ","),
                                              browser.download.manager.showWhenStarting = FALSE,
                                              # this is for marionette and related security
                                              "browser.tabs.remote.force-enable" = TRUE,
                                              pdfjs.disabled = TRUE))

remDr <- remoteDriver(remoteServerAddr = "localhost",
                      port = 4444,
                      browserName = "firefox",
                      extraCapabilities = firefox_profile.me)

remDr$open()

remDr$navigate("https://www.google.com/search?q=sample+xlsx")

remDr$findElement(using = "css selector", value = ".g:nth-child(1) a")$clickElement()

remDr$navigate("https://www.google.com/search?q=test+xls")

remDr$findElement(using = "css selector", value = ".g:nth-child(1) a")$clickElement()

和我一起工作很好 我正在使用

Firefox 50.1.0 [while I'm writing this post]
Selenium [3.0.1]
R [3.3.2 (2016-10-31)]

希望您能够将其移植到 Python。只需尝试复制 firefox 中的配置 makeFirefoxProfile

进一步理解引用:-
How to Download files using Selenium
Firefox Profile Settings in Selenium

关于python - 使用 Selenium 和 Firefox 版本 40,如何下载文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32278804/

相关文章:

python - Flask 没有从配置对象中获取 secret_key 配置

python - WebDriver异常: Message: unknown error: Chrome failed to start: exited abnormally with ChromeDriver Chrome and Selenium on debian server

html - 宽度小于800时显示滚动

CSS3 动画在 Firefox 中不起作用

javascript - javascriptExecutor 执行后无法执行处理步骤

c# - 使用 C# 将驱动程序导航到 selenium 中新打开的窗口

python - 如何使用 psycopg2.sql 将列列表或 *(所有列)传递给动态 SQL 查询

python - 提交表单不变时,Django formset cleaned_data 为空

python - Windows 8 (Anaconda) 上的 rpy2 安装错误

selenium - Selenium XPath text()-简单选择似乎不起作用