1

I have done a fair amount of googling and Selenium does not seem to natively support the clicking of the Save button in pop-up box. However I see a workaround which sets the browser preferences in this question -- Access to file download dialog in Firefox

The code given there is as below --

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");

Unfortunately, I cannot navigate to the file location, shown in the line below, since it redirects me to the login page of the website.

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

I have already logged-in to the website and log-in again does not make sense. Even for that I have to open a new window handler and switch to the new window(I don't think it will work anyway, since after login it directs to the previous page from which the pop-up occurs ). Is there some workaround to avoid this

driver.navigate().to

and somehow click on the Save button on the pop-up dialogue box?

Note1: When I manually open my browser, The pop-up window does not appear at all. The file starts to download when I click on the previous link even when I don't change firefox preferences manually to automatically download the file.

Note2: I Changed the Firefox preferences manually to automatically download the file types (XML in my case), but Selenium does not pick up this preference(expected behavior, I guess).

Community
  • 1
  • 1
Goku__
  • 940
  • 1
  • 12
  • 25
  • Have you tried to use your default profile with selenium? In that case there should be no pop-up window. For a test remove those `FirefoxProfile`lines from your code and run Selenium with command-line parameter: -Dwebdriver.firefox.profile=[name of your profile] – Würgspaß May 27 '15 at 09:42
  • Tried earlier. It does not work. – Goku__ May 27 '15 at 10:07
  • OK. Solved it. I didn't need the driver.navigate() ... The MIME type of the file was not correct. Found the correct mime type of the file from here -- http://watirmelon.com/2011/09/07/determining-file-mime-types-to-autosave-using-firefox-watir-webdriver/ – Goku__ Jun 02 '15 at 10:16
  • Selenium has br.cookies or use execute "return document.cookie" and cast as a String. Apache Http Components has CloseableHttpComponent. Use the client to download the file. You can use new URL(br.url()).getHost to get a host from selenium and specify a user agent and cookie in headers as needed in the client. Then just get the bytes and submit to a folder. You could even perform some verification for certain files. See my project https://github.com/asevans48/GoatGrazerSelenium. – Andrew Scott Evans Oct 05 '16 at 17:45
  • Also, phantomjs works – Andrew Scott Evans Oct 05 '16 at 17:48

1 Answers1

0

Simply put, it's not possible to interact with anything that's not part of the DOM. You can't make Selenium click on the "Save" button of the browser.

Most of the time it's not a good idea to download a file anyway (at least not when you're just doing UI testing).

Depending on what exactly you want to do with the file I'd suggest finding another way to test that behavior.

mhlz
  • 3,497
  • 2
  • 23
  • 35
  • Ummm, I am not testing anything. I am using selenium to login to a site, because I could not log-in to the site by other means. I need to download the file, parse the data from the XML file and store in database. The downloading is important. – Goku__ May 27 '15 at 09:59
  • If that's all that you're doing then you should look into just using URLConnections and HttpUrlConnections to login and download things. It will be a lot faster than Selenium and you can actually download things and do stuff with them. See more in [this question](http://stackoverflow.com/questions/2793150/using-java-net-urlconnection-to-fire-and-handle-http-requests) – mhlz May 27 '15 at 10:04
  • Actually, I tried that method, but the Page does not seem to accept the logins through HttpUrlConnections. I am able to login to other websites through HttpUrlConnections, but not to Amazon. Selenium worked out pretty well, to login, to navigate to the required page, but got blocked in the download part. – Goku__ Jun 02 '15 at 07:09