
- Selenium - Home
- Selenium - Overview
- Selenium - Components
- Selenium - Automation Testing
- Selenium - Environment Setup
- Selenium - Remote Control
- Selenium - IDE Introduction
- Selenium - Features
- Selenium - Limitations
- Selenium - Installation
- Selenium - Creating Tests
- Selenium - Creating Script
- Selenium - Control Flow
- Selenium - Store Variables
- Selenium - Alerts & Popups
- Selenium - Selenese Commands
- Selenium - Actions Commands
- Selenium - Accessors Commands
- Selenium - Assertions Commands
- Selenium - Assert/Verify Methods
- Selenium - Locating Strategies
- Selenium - Script Debugging
- Selenium - Verification Points
- Selenium - Pattern Matching
- Selenium - JSON Data File
- Selenium - Browser Execution
- Selenium - User Extensions
- Selenium - Code Export
- Selenium - Emitting Code
- Selenium - JavaScript Functions
- Selenium - Plugins
- Selenium WebDriver Tutorial
- Selenium - Introduction
- Selenium WebDriver vs RC
- Selenium - Installation
- Selenium - First Test Script
- Selenium - Driver Sessions
- Selenium - Browser Options
- Selenium - Chrome Options
- Selenium - Edge Options
- Selenium - Firefox Options
- Selenium - Safari Options
- Selenium - Double Click
- Selenium - Right Click
- HTML Report in Python
- Handling Edit Boxes
- Selenium - Single Elements
- Selenium - Multiple Elements
- Selenium Web Elements
- Selenium - File Upload
- Selenium - Locator Strategies
- Selenium - Relative Locators
- Selenium - Finders
- Selenium - Find All Links
- Selenium - User Interactions
- Selenium - WebElement Commands
- Selenium - Browser Interactions
- Selenium - Browser Commands
- Selenium - Browser Navigation
- Selenium - Alerts & Popups
- Selenium - Handling Forms
- Selenium - Windows and Tabs
- Selenium - Handling Links
- Selenium - Input Boxes
- Selenium - Radio Button
- Selenium - Checkboxes
- Selenium - Dropdown Box
- Selenium - Handling IFrames
- Selenium - Handling Cookies
- Selenium - Date Time Picker
- Selenium - Dynamic Web Tables
- Selenium - Actions Class
- Selenium - Action Class
- Selenium - Keyboard Events
- Selenium - Key Up/Down
- Selenium - Copy and Paste
- Selenium - Handle Special Keys
- Selenium - Mouse Events
- Selenium - Drag and Drop
- Selenium - Pen Events
- Selenium - Scroll Operations
- Selenium - Waiting Strategies
- Selenium - Explicit/Implicit Wait
- Selenium - Support Features
- Selenium - Multi Select
- Selenium - Wait Support
- Selenium - Select Support
- Selenium - Color Support
- Selenium - ThreadGuard
- Selenium - Errors & Logging
- Selenium - Exception Handling
- Selenium - Miscellaneous
- Selenium - Handling Ajax Calls
- Selenium - JSON Data File
- Selenium - CSV Data File
- Selenium - Excel Data File
- Selenium - Cross Browser Testing
- Selenium - Multi Browser Testing
- Selenium - Multi Windows Testing
- Selenium - JavaScript Executor
- Selenium - Headless Execution
- Selenium - Capture Screenshots
- Selenium - Capture Videos
- Selenium - Page Object Model
- Selenium - Page Factory
- Selenium - Record & Playback
- Selenium - Frameworks
- Selenium - Browsing Context
- Selenium - DevTools
- Selenium Grid Tutorial
- Selenium - Overview
- Selenium - Architecture
- Selenium - Components
- Selenium - Configuration
- Selenium - Create Test Script
- Selenium - Test Execution
- Selenium - Endpoints
- Selenium - Customizing a Node
- Selenium Reporting Tools
- Selenium - Reporting Tools
- Selenium - TestNG
- Selenium - JUnit
- Selenium - Allure
- Selenium & other Technologies
- Selenium - Java Tutorial
- Selenium - Python Tutorial
- Selenium - C# Tutorial
- Selenium - Javascript Tutorial
- Selenium - Kotlin Tutorial
- Selenium - Ruby Tutorial
- Selenium - Maven & Jenkins
- Selenium - Database Testing
- Selenium - LogExpert Logging
- Selenium - Log4j Logging
- Selenium - Robot Framework
- Selenium - AutoIT
- Selenium - Flash Testing
- Selenium - Apache Ant
- Selenium - Github Tutorial
- Selenium - SoapUI
- Selenium - Cucumber
- Selenium - IntelliJ
- Selenium - XPath
- Selenium Miscellaneous Concepts
- Selenium - IE Driver
- Selenium - Automation Frameworks
- Selenium - Keyword Driven Framework
- Selenium - Data Driven Framework
- Selenium - Hybrid Driven Framework
- Selenium - SSL Certificate Error
- Selenium - Alternatives
Selenium WebDriver - File Upload
Selenium Webdriver can be used to automate tests requiring uploading files on a web page. In HTML terminology, a file upload element is identified by the tagname called input. Also, it should have a property type having the value as file.
Identify Upload Functionality in HTML
Right click on the page shown below, and click on the Inspect button in the Chrome browser. As a result, the complete HTML code for the entire page would be visible. For inspecting the Choose file button on the page, click on the left upward arrow, as highlighted below.

Once, we had clicked and pointed the arrow to that button beside the text Select a file, its HTML code was visible, reflecting the input tagname, and its type attribute having the value file.

<input id="uploadFile" type="file" lang="en" class="form-control-file">
To upload a file, we would use the sendKeys() method. The path of the file to be uploaded is passed as a parameter to that method.
Example
Let us take an example of the below page, where we would upload a file Picture.png by clicking the Choose file button. Once successfully uploaded, the file Picture.png should be visible on a web page.

Syntax
WebDriver driver = new ChromeDriver(); // identify element the element WebElement l = driver.findElement(By.xpath("value of xpath locator")); // getting file path to be uploaded File f = new File("./Picture.png"); System.out.println("Getting the file path to be uploaded: " + f.getAbsolutePath()); // uploading file with path of file uploaded m.sendKeys(f.getAbsolutePath());
Code Implementation
package org.example; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import java.io.File; import java.util.concurrent.TimeUnit; public class FilesUpload { public static void main(String[] args) throws InterruptedException { // Initiate the Webdriver WebDriver driver = new ChromeDriver(); // adding implicit wait of 15 secs driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS); // Opening the webpage where we will upload a file driver.get("https://www.tutorialspoint.com/selenium/practice/upload-download.php"); // identify element with xpath for file upload WebElement m = driver.findElement(By.xpath("//*[@id='uploadFile']")); // getting file path to be uploaded File f = new File("./Picture.png"); System.out.println("Getting the file path to be uploaded: " + f.getAbsolutePath()); // uploading file with path of file uploaded m.sendKeys(f.getAbsolutePath()); // check if file uploaded successfully if (m.getAttribute("value").equalsIgnoreCase("Picture.png")) { System.out.println("File uploaded successfully "); } else { System.out.println("File uploaded unsuccessfully "); } // Closing browser driver.quit(); } }
Dependencies added in pom.xml file −
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId> <artifactId>SeleniumJava</artifactId> <version>1.0-SNAPSHOT</version> <properties> <maven.compiler.source>16</maven.compiler.source> <maven.compiler.target>16</maven.compiler.target> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java --> <dependencies> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>4.11.0</version> </dependency> </dependencies> </project>
Output
Getting the file path to be uploaded: /Users/IdeaProjects/Selenium Java/./Picture.png File uploaded unsuccessfully Process finished with exit code 0
In the above example, we had obtained the path of the file to be uploaded with the message in the console - Getting the file path to be uploaded: /Users/IdeaProjects/Selenium Java/./Picture.png. Then we had successfully uploaded the file with the message in the console - File uploaded unsuccessfully.
Finally, the message Process finished with exit code 0 was received, signifying successful execution of the code.
Exception with Incorrect File Path
Let us take another example, where a wrong path of the file to be uploaded is passed as a parameter to the sendKeys() method(that is the file name to be uploaded is named as Picture1.png instead of Picture.png). In such a scenario, a file would not be uploaded, and an exception would be thrown.
Example
package org.example; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import java.io.File; import java.util.concurrent.TimeUnit; public class FilesExcUpload { public static void main(String[] args) throws InterruptedException { // Initiate the Webdriver WebDriver driver = new ChromeDriver(); // adding implicit wait of 15 secs driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS); // Opening the webpage where we will upload a file driver.get("https://www.tutorialspoint.com/selenium/practice/upload-download.php"); // identify element with xpath for file upload WebElement m = driver.findElement(By.xpath("//*[@id='uploadFile']")); // getting file path to be uploaded File f = new File("./Picture1.png"); System.out.println("Getting the file path to be uploaded: " + f.getAbsolutePath()); // uploading file with path of file uploaded m.sendKeys(f.getAbsolutePath()); // check if file uploaded successfully if (m.getAttribute("value").equalsIgnoreCase("Picture1.png")) { System.out.println("File uploaded successfully "); } else { System.out.println("File uploaded unsuccessfully "); } // Closing browser driver.quit(); } }
Output
Getting the file path to be uploaded: /Users/IdeaProjects/Selenium Java/./Picture1.png Exception in thread "main" org.openqa.selenium.InvalidArgumentException: invalid argument: File not found : /Users/IdeaProjects/Selenium Java/./Picture1.png Process finished with exit code 1
In the above example, we had obtained the path of the file to be uploaded with the message in the console - Getting the file path to be uploaded: /Users/IdeaProjects/Selenium Java/./Picture1.png. Then received the exception since an incorrect file path was sent for upload.
Finally, the message Process finished with exit code 1 was received, signifying unsuccessful execution of the code.
Conclusion
This concludes our comprehensive take on the tutorial on Selenium WebDriver File Upload. Weve started with describing identify upload functionality in HTML, and examples to illustrate how to upload files in Selenium Webdriver. This equips you with in-depth knowledge of the Selenium WebDriver File Upload. It is wise to keep practicing what youve learned and exploring others relevant to Selenium to deepen your understanding and expand your horizons.