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,340 points
8,130 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,710 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,520 points
4,175 views
0 votes
1 answer

How to check if an image is displayed on web page while working with Selenium WebDriver?

I've come across a similar situation before, where the image ...READ MORE

answered May 10, 2019 in Selenium by Surya
• 970 points
5,417 views
0 votes
2 answers

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,520 points
5,306 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

enable trusted connection  in internet explorer by ...READ MORE

answered Aug 31, 2020 in Selenium by Sri
• 3,050 points
5,988 views
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,890 points
3,707 views
0 votes
2 answers

How to use such xpath to find web elements

xpath are two types. 1) Absolute XPath:    /html/b ...READ MORE

answered Sep 2, 2020 in Selenium by Sri
• 3,050 points
4,495 views
0 votes
1 answer
0 votes
2 answers

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

Hi , Maven is a build tool . Why ...READ MORE

answered Sep 2, 2020 in Selenium by Sri
• 3,050 points
459 views