App & Browser Testing Made Easy

Give your users a seamless experience by testing on 3000+ real devices and browsers. Don't compromise with emulators and simulators

Home Guide How to Upload File in Selenium?

How to Upload File in 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 websites because it allows testers to automate valuable functions. This step-by-step guide will simplify how to upload file in Selenium using relevant code snippets and two examples.

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

Introduction to Selenium WebDriver

Selenium is an open-source tool that is used for browser automation 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 automates 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 cannot handle window components, but this drawback can be overcome using tools like Sikuli, Auto IT, etc.
  • Like Selenium RC, Selenium WebDriver supports multiple programming platforms to provide more comprehensive flexibility and requires knowing any one programming language.

How to Upload a File Using Selenium?

While there are multiple approaches to upload a file in 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 the 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 allow you to upload a picture, as shown below.

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

Code Example to Upload File in 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;
@BeforeClass
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);
}

@Test
public void UploadTest() throws Exception {
driver.get("https://github.com/settings/profile");
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");
}
}
@AfterClass
public void tearDown() throws Exception {
driver.quit();
}
}

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.

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();
driver.get("https://smallseotools.com/plagiarism-checker/");

//Locating upload filebutton
WebElement upload =driver.findElement(By.linkText("Upload a Document:( .tex, .txt, .doc, .docx, .odt, .pdf, .rtf )"));
upload.sendKeys("D:\\path\\File.txt"); 
driver.close();
}
}

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 relatively uncomplicated. Yet, since it is a standard user action, it must 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.

Note:

  • Selenium testing must be run on real devices for accuracy.
  • With access to 3000+ real browsers and devices on BrowserStack’s real device cloud, teams can also opt for parallel tests on a Cloud Selenium Grid to get faster results.
  • Detect bugs before users do by testing software in real user conditions with BrowserStack Automate.

Test on BrowserStack

Tags
Automation Testing Selenium Webdriver

Featured Articles

How to Read/Write Excel Data using Apache POI Selenium

How To Validate Text in PDF Files Using Selenium Automation

Curated for all your Testing Needs

Actionable Insights, Tips, & Tutorials delivered in your Inbox
By subscribing , you agree to our Privacy Policy.
thank you illustration

Thank you for Subscribing!

Expect a curated list of guides shortly.