Header Ads

HOW TO UPLOAD FILE USING SENDKEYS() METHOD OF SELENIUM WEBDRIVER?

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.

File to upload:


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;
Above code is package structure for current program file.
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;
Following code is the required packages for Selenium Webelement.
import org.openqa.selenium.WebElement;
Following code is the required packages for Firefox browser.
import org.openqa.selenium.firefox.FirefoxDriver;
Above both required packages are available in downloaded selenium server jar file.
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");
You can also use the following code is to open the hello selenium blog page in browser.
driver.navigate().to("http://www.helloselenium.com/2015/03/how-to-upload-file-using-sendkeys.html");
Following code is to find the file input type webelement on the page.
WebElement fileInput = driver.findElement(By.name("uploadFileInput"));
Following code is to upload the file using sendKeys() method. Here you can replace "path of the file to upload" to path of the file like "C:\\test-data\\test.txt".
fileInput.sendKeys("path of the file to upload");
Following code is to find the button webelement on the page.
WebElement fileButton = driver.findElement(By.name("uploadFileButton"));
Following code is to click on the button.
fileButton.click();
Following code is to quit the webdriver instance.
driver.quit();
You can also use the following code is to close the webdriver instance.
driver.close();


No comments


What would you like to add in my list? I look forward to reading your comments below.



Powered by Blogger.