Have a question?
Message sent Close
View Categories

How to upload File in Selenium?

📄
filename.js
import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.chrome.ChromeDriver;

public class FileUploadExample {

    public static void main(String[] args) {

        System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");

        WebDriver driver = new ChromeDriver();

        try {

            driver.get("https://demoqa.com/upload-download");

            WebElement uploadInput = driver.findElement(By.id("uploadFile"));
             
            // Give your absolute file path here
            uploadInput.sendKeys("C:\\path\\to\\your\\file.txt"); 

            // Optionally, verify upload
            WebElement uploadedPath = driver.findElement(By.id("uploadedFilePath"));

            System.out.println("Uploaded file path: " + uploadedPath.getText());

        } finally {

            driver.quit();

        }

    }

}