How to Upload File in Selenium?
Related Product On This Page Introduction to Selenium WebDriverFebruary 17, 2026 · 10 min read · Tool Comparison
File upload is a common task when automatize websites. Many e-commerce and former related websites command file uploads, and in such scenarios, Selenium plays a crucial office in automating this operation. When Selenium alone can not handle these cases, tools like Java ’ s Robot class or AutoIt can be used to manage system-level file dialogs. Methods to Upload a File using Selenium? This guide report the different methods to upload a file using Selenium, codification examples for each and why test on existent devices. Selenium is an open-source creature that is used for It supply a single interface that permit the writing of test scripts in programming languages like Ruby, Java, NodeJS, PHP, Perl, Python, and C #, among others. Read More: ? Here are some of the methods to upload a file using Selenium: 1. sendKeys () method:The simplest and most reliable way to upload a file in Selenium is using the sendKeys () method. This is the built-in feature that Selenium provides, and hence it should be favour over former method as the syntax is very straightforward. This method works only for input HTML elements with the type attribute as file. 2. Robot class:The Robot Class in Java is a built-in utility that assist automate keyboard and shiner interactions at the operating system level. While Selenium is primarily project for web-based automation, it can not directly handle system pop-ups, file upload dialog, or native OS interactions. The Robot Class fills this gap by sham existent user actions, such as urge keys, move the mouse, snap button, etc. 3. AutoIt:AutoIt is a third-party freeware scripting tool project to automate Windows GUI interactions such as aboriginal file dialogs and popups. It can be incorporate with Selenium to handle tasks outside the browser, using keystroke, shiner actions, and window control, activeness that Selenium alone can not perform. Follow the steps below to integrate AutoIt with Selenium tests to upload a file. SendKeys example: Robot class example: For autonomous testing across multiple user personas, check out SUSATest — it explores your app like 10 different real users. Below program will assist you realize how to upload file upload in Selenium apply theRobot class: Auto It example: While there are multiple approaches to upload a file in Selenium, this clause will describe the activeness using. It is better to read the process through a couple of real-world examples. Assume that you are setting up your GitHub account for the first time and wish to finishprofile settings. One of the feature is to upload your profile picture. Let ’ s see how to do that. The snapshot below show the page to which the exploiter must upload a picture. Once you tick the Edit button, the website will grant you to upload a picture, as shown below. The task here is to locate both these elements using and a file upload method to upload a picture. Also Read: Code Example to Upload File in Selenium On accomplish the code above, a profile picture will be upload into the GitHub history. Read More: In this scenario, the user wants to upload a file to check whether the take content is lift. Refer to the snapshot below to upload a papers. This example uses a link text locater to locate the element and pen the accomplished broadcast. Code Example: On executing the above code, the contents of the file will be uploaded as shown below: As demonstrated above, uploading a file with Selenium is comparatively uncomplicated. Yet, since it is a standard user action, it must be tested repeatedly to ascertain that the site offers a seamless user experience. Thus, testers demand noesis of this procedure as a fundamental part of their repertory. Different device and browser provide web pages differently. To check that a given covering behaves uniformly across different pond of devices and browsers, it is advisable to prove it on the real devices and browsers with a real environment set up. Testing in real device and browsers ensures a high-quality user experience and compatibility across different platforms, and it validates the application & # 8217; s performance and security in existent time conditions. simplifies by offering cross-browser and cross-platform support, real-device examination, and the power to run tests at scale in a cloud-based environment. The following are some features provided by BrowserStack. 1. : Parallel testing is paramount in reducing overall execution time as it let the testers fulfil the scripts simultaneously on multiple devices/ browsers. Automate program provision this feature by giving access to latest devices and browsers for the testers to test their applications. 2. Real devices and browsers: Testing on emulators and simulator can be easy, however it may not yield accurate test result at all multiplication with respect to functional and even non-functional testing such as coating ’ s execution. 3. Dedicated Dashboard: Running Selenium test causa on the Automate product create a elaborated study, which can be pertain to manage and monitor the automation testing activities. It includes an overview of the screen status as Pass/Fail/Pending with all the environment details such as device gens, OS adaptation, Platform, browser name, browser version, test executing time and duration, screenshots, etc. 4. Custom Reports with Artifacts:In Automate, impost reports can be generated to provide detailed and customized reports for the automated test execution. It includes a vast compass of test data such as test performance condition, device and browser configurations, test duration, picture recording, screenshots, etc. 5. Easy Integration with CI/CD Pipeline:Automate can be easily integrated with popular CI/CD tools such as Jenkins, TeamCity, TravisCI. The team can achieve faster delivery rhythm with great authority in the reliability, performance and compatibility of the application across different devices and platforms. Also Read: Note: In Selenium mechanisation, file uploads can be straightforward if the element is a standard& lt; input type= & # 8217; file & # 8217; & gt;, where thesendKeys ()method can be used directly. However, if the stimulus is hidden or the upload is triggered by a custom UI element without a file input, you will require to rely on tool like the Java Robot course or AutoIt tool to handle aboriginal OS file dialog. Testing file upload functionality across different browsers and device is essential for ensuring a unseamed user experience. Selenium is one of the most widely used web mechanisation tools for cross-browser testing. To run these test efficiently, platforms like BrowserStack provide access to over 3,500 real devices and browser. With, examiner can validate their covering under real-world conditions. Sign in today to start automating your web coating tests. # Ask-and-Contributeabout this topic with our Discord community. Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts needed. Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts.Related Product
How to Upload File in Selenium?
Overview
Introduction to Selenium WebDriver
Methods to Upload a File using Selenium?
WebElement uploadFile = driver.findElement (By.cssSelector (`` input [type='file '] '')); uploadFile.sendKeys (`` Desktop/testfile.jpg '');
FileUpload.au3 ControlFocus (`` Open '', '' '', '' Edit1 '') ControlSetText (`` Open '', '' '', '' Edit1 '', '' & lt; path of the file to be uploaded & gt; '') ControlClick (`` Open '', '' '', '' Button1 '')
Runtime.getRuntime() .exec (`` D: \\Auto IT\\FileUpload.exe '');
importjava.util.List;importorg.openqa.selenium.By;importorg.openqa.selenium.WebDriver;importorg.openqa.selenium.WebElement;importorg.openqa.selenium.chrome.ChromeDriver;public classFileUploadUsingSendKeys {public static voidmain (String [] args) {// Create a new instance of the Chrome driver WebDriver driver =newChromeDriver ();try{// Maximize the browser window driver.manage () .window () .maximize (); // Navigate to the specified URL driver.get (`` https: //demoqa.com/upload-download ''); // Find the `` Choose File '' push element on the webpage WebElement chooseFile = driver.findElement (By.xpath(`` //input [@ id='uploadFile '] '')); chooseFile.sendKeys (`` & lt; path of the file to be uploaded & gt; ''); Thread.sleep(2000); List & lt; WebElement & gt; uploadedPath = driver.findElements (By.cssSelector(`` p # uploadedFilePath ''));if(uploadedPath.size () & gt; 0) {System.out.println (`` File Uploaded successfully '');}}catch(Exception e) {e.printStackTrace ();}finally{driver.quit ();}}}importjava.awt.Robot;importjava.awt.Toolkit;importjava.awt.datatransfer.StringSelection;importjava.awt.event.KeyEvent;importjava.util.List;importorg.openqa.selenium.By;importorg.openqa.selenium.WebDriver;importorg.openqa.selenium.WebElement;importorg.openqa.selenium.chrome.ChromeDriver;importorg.openqa.selenium.interactions.Actions;public classFileUploadUsingRobot {public static voidmain (String [] args) {// Create a new illustration of the Chrome driver WebDriver driver =newChromeDriver ();try{// Maximize the browser window driver.manage () .window () .maximize (); // Navigate to the specified URL driver.get (`` https: //demoqa.com/upload-download ''); // Find the `` Choose File '' button element on the webpage WebElement chooseFile = driver.findElement (By.xpath(`` //input [@ id='uploadFile '] '')); // Use Actions class to move to the `` Choose File '' button and pawl on it Actions ac =newActions (driver); ac.click (chooseFile) .perform (); // Initialize the Robot class Robot rb =newRobot (); // Wait for 2 seconds to ensure the file dialog is unfastened rb.delay (2000); // Copy the file path to the clipboard StringSelection ss =newStringSelection (`` & lt; itinerary of the file to be uploaded & gt; ''); Toolkit.getDefaultToolkit() .getSystemClipboard () .setContents (ss,null); rb.delay (1000); // Simulate pressing CTRL + V to glue the copied file way rb.keyPress (KeyEvent.VK_CONTROL); rb.keyPress (KeyEvent.VK_V); rb.keyRelease (KeyEvent.VK_CONTROL); rb.keyRelease (KeyEvent.VK_V); // Simulate pressing ENTER to confirm the file selections rb.keyPress (KeyEvent.VK_ENTER); rb.keyRelease (KeyEvent.VK_ENTER); Thread.sleep(2000); List & lt; WebElement & gt; uploadedPath= driver.findElements (By.cssSelector(`` p # uploadedFilePath '')); if (uploadedPath.size () & gt; 0) {System.out.println (`` File Uploaded successfully '');}} match (Exception e) {e.printStackTrace ();} ultimately {driver.quit ();}}}importjava.util.List;importorg.openqa.selenium.By;importorg.openqa.selenium.WebDriver;importorg.openqa.selenium.WebElement;importorg.openqa.selenium.chrome.ChromeDriver;importorg.openqa.selenium.interactions.Actions;public classFileUploadUsingAutoIt {public static voidchief (String [] args) {// Create a new instance of the Chrome driver WebDriver driver =newChromeDriver ();try{// Maximize the browser window driver.manage () .window () .maximize (); // Navigate to the specified URL driver.get (`` https: //demoqa.com/upload-download ''); // Find the `` Choose File '' button ingredient on the webpage WebElement chooseFile = driver.findElement (By.xpath(`` //input [@ id='uploadFile '] '')); // Use Actions class to move to the `` Choose File '' button and click on it Actions ac =newActions (driver); ac.click (chooseFile) .perform (); Thread.sleep(2000); Runtime.getRuntime() .exec (`` D: \\Auto IT\\FileUpload.exe ''); Thread.sleep(2000); List & lt; WebElement & gt; uploadedPath= driver.findElements (By.cssSelector(`` p # uploadedFilePath ''));if(uploadedPath.size () & gt; 0) {System.out.println (`` File Uploaded successfully '');}}catch(Exception e) {e.printStackTrace ();}finally{driver.quit ();}}}How to Upload a File Using Selenium? (with Examples)
Uploading a profile picture into the GitHub account
import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; importee org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.remote.RemoteWebDriver; import org.testng.annotations.AfterClass; significance org.testng.annotations.BeforeClass; import org.testng.annotations.Test; public class UploadFile {private RemoteWebDriver driver; @ BeforeClass public emptiness 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 () shed 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 picture ... '')) .click (); // Mention the own itinerary 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 gens can be of your selection if (driver.findElement (By.xpath (`` .//a [text () ='caps.jpeg '] '')) .isDisplayed ()) {assertTrue (true, `` Profile picture is Uploaded '');} else {assertTrue (false, `` Profile painting not Uploaded '');}} @ AfterClass populace void tearDown () throw Exception {driver.quit ();}}Uploading a File using Selenium to check plagiarized content
import java.io.IOException; import org.openqa.selenium.By; importation org.openqa.selenium.WebDriver; significance 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 ();}}Why run Selenium tests on real device?
Conclusion
Related Guides
Automate This With SUSA
Test Your App Autonomously