Upload & Download File in Web Driver

0


For file uploading we use sendkeys() and for file downloading, we will use click() under hyperlink or button, but if we want to download on specific folder then we can use wget.exe file.
WebDriver Provide sendkeys() to set path of the current file.
no extra code is required for file uploading.
Example of Web Driver File Upload:-
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class UploadExample {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "c://chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://demo.guru99.com/test/upload/");
WebElement ele = driver.findElement(By.name("uploadfile_0"));
ele.sendKeys("c://abc.txt");
WebElement chk = driver.findElement(By.name("terms"));
chk.click();
WebElement btn = driver.findElement(By.id("submitbutton"));
btn.click();
}
}
Download in Web Driver:-
to download data from the file we will use wget.exe, this file should be present in the system, it will provide the destination path for downloading.
download using this link:-
https://eternallybored.org/misc/wget/
package scs;
import java.io.IOException;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class DownloadExample {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "c://chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://demo.guru99.com/test/yahoo.html");
WebElement downloadButton = driver.findElement(By
        .id("messenger-download"));
        String sourceLocation = downloadButton.getAttribute("href");
        String wget_command = "cmd /c C:\\wget.exe -P C: --no-check-certificate " + sourceLocation;
        try {
        Process exec = Runtime.getRuntime().exec(wget_command);
        int exitVal = exec.waitFor();
        System.out.println("Exit value: " + exitVal);
        } catch (InterruptedException | IOException ex) {
        System.out.println(ex.toString());
        }
        driver.close();
        }
}
Another Example of Download File in Selenium Web Driver:-
package scs;
import java.util.HashMap;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class DownloadExampleNew {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver","c:\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://skpatro.github.io/demo/links");
driver.manage().window().maximize();
WebElement ele=driver.findElement(By.xpath("//*[@id='regform']/div[3]/a"));
String path = ele.getAttribute("href");
String wget_command = "cmd /c C:\\wget.exe -P F: --no-check-certificate " + path;
try
{
Process p = Runtime.getRuntime().exec(wget_command);
int res = p.exitValue();
System.out.print(res + path);
}
catch(Exception ex)
{
System.out.print(ex.getMessage().toString());
}
Thread.sleep(2000);
        driver.close();   
}
}

Post a Comment

0Comments

POST Answer of Questions and ASK to Doubt

Post a Comment (0)