Uploading file in Selenium using sendKeys() is not working
Please try the following javascript executor code
String filePath = "D:\\Images\\icons.png";
JavascriptExecutor jsx = (JavascriptExecutor) driver;
jsx.executeScript("document.getElementById('iconFlagFile').value='" + filePath + "';");
![Ubaid Ahmed](https://lh6.googleusercontent.com/-8-XSkzDakD4/AAAAAAAAAAI/AAAAAAAAACI/W1nIoZzP9SA/photo.jpg?sz=256)
Ubaid Ahmed
Updated on June 23, 2022Comments
-
Ubaid Ahmed about 2 years
I am unable to upload files using
sendKeys()
. This is my codedriver.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:
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 over 7 yearsIt 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 over 7 yearsI have added the html code. I have an input tag of type file for uploading files
-
FayazMd over 7 yearsIf that doesn't work then try the following:
JavascriptExecutor js = (JavascriptExecutor) driver; js.executeScript("document.getElementById('iconFlagFile').style.display = 'block';"); driver.findElement(By.id("iconFlagFile")).sendKeys("D:\\Images\\icons.png"); js.executeScript("document.getElementById('iconFlagFile').style.display = 'none';");
-
Ubaid Ahmed over 7 yearsYes 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 over 7 years@UbaidAhmed Can you please post the actual exception
-
Ubaid Ahmed over 7 yearsI 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 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.