java 用于保存文件的 Selenium Firefox 配置文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12759256/
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-10-31 10:05:53  来源:igfitidea点击:

Selenium firefox profile for saving a file

javafirefoxseleniumautomation

提问by ork

Possible Duplicate:
Access to file download dialog in Firefox

可能的重复:
访问 Firefox 中的文件下载对话框

I'm using selenium and firefox to download a file from the internet. When i'm trying to download the file, i'm getting the download box which asking me if i want to save the file or "open with". I want to save the file but not automatically (want to rename the file name), i want the browser will ask me where to save the file. This option of "always ask where to save files" at the firefox settings is selected and still.. when i'm running the script with selenium it is not asking me and saving the file. How can i set the firefox profile to do this? and where can i see the all firefox profiles? THanks for helpers.

我正在使用 selenium 和 firefox 从互联网下载文件。当我尝试下载文件时,我收到下载框,询问我是要保存文件还是“打开方式”。我想保存文件但不是自动保存(想重命名文件名),我希望浏览器会询问我保存文件的位置。在 Firefox 设置中选择了“总是询问保存文件的位置”这个选项,并且仍然......当我使用 selenium 运行脚本时,它不会询问我并保存文件。如何设置 Firefox 配置文件来执行此操作?我在哪里可以看到所有的 Firefox 配置文件?感谢帮助者。

回答by eugene.polschikov

Investigated a lil bit workaround of the problem. Would like to share what I found. Concerning automation browser dialog boxes using Selenium in general: There is no easy way to make Selenium download files because browsers use native dialogs for it which cannot be controlled by JavaScript, so you need some "hack".Check this

调查了这个问题的一点点解决方法。想分享我的发现。关于一般使用 Selenium 的自动化浏览器对话框: 没有简单的方法来制作 Selenium 下载文件,因为浏览器使用无法由 JavaScript 控制的本机对话框,因此您需要一些“hack”。检查这个

Concerning ffox browser settings in particular, you can configure Firefox to automatically start the download and save the filein a specific place.

特别是关于 ffox 浏览器设置,您可以将 Firefox 配置为自动开始下载并将文件保存在特定位置

Or use that:

或者使用那个:

    FirefoxProfile firefoxProfile = new FirefoxProfile();

    firefoxProfile.setPreference("browser.download.folderList",2);
    firefoxProfile.setPreference("browser.download.manager.showWhenStarting",false);
    firefoxProfile.setPreference("browser.download.dir","c:\downloads");
    firefoxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk","text/csv");

    WebDriver driver = new FirefoxDriver(firefoxProfile);//new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capability);

    driver.navigate().to("http://www.myfile.com/hey.csv");

Hope this works for you

希望这对你有用

回答by vishesh

The firefox profile which selenium uses is with the default options firefox comes with.You will have to set the option to ask for, where to save file, in the selenium code.

selenium 使用的 firefox 配置文件与 firefox 附带的默认选项一起使用。您必须在 selenium 代码中设置询问、保存文件的位置的选项。