How to download .docx file using Selenium webdriver in Java?

Deepak gupta picture Deepak gupta · Apr 21, 2015 · Viewed 11.9k times · Source

Can anyone let me know how to download a word file using selenium(java)? My below code is not working.

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.

Please let me know how to do this using Firefox.

Save Popup

Answer

Vaibhav picture Vaibhav · Apr 21, 2015

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.