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

时间:2015-08-28 19:49:51

标签: 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文件的方法。有没有人有任何想法?

为了抢先任何人建议我只是下载URL,为用户生成文件,我需要专门验证.pdf文件是否已下载到驱动器。

2 个答案:

答案 0 :(得分:2)

我是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

答案 1 :(得分:-1)

您可以通过链接创建从互联网下载文件的其他方法。

来自我的c#代码示例:

public Bitmap Image
        {
            get
            {
                string webPath = Element.GetAttribute("src");

                if (webPath != string.Empty)
                {
                    try
                    {
                        System.Net.WebRequest request =
                            System.Net.WebRequest.Create(webPath);

                        System.Net.WebResponse response = request.GetResponse();

                        System.IO.Stream responseStream = response.GetResponseStream();

                        Bitmap bitmapImg = new Bitmap(responseStream);

                        return bitmapImg;
                    }
                    catch (System.Net.WebException)
                    {
                    }
                }

                return new Bitmap(1,1);
            }
        }

所以如你所见,在这段代码中我从image元素获取src属性并从浏览器下载它以获得绝对正确的位图图像(之后我可以将其保存到HDD)。 通过相同的方式,您可以从links =)

下载任何文件