Uploading file in Selenium using sendKeys() is not working

13,566

Please try the following javascript executor code

String filePath = "D:\\Images\\icons.png";
JavascriptExecutor jsx = (JavascriptExecutor) driver;
jsx.executeScript("document.getElementById('iconFlagFile').value='" + filePath + "';");
Share:
13,566
Ubaid Ahmed
Author by

Ubaid Ahmed

Updated on June 23, 2022

Comments

  • Ubaid Ahmed
    Ubaid Ahmed about 2 years

    I am unable to upload files using sendKeys(). This is my code

    driver.findElement(By.xpath(Locators.browseFlagIconBtn)).sendKeys("D:\\Images\\icons.png");
    

    I have an input tag of type file for file upload

    <input type="file" id="iconFlagFile" name="iconFile" onchange="setFlagIconFile()" value="" class=" file-input-opacity" style="position: absolute; z-index: 100; font-size: 200px; line-height: 200px; top: 0px; left: 0px; opacity: 0;">
    

    However when I click on submit button of the form where this input field is then I am redirected to the below page:

    enter image description here

    Cause of this issue :

    When I pass the path using sendKeys() then it correctly picks up the file that needs to be uploaded. However, the actual upload takes place when user clicks on the submit button. Now I don't know why it it searches for the file that it picked in Chrome's directory, for example: "C:\Program Files (x86)\Google\Chrome\Application\53.0.2785.116"

    Obviously it will not be able to find the file.

    Question: How to fix this? Let me know if I need to provide more information.

  • FayazMd
    FayazMd over 7 years
    It would be nice to put html code of the element where you want to put the path, is it of input file type.?
  • Ubaid Ahmed
    Ubaid Ahmed over 7 years
    I have added the html code. I have an input tag of type file for uploading files
  • FayazMd
    FayazMd over 7 years
    If that doesn't work then try the following: JavascriptExecutor js = (JavascriptExecutor) driver; js.executeScript("document.getElementById('iconFlagFile').st‌​yle.display = 'block';"); driver.findElement(By.id("iconFlagFile")).sendKeys("D:\\Imag‌​es\\icons.png"); js.executeScript("document.getElementById('iconFlagFile').st‌​yle.display = 'none';");
  • Ubaid Ahmed
    Ubaid Ahmed over 7 years
    Yes I have already tried this. Actual file upload takes place when the user clicks on the submit button and not when the file is selected in File Upload Box. So unfortunately the solution given by you is not working in my case. :(
  • Chandra Shekhar
    Chandra Shekhar over 7 years
    @UbaidAhmed Can you please post the actual exception
  • Ubaid Ahmed
    Ubaid Ahmed over 7 years
    I am not getting any exception. Please refer the screenshot attached. I am providing the path of D:\ but when I am submitting the form it is looking for the file in C:\. When I perform the same steps manually then it is working fine. Problem arises when it is done via automation.
  • Chandra Shekhar
    Chandra Shekhar over 7 years
    @UbaidAhmed This works in every case where input type is a File. I dont have much of your data to test from my end. But can you please try again by changing locator from 'xpath' to either 'name' or 'id' and using path as a String variable only.