Selenium Upload Multiple Files

Profile picture for user arilio666
  • In this article, we will see about uploading multiple files using selenium sendkeys and java robot class.
  • For the demo, we will be using this site for uploading files.

1. Using Send Keys


import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;
import java.awt.event.KeyEvent;
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) throws Exception {
 WebDriverManager.chromedriver().setup();
 ChromeDriver driver = new ChromeDriver();
 driver.get("http://autopract.com/selenium/upload1/");
 driver.manage().window().maximize();
 String path1 = "C:/Users/arili/git/Assignments/Selenium/target/H.png";
 String path2 = "C:/Users/arili/git/Assignments/Selenium/target/I.png";

 WebElement uploadButton = driver.findElement(By.xpath("//span[@class=\"btn btn-success fileinput-button\"]"));
 Thread.sleep(5000);
 uploadButton.sendKeys(path1 + "\n" + path2);
 WebElement startButton = driver.findElement(
   By.xpath("//tr[@class=\"template-upload fade image in\"]//button[@class=\"btn btn-primary start\"]"));
 startButton.click();
 Thread.sleep(10000);
 System.out.println("DONE!");
}
}
  • After fetching the locator for the 'add file' button, sendkey the paths stored in the variable path1 and path2 followed by "\n."
  • Doing this will upload multiple files.

2. Using Robot Class:


import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;
import java.awt.event.KeyEvent;
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) throws Exception {
 WebDriverManager.chromedriver().setup();
 ChromeDriver driver = new ChromeDriver();
 driver.get("http://autopract.com/selenium/upload1/");
 driver.manage().window().maximize();

 WebElement uploadButton = driver.findElement(By.xpath("//span[@class=\"btn btn-success fileinput-button\"]"));
 uploadButton.click();
 Thread.sleep(5000);
 Robot robot = new Robot();
 StringSelection filepath = new StringSelection("C:\\Users\\arili\\git\\Assignments\\Selenium\\target");
 Toolkit.getDefaultToolkit().getSystemClipboard().setContents(filepath, null);
 robot.keyPress(KeyEvent.VK_CONTROL);
 Thread.sleep(1000);
 robot.keyPress(KeyEvent.VK_V);
 Thread.sleep(1000);
 robot.keyRelease(KeyEvent.VK_V);
 robot.keyRelease(KeyEvent.VK_CONTROL);
 robot.keyPress(KeyEvent.VK_ENTER);
 robot.keyRelease(KeyEvent.VK_ENTER);
 StringSelection filepath2 = new StringSelection("\"G.png\" \"H.png\" \"I.png\" \"L.png\"");
 Toolkit.getDefaultToolkit().getSystemClipboard().setContents(filepath2, null);
 robot.keyPress(KeyEvent.VK_CONTROL);
 Thread.sleep(1000);
 robot.keyPress(KeyEvent.VK_V);
 Thread.sleep(1000);
 robot.keyRelease(KeyEvent.VK_V);
 robot.keyRelease(KeyEvent.VK_CONTROL);
 robot.keyPress(KeyEvent.VK_ENTER);
 robot.keyRelease(KeyEvent.VK_ENTER);
 Thread.sleep(10000);
 System.out.println("DONE!");
}
}
  • After clicking the 'Add Files Button,' an Open dialogue box of windows appears during this course robot class needs to perform.
  • Copying of the path in the path field.
  • Click on open.
  • Coming back to the perspective of the code after clicking the 'add files,' StringSelection is created to select the provided link for copying.
  • Using the toolkit facility, we are copying the path to the clipboard by passing the StringSelection variable into it.
  • Control 'V' and 'Enter' buttons are pressed and released using the robot class.
  • Once the file path is navigated, filenames are stored in StringSelection again, with multiple required files with double quotes around the names.
  • The toolkit is again used to copy it to the clipboard, and the same robot process is followed.
  • This will add multiple files to the site using the robot class.