Reputation: 1281
I know I'm not the first one having a hard time trying to upload a document from Windows file explorer with Selenium (I've done this before), but this app is weirdly done.
Basically, you click on an input
element, and on keyup, a Windows file explorer appears.
In my test, I populate the clipboard and try to send the file path in the Windows file explorer input (which seems to be focused):
String myString = text;
StringSelection stringSelection = new StringSelection(myString);
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(stringSelection, null);
Actions action = new Actions(driver);
action.sendKeys(Keys.chord(Keys.CONTROL, "v")).perform();
action.build().perform();
Unfortunately, it's the browser input that receives the pasted text.
I'm willing to unfocus this input, so maybe the text will be pasted in the Windows file explorer input.
Any idea? Thank you very much.
Upvotes: 2
Views: 497
Reputation: 1281
Robot helped me get out of this problem.
try {
Robot r = new Robot();
r.keyPress(KeyEvent.VK_CONTROL);
r.keyPress(KeyEvent.VK_V);
r.keyRelease(KeyEvent.VK_CONTROL);
r.keyRelease(KeyEvent.VK_V);
} catch (AWTException e) {
e.printStackTrace();
}
The Actions action = new Actions(driver);
should have given me a hint: if we're messing around with the driver, then it's impossible to mess around with the Windows file explorer.
Upvotes: 1