Search This Blog

WebDriver Provide sendkeys() to set path of 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();
        }


}





Post a Comment

If you have any doubt in programming or join online classes then you can contact us by comment .

Previous Post Next Post