Error with Selenium File Upload

0 votes

While uploading a file, i'm getting this error: "Element is not reachable by keyboard" Can you help me fix it? The only thing I want to do is to attach a PDF file. But as the cusror hovers, i'm unable to find the click or upload file.

Code is:-

WebElement uploadElement = driver.findElement(By.xpath("//*[@id=\"registerproduct\"]/div/div[4]/div/div/div/div[2]/div[2]/div/div/span/label"));
uploadElement.sendKeys("C:\\Users\\Bharat\\Downloads\\apmr-auction-detail-574.pdf");

The Html is:-

<div class="col s12">
 <div class="file-field input-field">
    <div class="">
         <input id="btn_myFileInput" onchange="checkimagetype()" name="productsheet" style="display:none;" type="file">
         <span class="attached sp_head">
          <label for="btn_myFileInput" class="gray-lite attach_circle left"> 
              <i class="fa fa-paperclip small"></i>
           </label>
         <span class="sp_head">
        Attach specification sheet</span>
          <span id="fileinput-msg"></span> </span>
    </div>
 </div>

Does anyone know a remedy for this?

Mar 30, 2018 in Selenium by Shubham
• 13,490 points
6,221 views

1 answer to this question.

0 votes

In your code, you have used "WebElement" to pass the file path, and you have used the <label> tag. You should rather target the <input> tag. Besides, the <input> tag also has style attribute set to "display: none;". So, this code should do the trick:

WebElement uploadElement = driver.findElement(By.xpath("//input[@id='btn_myFileInput']"));
((JavascriptExecutor)driver).executeScript("arguments[0].removeAttribute('style')", uploadElement)
uploadElement.sendKeys("C:\\Users\\Bharat\\Downloads\\apmr-auction-detail-574.pdf");
answered Mar 30, 2018 by nsv999
• 5,500 points
Hi there, I have been trying to find the solution for the same issue for a while. @nsv99 your solution worked for me. I m so happy. Big Thank you.!
Thanks @rachel. As long as it helped :)
Thank you @nsv999 it worked fine for me
Hi nsv999, your code worked well for me too. I have upvoted your answer too.

The above code is inserting file but it displays Choose file button and further the proceed button is not enabled.

Hey,

What did you want to try? Can you explain it a bit?

Related Questions In Selenium

0 votes
1 answer

How to handle the popup of file upload in Windows OS with WebDriver?

The value of name locator has probably ...READ MORE

answered Apr 25, 2018 in Selenium by king_kenny
• 3,710 points
18,527 views
0 votes
1 answer

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

Does not matter which OS or which ...READ MORE

answered Apr 29, 2018 in Selenium by king_kenny
• 3,710 points
10,815 views
0 votes
1 answer

File Upload in Selenium Webdriver

The problem I found is excessive backslash(\) is ...READ MORE

answered May 22, 2018 in Selenium by Meci Matt
• 9,460 points
1,498 views
0 votes
1 answer
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,674 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,143 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
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