How to click on <input type = file> in browsers using Selenium Webdriver?
I am working on a file selection dialog using Selenium 2 - WebDriver. Believe it or not, my problem is not that OS-native-file-chooser. This part I can handle!
The problem is that Selenium correctly clicks the "Select File" button. Since the source html source is simple <input type='file'>, the browser determines how to display it as a field and button. As a result, the placement and naming of the button changes depending on the browser. This works for me in Chrome, but only because Chrome places the button in the leftmost alignment, and by default, Selenium clicks on it.
Any ideas? I don’t understand if an input of this type is really accessible from inside the DOM ...
The right way to upload a file to any OS is
- Find the item
<input type='file'>. You do not need to worry about different implementations and precise positioning. Just find an element like xpath//input[@type='file'] sendKeys()ortype()(or any other method writes text to elements in your language) the file path for this input element.
Java code example:
// 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 for every OS and browser in WebDriver.
Have exactly the same situation with the item <input type='file'>. In my case, it is created using ExtJS.
I do not know if you have resolved this issue or not, but let me provide my solution.
JavascriptExecutor executor = (JavascriptExecutor)getDriver();
executor.executeScript("arguments[0].click();", element);
sendKeys(), (), ActionBuilder . JavascriptExecutor .