0

Here is the button inspect element for which file upload is not working

<button class="btn btn-success text-capitalize" id="ac-btn-imprt" type="button">Browse File</button>

So when I try to do sendkeys, it doesn't work because for send keys it has to be the input tag in my case it is as type=button.

Thanks in advance.

Note

  1. I am using selenium with java to automate my application.

  2. I have used robot class which works intermittently and i couldn't able to debug the robot class implementation as I haven't worked on it.

frianH
  • 7,295
  • 6
  • 20
  • 45
Sobhit Sharma
  • 697
  • 14
  • 45

2 Answers2

0

Add LocalFileDetector to driver if you using RemoteWebDriver:

driver.setFileDetector(new LocalFileDetector());

Find hidden <input type="file"> in HTML and sendKeys absolute path to file.

Details you can find:

How to upload file using Selenium WebDriver in Java

https://sqa.stackexchange.com/questions/12851/how-can-i-work-with-file-uploads-during-a-webdriver-test

Sers
  • 12,047
  • 2
  • 12
  • 31
0

You can use ROBOT API jars to upload the file. Simply trigger the browse button and when you need to give the location of your file to upload use Robot API to send the location and then again using Robot API press enter.

try {
        //Setting clipboard with file location
        setClipboardData(fileLocation);
        //native key strokes for CTRL, V and ENTER keys
        Robot robot = new Robot();

        robot.keyPress(KeyEvent.VK_CONTROL);
        robot.keyPress(KeyEvent.VK_V);
        robot.keyRelease(KeyEvent.VK_V);
        robot.keyRelease(KeyEvent.VK_CONTROL);
        robot.keyPress(KeyEvent.VK_ENTER);
        robot.keyRelease(KeyEvent.VK_ENTER);
    } catch (Exception exp) {
        exp.printStackTrace();
    }
  • compile group: 'org.robotframework', name: 'javalib-core', version: '1.2.1' added this dependency in build.gradle and the existing code started working. While debugging found that the tester who developed the code forgot to add the dependency. – Sobhit Sharma Sep 12 '18 at 17:40
  • Cheers.. All the best – Mukul Maheshwari Sep 14 '18 at 13:19