Contents

    Guides

    Mastering File Upload Automation in Selenium

    Published on

    February 4, 2026
    Mastering File Upload Automation in Selenium

    Uploading files is a crucial part of many web applications, and automating this process is essential for thorough test coverage. Selenium WebDriver provides several methods to handle file uploads, ranging from directly sending file paths to input elements to using external tools for native dialogs.

    This article explores these techniques with practical examples, including real-world scenarios like uploading a GitHub profile picture and verifying uploads on plagiarism checkers, while highlighting the importance of testing on real devices for comprehensive validation.

    Understanding Selenium WebDriver

    Selenium WebDriver is a powerful API that enables automation of web browsers by simulating user interactions like clicking, typing, and navigation. It works through browser-specific drivers that act as intermediaries between the test scripts and the browsers, translating commands into browser actions. 

    With WebDriver, testers can interact with web elements identified using various locator strategies, execute JavaScript, manage cookies, and control browser sessions. Selenium WebDriver supports multiple programming languages and browsers, making it versatile for cross-platform test automation needs.

    Different Methods to Upload a File using Selenium

    There are several common methods to upload a file using Selenium WebDriver:

    • Using sendKeys() on File Input Elements: This is the simplest and most straightforward method. Selenium sends the absolute path of the file directly to an <input type="file"> element without triggering the native file dialog. This works when the file input element is accessible on the page.
    • Using OS-Level Automation Tools (Robot Class, AutoIt): When the file upload involves interacting with native operating system dialogs, Selenium alone cannot handle this. Tools like the Java Robot class or AutoIt scripts simulate keyboard and mouse actions to control the file chooser window and select files.
    • Remote WebDriver with FileDetector: For tests running on remote machines or cloud grids, Selenium’s FileDetector transfers files from the local machine to the remote environment before uploading, allowing seamless file upload in distributed setups.

    These methods can be combined or chosen based on the complexity of the web application’s file upload functionality and the testing environment requirements.

    Step-by-Step Guide to Upload a File Using Selenium

    Here’s how to upload a file using Selenium WebDriver:

    • Locate the File Input Element: Inspect the web page to find the <input type="file"> element responsible for file selection. Typically, this element is hidden or styled but accessible via attributes like id, name, or class.
    • Prepare the File Path: Store the absolute path of the file you want to upload in a string variable. Ensure the file path is correct and accessible from the test environment.
    • Send File Path to the Input Element: Use Selenium’s sendKeys() method to send the file path directly to the input element, simulating the action of selecting a file.
    • Trigger the Upload (If Required): Some web applications require clicking an “Upload” or “Submit” button after file selection. Automate this step by locating and clicking the appropriate button.
    • Verify the Upload Status: After initiating the upload, wait for confirmation such as a success message, preview display, or file list update. Use assertions to validate successful upload.
    • Handle Special Scenarios: If the file dialog is native (not an input element), use OS automation tools like the Robot class or AutoIt to simulate keyboard input and select the file.
    • Clean Up: Close or reset elements after test completion as needed to maintain a clean test state.

    Example: Uploading a Profile Picture on GitHub

    Here is an example of how to automate uploading a profile picture on GitHub using Selenium WebDriver:

    • Navigate to GitHub and Log In: Open the GitHub login page, enter your username and password, and submit to log in.
    • Go to Profile Settings: After login, navigate to your profile settings page where the profile picture can be updated.
    • Locate the File Input Element: Find the hidden file upload input element responsible for selecting the profile picture.
    • Send the Image File Path: Use sendKeys() to provide the absolute path of the image file you want to upload.
    • Save or Submit the Change: Click the “Save” or “Update” button to upload and set the new profile picture.
    • Verify Upload Success: Confirm that the profile picture is updated successfully by checking the presence of the new image or a success message.

    Sample C# Code Snippet

    driver.Navigate().GoToUrl("https://github.com/login");

    driver.FindElement(By.Id("login_field")).SendKeys("your_username");

    driver.FindElement(By.Id("password")).SendKeys("your_password");

    driver.FindElement(By.Name("commit")).Click();

    driver.Navigate().GoToUrl("https://github.com/settings/profile");

    // Locate hidden file input and upload image

    IWebElement uploadInput = driver.FindElement(By.Id("avatar-upload-input"));

    uploadInput.SendKeys(@"C:\path\to\profile_picture.jpg");

    // Click save button

    driver.FindElement(By.CssSelector("button.js-update-avatar-button")).Click();

    // Add waits/assertions here to verify successful upload

    This example demonstrates the key steps to automate profile picture upload on GitHub, showcasing the file input interaction and form submission using Selenium with C#.

    Real-World Use Case: Uploading Files for Plagiarism Checks

    A practical real-world use case of file upload automation with Selenium is in plagiarism detection platforms. For instance, when users upload documents to check for copied content, Selenium can automate this by locating the file input element on the plagiarism checker’s website and sending the file path through the sendKeys() method.

    This enables testers to validate the upload functionality, verify the processing of the document, and ensure that the plagiarism report is generated correctly. Automating such workflows improves reliability and efficiency in testing key features of educational and content verification web applications.

    Step-by-Step Automation Flow

    • Open the plagiarism checker site and locate the file input
    • Send the path of the document to upload
    • Trigger the upload/check action
    • Verify upload/scan completion message or results

    Sample C# Selenium Code

    // Visit the plagiarism checker upload page

    driver.Navigate().GoToUrl("https://plagiarismchecker.example.com");

    // Locate the file input element

    IWebElement fileInput = driver.FindElement(By.Id("document-upload-input"));

    // Provide the absolute path to the document

    fileInput.SendKeys(@"C:\Users\User\Documents\sample-document.docx");

    // Click the button to start the check (if necessary)

    driver.FindElement(By.Id("submit-upload")).Click();

    // Use an explicit wait to wait for results page or confirmation

    WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(15));

    IWebElement resultMessage = wait.Until(

        d => d.FindElement(By.Id("scan-results"))

    );

    // Assert the result or display status

    Assert.IsTrue(resultMessage.Displayed);

    Key Considerations

    • Always check the selector for the file input element and buttons as these differ across platforms.
    • For non-standard file dialogs (rare), you may need to use automation tools like AutoIt or Robot.
    • Use explicit waits to handle result render times.

    Advantages of Running Selenium Tests on Real Devices

    Running Selenium tests on real devices offers several critical advantages for ensuring application quality, performance, and reliability:

    • Accurate User Experience Validation: Real devices replicate genuine user environments, capturing hardware, OS, browser, and network nuances that cannot be fully emulated on virtual machines or simulators.​
    • Detection of Device-Specific Issues: Bugs related to rendering, touch interactions, file pickers, and performance often surface only on real devices during automated testing.​
    • Comprehensive Cross-Platform Coverage: Running tests on a variety of real devices ensures that your application behaves consistently across different manufacturers, OS versions, and browser types.
    • Better Testing of Mobile Features: Features like camera access, GPS, push notifications, and file uploads are best validated end-to-end on real hardware rather than simulated environments.​
    • Increased Reliability and Confidence: Results from real device tests offer higher trust for stakeholders, reducing production surprises and ensuring quality for global user bases.

    Conclusion

    Automating file uploads with Selenium streamlines web testing for real-world scenarios such as plagiarism detection and profile management. By leveraging techniques like sendKeys() on file input elements, testers can efficiently validate key application workflows. 

    Running these tests on real devices further enhances reliability, uncovering issues related to hardware, browser, and OS variations that emulators often miss. Real-device testing ensures complete coverage, catching device-specific bugs, validating mobile file pickers, and verifying user experience across platforms.​

    To maximize confidence in your automation efforts, integrating Selenium scripts with cloud-based platforms like BrowserStack Automate is highly recommended. 

    BrowserStack Automate provides scalable access to thousands of real browsers and devices, allowing you to execute tests in genuine user environments without the hassle of managing physical devices. 

    This seamless integration improves test accuracy, reduces maintenance overhead, and accelerates feedback for every deployment.​

    Run Selenium Tests on Cloud

    Data-rich bug reports loved by everyone

    Get visual proof, steps to reproduce and technical logs with one click

    Make bug reporting 50% faster and 100% less painful

    Rating LogosStars
    4.6
    |
    Category leader

    Liked the article? Spread the word

    Put your knowledge to practice

    Try Bird on your next bug - you’ll love it

    “Game changer”

    Julie, Head of QA

    star-ratingstar-ratingstar-ratingstar-ratingstar-rating

    Overall rating: 4.7/5

    Try Bird later, from your desktop