How to Upload a File using Selenium Webdriver

HOME

 ChromeOptions options = new ChromeOptions();
WebDriver driver = new ChromeDriver(options);
driver.manage().window().maximize();
driver.get("https://demoqa.com/upload-download");
 WebElement upload = driver.findElement(By.id("uploadFile"));

 //Upload the file
upload.sendKeys("C:\\Users\\Vibha\\Documents\\SeleniumTest.txt");

public class Upload_Demo {

    public static void main(String[] args) {

        ChromeOptions options = new ChromeOptions();
        WebDriver driver = new ChromeDriver(options);
        driver.manage().window().maximize();
        driver.get("https://demoqa.com/upload-download");

        // Locating upload button
        WebElement upload = driver.findElement(By.id("uploadFile"));

        //Upload the file
        upload.sendKeys("C:\\Users\\Vibha\\Documents\\SeleniumTest.txt");

        String Message = driver.findElement(By.id("uploadedFilePath")).getText();
        System.out.println("Message is :" + Message);

        // close the browser
        driver.close();
    }
}

The web page after uploading the file looks something like as shown below

Leave a comment