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 29, 2018 in Selenium by Shubham
• 13,290 points
981 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 29, 2018 by nsv999
• 5,090 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.

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,650 points
3,085 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,650 points
3,045 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,420 points
419 views
0 votes
1 answer
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
597 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
903 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
587 views
0 votes
1 answer