How to upload a file in Selenium with no text box?


We can upload a file in Selenium with no text box. This is achieved with the help of the sendKeys method. It is applied on the web element which performs the task of selecting the path of the file to be uploaded.

As we make an attempt to upload, we shall click on the Browse button. If we investigate the HTML code for this, we shall be able to locate the attribute type having the value file. Moreover, the file path to be uploaded should be accurate.

Example

Code Implementation.

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
public class FileUpload{
   public static void main(String[] args) {
      System.setProperty("webdriver.chrome.driver","C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");
      WebDriver driver = new ChromeDriver();
      String u = "https://www.tutorialspoint.com/selenium/selenium_automation_practice.htm";
      driver.get(u);
      driver.manage().timeouts().implicitlyWait(6, TimeUnit.SECONDS);
      // identify element
      WebElement m=driver.findElement(By.xpath("//input[@name='photo']"));
      // file selection field with file path
      m.sendKeys("C:\Users\ghs6kor\Tulips.jpg");
   }
}

Output

Updated on: 28-Dec-2020

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements