如何在 Java 中使用 Selenium webdriver 下载 .docx 文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29770599/
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
How to download .docx file using Selenium webdriver in Java?
提问by Deepak gupta
Can anyone let me know how to download a word file using selenium(java)? My below code is not working.
谁能告诉我如何使用 selenium(java) 下载 word 文件?我下面的代码不起作用。
FirefoxProfile prof = new FirefoxProfile();
prof.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/word");
When I click on 'download link or icon' in the page, it prompts a popup to save the download file (see image below) and I need to click on OK
button in the popup.
当我点击页面中的“下载链接或图标”时,它会提示一个弹出窗口来保存下载文件(见下图),我需要点击OK
弹出窗口中的按钮。
Please let me know how to do this using Firefox.
请让我知道如何使用 Firefox 执行此操作。
采纳答案by Luffy
You need to use ROBOT class for firing an ENTER Action event. In java if you want to fire any event you have to use Robot class for typing using programatically or firing events like ENTER and ESCAPE.
您需要使用 ROBOT 类来触发 ENTER Action 事件。在 Java 中,如果你想触发任何事件,你必须使用 Robot 类以编程方式输入或触发事件,如 ENTER 和 ESCAPE。
// Create object of Robot class
Robot object=new Robot();
// Press Enter
object.keyPress(KeyEvent.VK_ENTER);
// Release Enter
object.keyRelease(KeyEvent.VK_ENTER);
and for information regarding this you can use this link
有关这方面的信息,您可以使用此链接
回答by Vaibhav
Try this
试试这个
import java.awt.Robot;
And use
并使用
Robot r = new Robot();
r.keyPress(KeyEvent.VK_ENTER);
r.keyRelease(KeyEvent.VK_ENTER);
This will press Enter Programatically.
这将以编程方式按 Enter。
回答by Alex
Got it working using the following setup:
使用以下设置使其工作:
FirefoxOptions options = new FirefoxOptions();
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("browser.download.folderList", 2);
profile.setPreference("browser.download.dir", "C:\Windows\temp");
profile.setPreference("browser.download.useDownloadDir", true);
profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/vnd.openxmlformats-officedocument.wordprocessingml.document");
options.setProfile(profile);
driver = new FirefoxDriver(options);
More info about Preference settings can be found here: http://toolsqa.com/selenium-webdriver/how-to-download-files-using-selenium/.
有关首选项设置的更多信息,请访问:http: //toolsqa.com/selenium-webdriver/how-to-download-files-using-selenium/。