【问题标题】:Using Selenium and Firefox version 40, how do i download a file?使用 Selenium 和 Firefox 40 版,我如何下载文件?
【发布时间】:2015-11-23 13:45:08
【问题描述】:

通过 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 文件是否已下载到驱动器。 p>

【问题讨论】:

标签: python selenium firefox download rselenium


【解决方案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

进一步了解参考:-
How to Download files using Selenium
Firefox Profile Settings in Selenium

【讨论】:

    【解决方案2】:

    您可以创建其他方法通过链接从 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)。 通过同样的方式,您可以从链接下载任何文件 =)

    【讨论】:

    • 我试图在我的原始帖子中避开这个答案——文件是在用户单击按钮时生成的。我没有办法抢先获得下载链接。 :(
    • @Staunch,点击按钮即可获取链接和下载。有什么问题?
    • 如何获得链接?这是一个javascript 方法和一个ajax 请求。
    猜你喜欢
    • 2019-08-30
    • 1970-01-01
    • 2019-02-05
    • 1970-01-01
    • 2020-08-10
    • 1970-01-01
    • 2018-05-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多