下载存储的文件位置和处理使用带有 JAVA 的 selenium webdriver 下载弹出窗口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21255338/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 07:42:42  来源:igfitidea点击:

download file stored Location and handling Download Popup using selenium webdriver with JAVA

javaseleniumdownload

提问by Prabu

Please suggest an idea with following points implementations

请提出以下几点实现的想法

1.how to handle the Download popup in IE with Selenium Webdriver with JAVA? want to save that Xml file

1.如何在 IE 中使用 Selenium Webdriver 和 JAVA 处理下载弹出窗口? 想要保存那个 Xml 文件

2.how to store that xml file in different location by using JAVA?

2.如何使用JAVA将该xml文件存储在不同的位置?

Note: we will pass 'n' number of Inputs and each input have an xml file , required all the xml file download and save in different location

注意:我们将传递“n”个输入,每个输入都有一个 xml 文件,需要将所有 xml 文件下载并保存在不同的位置

回答by nilesh

I would recommend you NOT to automate file download using selenium. It's a trap you don't want to fall for. File download works differently in different browsers. People will recommend using AutoIT, but it only works for windows, so cross platform testing is not possible. Since you use Java bindings, you could use the Robot class, to move your mouse pointer to a certain location on the window and send a native click. In my experience this solution is really flaky. You don't know the exact location where you must click and with Robot you are blindly clicking on things. To add to this, when you are running tests on remote machine using selenium grid, things get even more difficult.

我建议您不要使用 selenium 自动下载文件。这是一个你不想落入的陷阱。文件下载在不同浏览器中的工作方式不同。人们会推荐使用 AutoIT,但它仅适用于 Windows,因此无法进行跨平台测试。由于您使用 Java 绑定,因此您可以使用 Robot 类将鼠标指针移动到窗口上的某个位置并发送本地点击。根据我的经验,这个解决方案真的很脆弱。您不知道必须单击的确切位置,而使用机器人,您会盲目地单击事物。此外,当您使用 selenium grid 在远程机器上运行测试时,事情变得更加困难。

So how do you download the file? Just get the underlying link to download the file available in your DOM and fire a GET request. Download the content if you want to validate the file. If you don't want to validate the content, just response code is fine. Hereis a fantastic blog with Java examples on how to download files in the background using http requests and detailed explanation on why downloading files using selenium is a bad idea.

那么如何下载文件呢?只需获取底层链接即可下载 DOM 中可用的文件并触发 GET 请求。如果要验证文件,请下载内容。如果您不想验证内容,只需响应代码即可。是一个很棒的博客,其中包含有关如何使用 http 请求在后台下载文件的 Java 示例以及有关为什么使用 selenium 下载文件是个坏主意的详细说明。

回答by praneel

Try setting below set of preferences in your DesiredCapabilityObject before initializing driver object -

在初始化驱动程序对象之前,尝试在 DesiredCapabilityObject 中设置以下首选项 -

File ffProfileFolder = new File("." + File.separator + "src" + File.separator
                    + "test" + File.separator + "resources" + File.separator + "FFProfiles" + File.separator + "AutoUser" + File.separator);
File workspacePath = new File(".."+File.separator);
String workspaceCanPath = workspacePath.getCanonicalPath();
String downloadDir = workspaceCanPath+File.separator+"Downloads";
OSInteractions.createDir(downloadDir);

profileAutoUser.setPreference("browser.download.manager.showWhenStarting",false);
profileAutoUser.setPreference("browser.download.dir",downloadDir);
profileAutoUser.setPreference("browser.download.defaultFolder",downloadDir);
profileAutoUser.setPreference("browser.download.lastDir",downloadDir);
profileAutoUser.setPreference("browser.download.folderList",2);
profileAutoUser.setPreference("browser.download.useDownloadDir",true);
profileAutoUser.setPreference("browser.helperApps.neverAsk.saveToDisk","application/octet-stream,application/msexcel");

DesiredCapabilities capFF = DesiredCapabilities.firefox();
capFF.setCapability(FirefoxDriver.PROFILE, profileAutoUser);


driver = new FirefoxDriver(profileAutoUser);

Note that this works for FF only.

请注意,这仅适用于 FF。