Explore Courses Blog Tutorials Interview Questions
0 votes
in DevOps and Agile by (19.7k points)

I'm working on dealing with a file-chooser dialog using Selenium 2 - WebDriver. Believe it or not, my problem is NOT dealing with the OS-native file-chooser. That part I can handle!

The problem is getting Selenium to properly click on the "Choose File" button. Since the original source HTML is simply <input type='file'>, the browser determines how to render it as a field and a button. As a result, the placement and naming of the button change depending on the browser. I've got it working in Chrome, but only because Chrome places the button on the leftmost alignment and Selenium happens to click there by default.

Any ideas? It's not clear to me if an input of this type is truly navigable from within the DOM anyway.

1 Answer

0 votes
by (62.9k points)
  1. Find the <input type='file'> element. You need not worry about different implementations and exact positioning. Just find the element for example by XPath //input[@type='file']
  2.  sendKeys() or type() (or whatever methodology writes text into elements in your language) the trail 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


This works on each OS and browser in WebDriver.

If you are interested to learn Selenium on a much deeper level and want to become a professional in the testing domain, check out Intellipaat’s Selenium training and certification!

Browse Categories