How to Download a File using Selenium WebDriver II Automation with Selenium and Java

Опубликовано: 18 Октябрь 2024
на канале: Knowledge Share
248
15

In this session you will learn how to Download a file from different browsers

If you want to download the project/examples please download from below GitHub Repository
https://github.com/knowledgeshare-tec...

To Learn how to Upload a File please check below Session
   • How to Upload a File using Selenium W...  

To Learn Selenium step by step, please refer below Playlist
   • How to Upload a File using Selenium W...  


What is File Upload/Download

Uploading a File in to a webpage
Download a File from a webpage
Example :
(Upload) Profile Photo upload on any web page
(Download) any file from webpage

Note : File Download is varies from different browser to browser, and also depends on Operating Systems and Operating System versions itself

Different Methods to Upload / Download

Simple SendKeys to upload
Robot
Auto IT
*************************************
Example program :
*************************************
package com.seleniumbasics;

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class FileDowload_Example
{
public static void main(String args[]) throws AWTException
{
//File Download using Chrome Browser
/*
System.setProperty("webdriver.chrome.driver",
".\\drivers\\chromedriver.exe"); WebDriver driver=new ChromeDriver();
driver.navigate().to("http://the-internet.herokuapp.com/dow...");
driver.findElement(By.xpath("//a[text()='some-file.txt']")).click();
*/

// File Download using Firefox
System.setProperty("webdriver.gecko.driver",".\\drivers\\geckodriver.exe");
WebDriver driver=new FirefoxDriver();
driver.navigate().to("http://the-internet.herokuapp.com/dow...");
driver.findElement(By.xpath("//a[text()='some-file.txt']")).click();
// Pressing Down arrow to select Save File and then pressing Enter so that it will download the File
Robot robot=new Robot();
robot.keyPress(KeyEvent.VK_DOWN);
robot.keyRelease(KeyEvent.VK_DOWN);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);

}
}