How do i click on <input type=file> on any browser if i'm testing with Selenium Webdriver?

0 votes
I want to use the file-chooser option to upload files. The issue is not dealing with one particular OS's file-chooser dialog box.
The issue is making Selenium click on "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. Since, the placement & naming of button changes depending on the browser. I have solved it with Chrome. But only because Chrome places the button on the leftmost alignment and Selenium happens to click there by default. Any clue how to navigable to that dialog box using the DOM itself?
Apr 29, 2018 in Selenium by Martin
• 4,220 points
3,018 views

1 answer to this question.

0 votes

Does not matter which OS or which browser your dealing with. The rule is the same. You can use XPath element locator technique to locate the element. And once that is done, you can use sendKeys() with path to file, to upload the files. Code below.

// Locating XPath of the element(using Firebug)
WebElement elem = driver.findElement(By.xpath("//input[@type='file']"));
// Mention the path of file to do the upload
elem.sendKeys("C://path/To/File.txt")
answered Apr 29, 2018 by king_kenny
• 3,650 points

Related Questions In Selenium

0 votes
1 answer

How do i change the location where my file gets downloaded in Selenium Webdriver and firefox driver?

There are a couple of errors there. ...READ MORE

answered Apr 12, 2018 in Selenium by nsv999
• 5,090 points
1,510 views
0 votes
1 answer
0 votes
1 answer

How can I download the *.jar file from http:// seleniumhq.org using selenium WebDriver?

For Selenium Standalone Server use this: profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/java-archive"); and ...READ MORE

answered Apr 9, 2018 in Selenium by Martin
• 4,220 points
475 views
0 votes
1 answer

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,090 points
579 views
0 votes
1 answer
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,130 points
898 views
0 votes
1 answer

How to use such xpath to find web elements

It's much simpler to use: #MyDiv123 > div.super or ...READ MORE

answered Jun 14, 2018 in Selenium by Samarpit
• 5,130 points
569 views
0 votes
1 answer
0 votes
1 answer

How is Maven used with Selenium? Does it also do the testing?

You need to understand the basic processes ...READ MORE

answered Apr 13, 2018 in Selenium by king_kenny
• 3,650 points
259 views