3

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.

4 Answers 4

2

Please try the following javascript executor code

String filePath = "D:\\Images\\icons.png";
JavascriptExecutor jsx = (JavascriptExecutor) driver;
jsx.executeScript("document.getElementById('iconFlagFile').value='" + filePath + "';");
Sign up to request clarification or add additional context in comments.

1 Comment

If 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';");
0

From your explanation, I am assuming that, you have an HTML tag that allows you to upload a file from local drive but it is not enabled. Unless you hit submit button it wont allow you to upload a file.

In such case, try to edit the form tag to edit and then try to call sendKeys() method, and you can edit the html elements using javascript executor

2 Comments

It would be nice to put html code of the element where you want to put the path, is it of input file type.?
I have added the html code. I have an input tag of type file for uploading files
0

Please try this for file upload.

    String filename = "path of the file";
    File file = new File(filename);
    String path = file.getAbsolutePath();
    // give the URL to upload
    driver.findElement(By locator).sendKeys(path);

4 Comments

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. :(
@UbaidAhmed Can you please post the actual exception
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.
@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.
0

Use Clipboard class and Robot class combination

    Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
    StringSelection str = new StringSelection("File Path");
    clipboard.setContents(str, null);

    WebElement element = driver.findElement(By.xpath("elementXpath"));
    element.click();

    Robot robot = new Robot();
    robot.keyPress(KeyEvent.VK_CONTROL);
    robot.keyPress(KeyEvent.VK_V);
    driverwait(1);
    robot.keyRelease(KeyEvent.VK_CONTROL);
    robot.keyRelease(KeyEvent.VK_V);
    driverwait(1);
    robot.keyPress(KeyEvent.VK_ENTER);
    robot.keyRelease(KeyEvent.VK_ENTER);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.