How to Upload a File using Selenium Webdriver


We often face the problem where we have to upload a file in Web Application. This can be easily achieved in Selenium. We are using sendKeys() method to set the value of the file to upload. In this tutorial, will see how to upload a document in Selenium.

Let’s explain this with the help of an example.

1) Launch new Browser and open “”
2) Locate the button “Choose File” by using xpath
3) Use sendkeys() to upload the file
4) After uploading the file, verify the message displayed on web page
5) Close the browser

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

public class UploadDemo {
    public static void main(String args[]) {
        System.setProperty("", "src\\test\\resources\\webdrivers\\window\\chromedriver.exe");
        // Initiate Chrome browser
        WebDriver driver = new ChromeDriver();
        // Maximize the browser
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        // Locating upload button
        WebElement upload = driver.findElement("uploadFile"));
        //Upload the file
        String Message = driver.findElement("uploadedFilePath")).getText();
        System.out.println("Message is :" + Message);
        // close the browser

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


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s