java - 使用 ChromeDriver 下载文件

标签 java google-chrome selenium download

我有一个项目需要在 ChromeDriver 中下载一个音频文件。这里的行为与常规 Chrome 不同,如果我访问 URL,它会自动开始下载文件。如果我在 ChromeDriver 中手动执行相同的操作,它将不会下载该文件。

我尝试了 chrome 选项/首选项的不同配置。我还找到了适用于旧版 chrome 的选项,但这些选项不再有效。

这是我找到的更好的资源之一,但它仍然没有用,即使他们更新了博客文章 https://dkage.wordpress.com/2012/03/10/mid-air-trick-make-selenium-download-files/

当我尝试使用他的解决方案时,我的 chromedriver 以一种非 chrome 风格的方式突然崩溃。它就这样消失了。不是您通常期望的“出了点问题”页面。我最终导致 Java 无法找到我的 session ,因为它已停止存在。

有没有人在 Chrome 中通过 Selenium webdriver 成功下载文件?如果我需要使用其他浏览器,我可以。

我目前使用的是 Chrome Canary。

最佳答案

我也遇到了同样的问题。一种可能有效的解决方案是使用另一个能够在浏览器之外运行的库。我发现这些 stackoverflow 帖子讨论了这个问题:

https://sqa.stackexchange.com/questions/2197/how-to-download-a-file-using-seleniums-webdriver

它包含这篇博文,可以为您提供一些建议。 https://blog.codecentric.de/en/2010/07/file-downloads-with-selenium-mission-impossible/

  1. Window automation

The first approach smells like “brute force”: when searching the net for a solution to the problem, you easily end up with suggestions, to control the native window with some window automation software like AutoIt. Means you have to prepare AutoIt such, that it waits for any browser download dialog, the point at which Selenium is giving up, takes control of the window, saves the file, and closes the window. After that Selenium can continue as usual.

This might eventually work, but I found it to be techical overkill. And as it turned out, there was a much simpler solution to the problem.

  1. Change the browsers default behaviour

The second possibility is to change the default behaviour of the browser. When clicking on a PDF for example, the browser should not open a dialog and ask the user what to do with the file, but rather save it without comments and questions in a predefined directory. To accomplish that, a file download has to be initiated manually, saved to disk and marked as the default behaviour for these file types from now on.

Well, that could work. You “only” have to assure that all developers, hudson instances, etc. share the same browser profile. And depending on the amount of different file types, that could be some manual work.

  1. Direct download

Taking a step back, why do we want to download the file with Selenium in the first place? Wouldn’t it be much cooler, to download the file without Selenium, but rather with wget? You would have solved the second problem as you go. Seems a good idea, since wget is not only available for Linux but also for Windows.

Problem solved? Not quite: what about files, that are not freely accessible? What, when I first need to create some state with Selenium in order to access a generated file? The solution seems ok for public files, but is not applicable for all situations.

关于java - 使用 ChromeDriver 下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45673437/

相关文章:

ruby-on-rails - 如何在 Ruby 中使用 Selenium webdriver 截取警报的屏幕截图?

java - 如何在 Jenkins 中配置 Java 的 Checkstyle 配置

java - 循环 e​​clipse 代码模板中的字段

java - 找不到字符串字段的 validator

google-chrome - 在 Chrome 中模拟 session 超时/过期

python - Gmail 是如何做到的?

javascript - 属性错误 : 'WebDriver' object has no attribute 'manage' 的问题

java - 与 IBM SBT 的服务器到服务器通信

jquery/css 背景更改未显示在 Chrome 中

javascript - 点击Selenium on Sauce 发送后,Firefox 未完成页面加载