How to click on across browsers using Selenium Webdriver?

The proper way to upload a file on any OS is to

  1. Find the <input type="file"> element. You need not to worry about different implementations and exact positioning. Just find the element for example by xpath //input[@type="file"]
  2. sendKeys() or type() (or whatever method writes text into elements in your language) the path to file to that input element.

Sample Java code:

// find the input element
WebElement elem = driver.findElement(By.xpath("//input[@type="file"]"));
// 'type' the file location to it as it were a usual <input type="text" /> element
elem.sendKeys("C://path/To/File.jpg");

This works on every OS and browser in WebDriver.

Leave a Comment