Course – LS – All

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:


1. Overview

Selenium WebDriver is a tool that can automate user interactions with web browsers for testing web applications. It automates processes such as file upload, getting input value, scrapping HTML content, etc.

In this tutorial, we'll explore how to upload a file using the sendKeys() method in Selenium.

2. Uploading Files Using sendKeys()

Simply put, file upload is a common feature in many web applications. However, file upload can be tricky to test with Selenium WebDriver because it involves interacting with the native file system of the operating system. To overcome this challenge we can use the sendKeys() method.

The sendKeys() method helps to simulate keyboard action. This method can send data as input to form elements in HTML.

sendKeys() accepts String as an argument and inserts it into a selected HTML element. It's an important method in automation testing. Common use cases include filling out a web form and searching for a specific item on a web page.

In this tutorial, we'll use the sendKeys() to fill a web form with a focus on uploading a file to a web page. Let's see an example for uploading an image file using sendKeys():

class FileUploadWebDriverUnitTest {
    private WebDriver driver;
    private static final String URL = "";
    private static final String INPUT_NAME = "file_upload";
    public void setUp() {
        driver = new FirefoxDriver();
    public void tearDown() {
    public void givenFileUploadPage_whenInputFilePath_thenFileUploadEndsWithFilename() {
        String filePath = System.getProperty("user.dir") + "/1688web.png"; 
        WebElement inputElement = driver.findElement(;
        WebElement submitButton = driver.findElement("http_submit"));
        String actualFilePath = inputElement.getAttribute("value");
        String fileName = filePath.substring(filePath.lastIndexOf("/") + 1);
        WebElement submitButton = driver.findElement("submit"));;

To begin with, we configure WebDriver to use Mozilla Firefox and write a teardown() method to close the browser when the test is done. Next, we declare a field called URL that holds the URL “ do=show&subdo=common&test=file_upload” where we can upload the file. Then we find the name attribute of the HTML input element that accepts the file. The image resides in the project's root directory.

Moreover, we create the WebElement instances to access the name attribute of the input field and the submit button. Also, we specify the file path and invoke sendKeys() method on inputElement to enter the image path in the input field.

Finally, we initiate the upload action by performing a mouse click on submitButton. We verify that the uploaded file has the same name and extension as the original file.

3. Conclusion

In this article, we learned how to upload files using Selenium WebDriver. Additionally, we use the sendKeys() method to send commands to an HTML input element. This skill is useful for automating web testing and interacting with different types of web elements.

As always, the complete source code for the example is available over on GitHub.

Course – LS – All

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

res – Junit (guide) (cat=Testing)
Inline Feedbacks
View all comments