How to Handle File Upload in Selenium

Faisal Khatri

Posted On: April 29, 2025

view count405166 Views

Read time10 Min Read

When performing website testing, validating file upload functionality is crucial as users need to upload documents and images, such as in job portals, eCommerce websites, and others.

Automated testing tools like Selenium provide effective solutions for this task. You can automate file upload in Selenium to ensure reliability and efficiency in testing the upload functionalities.

Why Selenium for Handling File Upload?

Selenium is an open-source collection of tools and libraries to automate interactions with web browsers. Being open-source and free, it has become a popular choice for automation testing in the global testing community. With the release of Selenium 4, Selenium has become W3C compliant. Selenium Manager has also been introduced, simplifying driver and browser management.

When handling file uploads, Selenium offers extensive browser support, robust automation capabilities, and flexibility for efficiently handling different file types and dynamic interactions. Instead of interacting with the file upload dialog, it provides a way to upload files without opening the dialog box.

Methods to Handle File Upload in Selenium

We have the following methods for file upload in Selenium WebDriver:

  • sendKeys() method
  • Robot class
  • AutoIt

File Upload in Selenium Using sendkeys()

It is always preferred to use the built-in features Selenium provides to upload files. The Selenium sendKeys() method is one such method in Selenium. It directly applies to input tags that have an attribute as type=’file’.

Here is an example of file upload in Selenium and Java using the sendKeys() method:

File Upload in Selenium Using Robot Class

Using the Robot class is another good option for uploading a file in Selenium. Robot class is an AWT class package in Java. This class helps automate windows based alerts, popups, or native screens. It is independent of the operating system.

Let’s take an example of the Filebin website that allows uploading and sharing files online. We will upload a file using the Robot class with Selenium on this website.

The following test script will allow us to file upload in Selenium using the Robot class:

This code will navigate to the FileBin website, locate the “Select files to upload” button, and perform a click operation on it. Next, the selectFile() method that accepts the file path as a String is called, and it will perform file selection actions using the Robot class.

Using the Robot class, ideally, the path is pasted in the filename field of the file upload window, and the Enter key is pressed, which helps select the file for upload.

File Upload in Selenium Using AutoIt

AutoIt is an open-source test automation tool that automates native windows-related popups. However, AutoIt is not recommended for file upload. It can be used as an option if nothing works.
We will upload a sample file using the FileBin website.

Here are the following steps to integrate AutoIt with Selenium tests to upload a file:

  1. Install AutoIt.
  2. Navigate to C:\Programs Files\AutoIt3\SciTE\ and run the SciTE.exe file. Add the following script to the editor:
  3. Save the file with a meaningful name like uploadfile.au3.
  4. Compile the uploadfile.au3 file by right-clicking on it and selecting the Compile Script(x64) option to convert the file to uploadfile.exe.
  5. Save the file with the .exe extension and run using the following command:

The following test script will allow file upload in Selenium using AutoIt.

The test will navigate the Filebin website and locate the “Select files to upload” button. Using the script Runtime.getRuntime().exec() method, the AutoIt upload script will be executed, and the upload action will be performed to select the file for upload.

Finally, an assertion will be performed to check that the filename is displayed in the table, ensuring the file was uploaded successfully.

In order to further enhance your Selenium automation testing, you can consider exploring AI testing agents like KaneAI.

KaneAI is a smart GenAI native test assistant for high-speed quality engineering teams. With its unique AI features for test authoring, management, and debugging, KaneAI allows teams to create and evolve complex tests using natural language.

Demo: Handling File Upload in Selenium

Let’s look at how to test handling file upload in Selenium. For this, we will use the cloud grid offered by LambdaTest to run tests. As a part of this blog, we will focus on leveraging the sendKeys() method to upload files and use the below test scenario for cloud grid execution.

LambdaTest is an AI-native test execution platform where you can test websites on various real browsers and operating systems. That way, you won’t have to worry about maintaining your Selenium Grid, as LambdaTest will provide you with an online Selenium Grid with zero downtime.

To get started with testing file uploads with Selenium, check out this guide on upload files using LambdaTest Selenium Grid.

Upload an Image

We will upload an image to verify the website successfully uploads a file and displays its name in the interface, confirming a successful upload.

Test Scenario:

  1. Navigate to the home page of the Filebin website.
  2. Select a sample file to upload and share.
  3. Assert file upload by verifying the filename displayed in the table after upload is complete.

Implementation:

To run the above test scenario, we will create a new test class in the same uploaddownloaddemo package and name it FileUploadTest.

Below is the test script used for demonstrating the file upload test scenario on the latest version of Chrome browser on the Windows 11 platform:

Code Walkthrough:

The testFileUpload() method will perform all the file upload steps. It will first navigate to the homepage of the Filebin website, locate the “Select files to upload” button, and upload the file using the sendKeys() method.

After the file is uploaded, a table is displayed with the uploaded file details. We will be locating the name of the file in the table to perform an assertion with the filename.

filebin test scenario

However, to run this test on the LambdaTest cloud grid, we will need to configure the platform, browser, and browser versions. It will also require other capabilities such as project name, build, and test name; these capabilities can be provided as browser options and finally used in the parameters while instantiating the RemoteWebDriver.

LambdaTest Username, Access Key, and grid URL are mandatorily required to be set in the configuration to execute tests on the cloud grid.

Additionally, the setFileDetector() method of the RemoteWebDriver class needs to be called, as it will help upload files to the cloud grid.

In the tearDown() method, there is an additional statement where the executeScript() method of the RemoteWebDriver class. It sets the test status, i.e., pass or fail, as per the test execution.

Test Execution:

Run your test for file upload in Selenium. After that, to view your test results, go to the LambdaTest Web Automation dashboard.

filebin file upload

You can also watch this video to learn how to upload and download files in Selenium WebDriver using different techniques.

Subscribe to the LambdaTest YouTube Channel and stay updated with the latest tutorials.

Uploading a Document to Check Plagiarism

We will now upload a file from plagiarism checker website to check for plagiarized content.

Test Scenario:

  1. Navigate to the Plagiarism Checker website.
  2. Select a sample file to upload.
  3. Verify that the word count displayed after file upload should be greater than 0.

Let’s create a new test, testUploadFileForPlagiarismCheck(), in the same test class as we would be running this test on the LambdaTest cloud grid as well.

As we have added this test in the same class, there is no need for any configuration changes to run this test on the LambdaTest cloud grid. The previously set configuration is sufficient for this test to run as well.

We will locate the attach file icon and use the sendKeys() method will enter the file upload path.

plag checket file upload

The file upload process takes some time, hence we are using the explicit wait condition to wait till the loader is invisible. This wait is essential to make the test wait till the time the loader disappears, and then the assertion statement would be executed.

We are using the assertTrue() method of TestNG to check that the wordsCount is greater than 0 after the file is uploaded successfully.

Test Execution:

The results of the test can be viewed on the LambdaTest Web Automation dashboard.

plagiarized test execution

Wrapping Up

For a website that allows users to upload files, we need to ensure they work seamlessly across all browsers and platforms.

Selenium testing can easily automate the download & upload file functionality of the web application. Although Selenium can help you execute test cases in local infrastructure, it is always recommended to go for a cloud-based Selenium Grid to save time and resources.

Frequently Asked Questions (FAQs)

How do you upload a file in Selenium?

To upload a file in Selenium, use the sendKeys() method on the file input element, specifying the file path as the argument.

How to upload a file without input in Selenium?

You can upload a file without an input element in Selenium by using JavaScript to simulate the file selection process or by directly interacting with the browser’s file dialog.

How to validate file upload in Selenium?

Validate file upload in Selenium by checking the presence of the uploaded file’s name or verifying any success message or element that appears after a successful upload.

Citations

Author Profile Author Profile Author Profile

Author’s Profile

Faisal Khatri

Faisal is a Software Testing Professional having 14+ years of experience in automation as well as manual testing. He is a QA, freelancer, blogger and open source contributor. He loves learning new tools and technologies and sharing his experience by writing blogs.

Blogs: 40



linkedintwitter

Test Your Web Or Mobile Apps On 3000+ Browsers

Signup for free