Home Guide How to Upload a File Using Selenium?

How to Upload a File Using Selenium?

By Neha Vaidya, Community Contributor -

One of the most common online activities people undertake is uploading files. Selenium is one of the best tools for automation testing of websites because it provides testers the ability to automate a wide variety of user functions. This article discusses how testers can use Selenium to upload a file.

Let’s begin by going over some fundamentals of Selenium WebDriver.

Introduction to Selenium WebDriver

Selenium is an open-source tool that automates web browsers. It provides a single interface that allows the writing of test scripts in programming languages like Ruby, Java, NodeJS, PHP, Perl, Python, and C#, among others.

Selenium WebDriver is a web framework that permits the execution of cross browser tests. This tool is used for automating web-based application testing to verify that it performs as expected.

It is an advancement over Selenium RC and overcomes some of RC’s limitations. Selenium WebDriver is not capable of handling window components, but this drawback can be overcome by using tools like Sikuli, Auto IT, etc.

How to Upload a File Using Selenium?

While there are multiple approaches to upload a file using Selenium, this article will describe the action using SendKeys method. It is best to understand the process through a couple of real-world examples.

Uploading a profile picture into GitHub account

Assume that you are setting up your GitHub account for the first time and wish to complete profile settings. One of the features is to upload your profile picture. Let’s see how to do that.

The snapshot below depicts the page to which the user must upload a picture.

How to Upload file in Selenium-1
Once you click the Edit button, the website will offer the option to upload a picture as shown below.

How to Upload file in Selenium-2
The task here is to locate both these elements using locators and use a file upload method to upload a picture.

Code Example to Upload File using Selenium

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class UploadFile {
private RemoteWebDriver driver;
public void setUp() throws Exception {
System.setProperty("webdriver.chrome.driver", "Path of the chrome driver");
driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

public void UploadTest() throws Exception {
Thread.sleep(2000);// Xpath for Edit button

Web element file = driver.findElement(By.xpath("//div[@class='position-absolute bg-gray-dark rounded-2 text-white px-2 py-1 left-0 bottom-0 ml-2 mb-2']")).click(); //link text locator for uploading a photo..
WebElement addFile = driver.findElement(By.linkText("Upload a photo...")).click();
// Mention the own path of the file location

// Add file method 
addFile.sendKeys("D:\Big Data on AWS\Images\caps.jpeg");// For setting a profile picture

driver.findElement(By.linkText("Set new profile picture")).click();
Thread.sleep(2000);// Image name can be of your choice

if(driver.findElement(By.xpath(".//a[text()='caps.jpeg']")).isDisplayed()) {
assertTrue(true, "Profile picture is Uploaded");
}else {
assertTrue(false, "Profile picture not Uploaded");
public void tearDown() throws Exception {

On executing the code above, a profile picture will be uploaded into the GitHub account.

Uploading a File using Selenium to check plagiarized content

In this scenario, the user wants to upload a file to check whether the selected content is plagiarized or not.

Refer to the snapshot below to upload a document. This example uses a link text locator to locate the element and write the complete program.

How to Upload file in Selenium-3
Code Example

import java.io.IOException;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class UploadFile {
public static void main(String[] args) throws IOException { 

//Instantiation of driver object to launch Firefox browser
System.setProperty("webdriver.gecko.driver", "Path of the gecko driver");WebDriver driver = new FirefoxDriver();

//Locating upload filebutton
WebElement upload =driver.findElement(By.linkText("Upload a Document:( .tex, .txt, .doc, .docx, .odt, .pdf, .rtf )"));

On executing the above code, the contents of the file will be uploaded as shown below:

How to Upload file in Selenium-4

As demonstrated above, uploading a file with Selenium is a relatively uncomplicated process. Yet, since it is a common user action, it needs to be tested repeatedly to ensure that the website offers a seamless user experience. Thus, testers require knowledge of this process as a fundamental part of their repertoire.

Automation Testing Selenium Selenium Webdriver

Featured Articles

Basic Commands in Selenium WebDriver

Data Driven Framework in Selenium

Top 5 Challenges Faced During Automation Selenium Testing

BrowserStack Logo Run Selenium Tests on 2000+ Browsers & Devices Get Started Free