Upload File in Selenium WebDriver using SendKeys

Profile picture for user arilio666

There is a method using 'SendKeys' which can upload files. In selenium, files can be easily uploaded using sendkeys. For this to work, make sure that the input element is visible.

Upload Practise Site
  • This is precisely how it should look for it to work flawlessly.
  • Within the sendkeys, the absolute path to the file has to be mentioned.

For demo, files will be uploaded on this practice site.

Import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import io.GitHub.bonigarcia.wdm.WebDriverManager;
public class FileUpload {
public static void main(String[] args) {
 ChromeDriver driver = new ChromeDriver();
 WebElement uploadButton = driver.findElement(By.xpath("//input[@name=\"files[]\"]"));
 WebElement startButton = driver.findElement(
   By.xpath("//tr[@class=\"template-upload fade image in\"]//button[@class=\"btn btn-primary start\"]"));
  • N.png is successfully uploaded using the sendkeys method in selenium.
  • Note that the browse button should not be clicked for this to work; instead, after locating the browse element doing senkeys on it with the path will upload the file.