In this blog, I am going to explain about "How to upload file using sendKeys() method of Selenium WebDriver?". To perform same I will use below HTML form which contains a file type input to choose a file from system.
Below is the example Java program to upload file using sendKeys() method of Selenium Webdriver. This example program is written for the above HTML form.
Below is the example Java program to upload file using sendKeys() method of Selenium Webdriver. This example program is written for the above HTML form.
Java Source code:
package com.helloselenium.selenium.test; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; public class UploadFileUsingSendKeys{ public static void main(String[] args) { WebDriver driver = new FirefoxDriver(); driver.manage().window().maximize(); driver.get("http://www.helloselenium.com/2015/03/how-to-upload-file-using-sendkeys.html"); WebElement fileInput = driver.findElement(By.name("uploadFileInput")); fileInput.sendKeys("path of the file to upload"); WebElement fileButton = driver.findElement(By.name("uploadFileButton")); fileButton.click(); driver.quit(); } }
Below are the line by line code explanation for above Java program:
package com.helloselenium.selenium.test;
Following code is the required packages for Selenium Webdriver to define the element locator type.
import org.openqa.selenium.By;
Following code is the required packages for Selenium Webdriver.
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
Following code is to initialize the Firefox driver.
WebDriver driver = new FirefoxDriver();
Following code is to open the hello selenium blog page in browser.
driver.get("http://www.helloselenium.com/2015/03/how-to-upload-file-using-sendkeys.html");
driver.navigate().to("http://www.helloselenium.com/2015/03/how-to-upload-file-using-sendkeys.html");
WebElement fileInput = driver.findElement(By.name("uploadFileInput"));
fileInput.sendKeys("path of the file to upload");
WebElement fileButton = driver.findElement(By.name("uploadFileButton"));
fileButton.click();
driver.quit();
driver.close();
0 Comments
What would you like to add in my list? I look forward to reading your comments below.