Selenium Upload File Example

Upload files is a very general function on the web. But when you want to implement it in the selenium testing automation script, a dialog box will always pop up which selenium can not handle. This article will tell you 3 methods to use selenium webdriver to upload files on an Html web page.

1. Selenium Upload File In Source Code Directly Steps.

  1. Locate the Html upload file web element in selenium webdriver source code.
    WebElement element = driver.findElement("uploadFileInputBox"));
  2. Send the uploaded file’s absolute path ( as string ) to the upload file web element.
  3. Click the Html form submit button to submit the Html form.
    WebElement submitBtn = driver.findElement("uploadFileSubmitBtn"));;
  4. Create an eclipse Maven project, create a new java class com.dev2qa.webdriver.TestUploadFile and an Html file uploadFileTest.html.  Below is the maven project file structure.
    > tree /F ./
    │  pom.xml
    │  ├─main
    │  │  ├─java
    │  │  │  └─com
    │  │  │      └─dev2qa
    │  │  │          └─webdriver
    │  │  │              │
    │  │  │              │  uploadFileTest.html
  5. Below is the file source code.
    package com.dev2qa.webdriver;
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.firefox.FirefoxDriver;
    public class TestUploadFile {
        public static void testUploadFile()
            try {
                // Set the geckodriver.exe path to the system property webdriver.gecko.driver's value.
                System.setProperty("webdriver.gecko.driver", "D:\\Work\\Tool\\geckodriver-v0.29.1-win64\\geckodriver.exe");
                /* This is the WebDriver object used to manipulate web browsers.*/
                WebDriver driver;
                /* This page displays upload file web element, 
                 * this is just a fake one. You can specify your real page.*/
                String uploadFileDisplayUrl = "";
                // Initialize the WebDriver object use FirefoxDriver.
                driver = new FirefoxDriver();
                // Maximize the Firefox browser window.
                // Use Firefox web browser to load and display the upload file web page.
                // Get the upload file web element by it's name "uploadFileInputBox"
                WebElement element = driver.findElement("uploadFileInputBox"));
                //Input the uploaded file's absolute file path to the upload file web element as string use sendKeys() method.
                // Find the Html form submit button in the web page.
                WebElement submitBtn = driver.findElement("uploadFileSubmitBtn"));
                // Click the submit button to upload the file. 
                // Get the return message in the returned web page. 
                String checkText = driver.findElement("returnMessage")).getText();
                if("Your file has been uploaded successful".equalsIgnoreCase(checkText)) {
                    System.out.println("Upload file success.");
            }catch(InterruptedException ex) {
        public static void main(String[] args) {
  6. Below is the Html file uploadFileTest.html content.
    upload file html web page

    <!DOCTYPE html>
    <title>This is a upload file demo page.</title>
        <!--This is just the html form which include the upload file textbox -->
        <form enctype="multipart/form-data" action="" method="post"> 
          <p>Browse and upload file: </p>
          <input type="file" name="uploadFileInputBox">
          <!-- This is the form submit button -->
          <input type="submit" value="Upload File" name="uploadFileSubmitBtn">
  7. This method works only when the Html upload file input textbox is visible and enabled.
  8. You should not click the Choose File button for the upload file web element. If you click it, a windows dialog will pop up, this can not be handled by selenium.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.