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 ...

+23
source share
4 answers

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()or type()(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.

+39
source

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 .

+3

I tested the following element:

<INPUT style="WIDTH: 550px; background-color:yellow" type="file">

Results:

  • IE: doubleclick in any area of ​​the element, the "Select File" dialog will appear;
  • Firefox: click in any area of ​​the element, the "Select file" dialog will appear.
0
source

I am definitely facing the same problem when coding in Python 3.6, selenium.common.exceptions.InvalidArgumentException: Message: Unable to click elements. Please help me.

0
source

All Articles