4

通过 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

但是,文件对话框仍然出现。我已经打开和关闭了很多切换字段,但是经过一番挖掘后,我发现prefs.jsSelenium 生成的默认 Firefox 配置文件的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 文件是否已下载到驱动器。

4

2 回答 2

1

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

进一步了解参考:-
如何
在 Selenium 中使用 Selenium Firefox Profile Settings下载文件

于 2017-01-04T06:51:12.427 回答
-1

您可以通过链接创建其他方法从 Internet 下载文件。

来自我的 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);
            }
        }

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

于 2015-08-28T21:13:18.007 回答