How do I download a file using java Selenium WebDriver? - java

I'm using Selenium 2.21.0 with Java 6. How do I use the Selenium WebDriver API to download a file on a web page? That is, there is a link that causes the download of an Excel file to start. I would like to know how to initiate that download, determine when its finished, and then figure out where the file got downloaded to on my local system.

Once you click on any link to download a file, it depends on browsers behaviour like
Chrome Behaviour: It will start downloading the file by default, as soon as the user click on the link of any file.
IE Behaviour: IE displays a bar at the bottom of the window and displays the option to Save or Cancel the file download.
FireFox Behaviour: This will display an dialog box window and displays the option to Save or Cancel the file download.
So this can be achieved with the help of FireFox Profile.
And Before downloading any file you have to pass the MIME type of the file to FireFox Profile.
Some of the commonly used MIME types are:
Text File (.txt) – text/plain
PDF File (.pdf) – application/pdf
CSV File (.csv) – text/csv
MS Excel File (.xlsx) – application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
MS word File (.docx) – application/vnd.openxmlformats-officedocument.wordprocessingml.document
HERE IS THE CODE:
import org.openqa.selenium.By;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
public class DownloadFiles {
public static void main(String[] args) throws InterruptedException {
//Create FireFox Profile object
FirefoxProfile p = new FirefoxProfile();
//Set Location to store files after downloading.
profile.setPreference("browser.download.folderList", 2);
//Set preference not to file confirmation dialogue
profile.setPreference("browser.helperApps.neverAsk.saveToDisk",
"application/vnd.openxmlformats-
officedocument.spreadsheetml.sheet");
// Specify the local system path where to download
p.setPreference("browser.download.dir", "D:\\downloads");
// Pass Profile parameters in Firefox browser
FirefoxDriver driver = new FirefoxDriver(profile);
// Open APP to download application
driver.get("http://url");
// Click to download
driver.findElement(By.xpath("//html[#attribute='value']")).click();
Thread.sleep(5000);
driver.close();
Hope it will solve your queries. Happy coding.

I am specifically focusing of the Firefox browser you can use where the download option comes with pop up option when any download link is been click by the visitor. It is shows two buttons and two radio button that gives us the option of save and open the file directly without downloading it afterwards if we find the file is useful we can download it explicitly though the download icon on the above toolbar of the Firefox browser.
So you can perform the following steps
1) Clicking on the download link
WebDriver driver = new FirefoxDriver();
driver.findElement(By.linkText(“somelink”)).click();
The above code can help the WebDriver to identify the object and perform the action which is mentioned
2) Once the download link is clicked the firefox browser will pop up a download dialog box with multiple option
Like Save radio button, open radio button, ok button, cancel button inorder to use this
You can use the Robot class or the Keys in the WebDriver
Like
Robot r = new Robot();
r.KeyPress(KeyEvent.VK_TAB);
you can use as many time the above code to press the tab button
r.KeyRelease(KeyEvent.VK_TAB);
you have to release the pressed key
lastly perform the enter
r.KeyPress(KeyEvent.VK_ENTER);
That’s it it will help you to download the object when the download link is pressed
Hope it will help you

Related

Can't automatically choose file on <input type="file"> by using Selenium Webdriver

I ran webdriver, everything was great, until OS file browser pop-ups and webdriver can't interact with it so it can't automatically choose the file on file browser.
So how can I choose the file on file browser automatically?
Did you click an upload button?
Instead you'll want to find the input element with the type 'file'. Once you've located this, perform a send keys command with the file location.
E.g.
driver.findElement(By.xpath("//input[#type='file']")).sendKeys("File Location") ;

IE11 is failing to click on file browser button through selenium

Scenario:
In my application there is file browser button. Clicking this browser the file selection windows opens. From there we select our required file and click on upload.
Problem:
When we are doing this on Firefox and Chrome, there is no problem. But when we are doing this through IE11 that time the file browser button is being clicked by the script, but the file selection window is not poping up. I have checked the same manually on IE11, that time no problem is happening. But script opened is not responding on that specific button.
For ex- Through script I launched the IE and paused the script. If I am trying to click the file browser option manually on the same browser now, it would not respond with the file selection dialog window. But at the same time if I open a new browser manually and try clicking that browser option manually, then it would work.
Web Code:
There is a <iframe> object appears. Within this iframe the whole html page loads and under some nested <div> container this file browser option resides under <input id="UploadFile" class=*** type=*** ..> like wise.
Selenium Code:
WebElement ImportDef = driver.findElement(By.id("UploadFile"));
Thread.sleep(3000);
ImportDef.click();

Using Java Selenium, how can I make Firefox open downloaded text files in a new window or tab?

We want to run Selenium tests in a grid environment, and so won't have access to read any downloaded files from the remote browsers. To be able to check the contents of some downloaded files with dynamic content, we thought we could configure the FirefoxProfile so that downloaded files of certain types open in a new tab, and then read the contents directly from the browser.
The files are downloaded upon submission of a form, and altering any of the form attributes is not an option, since we do not want to change the user experience.
I have found information on how to set the download folder and specify direct downloading of specified file types, but not how to do "the opposite".
Since I am able to specify this behavior when manually downloading these files, I assume there is a way of doing it programatically too.
The FirefoxProfile we use is new FirefoxProfile(), and not an imported profile from the file system.
My last attempt was something like this:
FirefoxProfile firefoxProfile = new FirefoxProfile();
firefoxProfile.setPreference("browser.helperApps.neverAsk.openFile","text/csv, text/plain");
return firefoxProfile;
Get the File Path and Name then Open a new Instance of WebDriver or go to page in existing instance
Path file path
String filePath = "file:///C:/Users/userdir/Documents/Logs/error.log"
Using driver.get(path);
would open the txt file in a browser window.

Automating the action of downloading file and validating the status

My requirement, after cliking on the Export button, os save dialog appears where i need to select the download path and save it. Also i need to wait till the file download complete to make sure that the file is downloaded successfully.
I have used Selenium Webdriver till cliking on the Export button, I have handled the os save dialog for firefox browser by setting the firefox profile preferences to auto downlaod the file to the provided path. File starts downloading, but am stuck in verifying the status of file download. My file size varies and hence i cant wait for some time and check in the downlaod path, if the file exists. Also i will be not knowing the file name while exporting and #href attribute of Export button doesnot provide any url info.
can i use AutoIT? as am working on Windows platform.
I need to implement the above in IE, Chrome browsers as well. Pls let me know the possible solutions to handle the os save dialog and to verify the status of file download.
See if this helps: http://imobiliarerolabs.tumblr.com/post/64859016557/how-to-use-sikuli-when-automating-tests-with-selenium
It's Sikuli and can be used with Selenium.

File Upload Feature using Selenium and java

I need to automate the file upload feature in a web console and i am using selenium and java for this.
I tried multiple approaches but when i click on upload button and windows explorer gets opened, it stops there.
doesn't select any file...and gives error that there is no file..
I tried in firefox and chrome both but i am not able to solve this problem.
Then i also tried AutoIt tool. I downloaded it and made a script. trying to compile my script i am getting this error:
Code I'm using:
WebDriver driver = new FirefoxDriver();
driver.get("localhost:8080/page");
WebElement selectUploadApk = driver.findElement(By.id("id of upload button"));
selectUploadApk.click();
WebElement file = driver.findElement(By.xpath("//input[#type='file']"));
file .sendKeys("path of the file");
Error: Unable to execute upx.exe to compress stub file
File not found Exception
Please help
Thanx in advance
Megha
You don't need to click on the field to open the dialogue box.
Opening the dialogue box is what is 'breaking' your test.
Just send the keys directly to the input element, as you are, and then click on which ever button is the 'upload' button.
driver.findElement(By.xpath("//input[#type='file']")).sendKeys("/path/to/file");
driver.findElement(By.id("id of upload button")).click();

Categories