Back to Top

Uploading file through Selenium Webdriver

Updated 29 January 2019

Uploading file is a common scenario found in many website now a days. We can handle uploading file through selenium webdriver. Below is the screenshot of upload file from chrome browser :-

image1

Step by step execution of Code:-

1. First we will set the path of chrome driver :-

System.setProperty("webdriver.chrome.driver","/home/users/garima.pathak/Desktop/softwares/chromedriver");

2. Create a instance of ChromeDriver constructor.

WebDriver driver = new ChromeDriver();

Here, WebDriver is an interface, driver is a reference variable, ChromeDriver() is a constructor, new is  a keyword and new ChromeDriver () is an object.

Start your headless eCommerce
now.
Find out More

3. Give the navigation of the page in which we want to upload the file :-

driver.get("http:garima.com/membership/wp-admin/admin.php?page=mass-upload");

4. Now we will find upload button WebElement in the page.

WebElement uploadZip = driver.findElement(By.id("upload_zip"));

Here, findElement() finds a single web element and returns as a WebElement object.

5.  Now we will enter the file path onto the file-selection input field.

uploadZip.sendKeys("/home/users/garima.pathak/Desktop/example.zip");

Note :- WebDriver automatically enters the file path onto the file-selection text box of the <input type=”file”> element.

6. Finally, we will click on the upload button.

driver.findElement(By.name("submit_admin_csv")).click();

Below is the complete code :-

package automationFramework;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;


public class Upload {

	public static void main(String[] args) {
		System.setProperty("webdriver.chrome.driver","/home/users/garima.pathak/Desktop/softwares/chromedriver");
		WebDriver driver = new ChromeDriver();
		driver.get("http:garima.com/membership/wp-admin/admin.php?page=mass-upload");
	
		driver.manage().window().maximize();
		driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

                WebElement uploadZip = driver.findElement(By.id("upload_zip"));
                uploadZip.sendKeys("/home/users/garima.pathak/Desktop/example.zip");
        
                driver.findElement(By.name("submit_admin_csv")).click();
        
        
	}

}

After running this script,we will be able to upload the file successfully and we get a message similar to the one shown in below screenshot :-

image2

This is all about uploading files through Selenium WebDriver.

Thanks for reading this blog 🙂

Happy testing!!

 

. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


3 comments

  • surbhi nahta
    • Rajan Dimri (Moderator)
  • Naman
  • Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home