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,320 points
10,813 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 13, 2018 in Selenium by nsv999
• 5,500 points
6,190 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
7,925 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,500 points
13,673 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,190 points
9,142 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,910 points
7,221 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 3, 2020 in Selenium by Sri
• 3,190 points
7,874 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 3, 2020 in Selenium by Sri
• 3,190 points
1,328 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP