3

I have a form, where <input type="file"> is invisible but there is a visible "Browse" button. I can't find element of file and update its value via sendKeys("filename").

I can use selenium to click "Browse" button and it will open a file dialog. How can I select a file in this open file dialog and close current dialog window?

Python 3.6 Selenium 3.0 Firefox webdriver Mac OS.

xudesheng
  • 1,082
  • 11
  • 25
  • using `sendkeys` also you can upload file https://stackoverflow.com/a/56168803/4513879 or using `robot` and `action class` you can upload file – Pradnya Bolli May 20 '19 at 07:11

1 Answers1

0

Once you have opened the dialog box use can use the following code to browse your file by pasting its location:

public static void copy(String text)
    {
     String myString = "file location";
     StringSelection stringSelection = new StringSelection(myString);
     Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
     clipboard.setContents(stringSelection, null);
    }

public static void paste() throws AWTException
    {
        Robot robot = new Robot();

        robot.keyPress(KeyEvent.VK_CONTROL);
        robot.keyPress(KeyEvent.VK_V);
        robot.keyRelease(KeyEvent.VK_CONTROL);
        robot.keyRelease(KeyEvent.VK_V);
        robot.keyPress(KeyEvent.VK_ENTER);
        robot.keyRelease(KeyEvent.VK_ENTER);
    }

Nikunj
  • 1
  • 1