加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

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

发布时间:2020-12-20 13:52:42 所属栏目:Python 来源:网络整理
导读:通过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.s
通过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文件的方法.有谁有想法吗?

为了抢先任何人建议我只是下载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,# 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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读