Site icon

Upload File using SendKeys in Selenium WebDriver

Upload file using SendKeys method in Selenium WebDriver:

Step 1 – Choose File Button :

WebElement uploadElenment = driver.findElement(By.id("Choosefile"));

Step 2 – Select File using sendKeys:

uploadElenment.sendKeys("C:\\xyz.txt");

Step 3 – Upload FIle :

driver.findElement(By.id("UploadFile")).click();

Program to upload a file using sendKeys method:

package TestCases;

import java.io.IOException;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;

public class FileUpload {

	@Test
	public void FileUploadTest() throws IOException {
		// Instantiation of driver object to launch browser
		System.setProperty("webdriver.chrome.driver", "C:\\work\\chromedriver.exe");
		WebDriver driver = new ChromeDriver();
		driver.get("https://makeinjava.com");

		// Locating "Choose file" button
		WebElement uploadElenment = driver.findElement(By.id("Choosefile"));
		// Pass the path of the file we want to upload using Sendkeys method
		uploadElenment.sendKeys("C:\\xyz.txt"); 
		// click on the "Upload button to upload file"
		driver.findElement(By.id("UploadFile")).click();
	}
}
Exit mobile version