I noticed a large number of folks have Troubles importing a file in an evaluation Surroundings with Selenium WebDriver. I utilize the selenium WebDriver and java, and had the identical issue. I eventually have discovered a remedy, and so I may post it here hoping it helps someone else.

while I should add a document in a test, I click with Webdriver in the switch and watch for the windowpane “Available” to take. And then I duplicate the path to the document in the clipboard and after that stick it in the “available” windowpane and click “Enter”. This is functioning because when the window “available” appears, the emphasis is consistently in the “available” switch.

You are going to want these types and procedure:

import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;
public static void setClipboardData(String string) {
StringSelection stringSelection = new StringSelection(string);
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null);
}
And that is what i do, just after opening the “open” window:

setClipboardData(“C:\\path to file\\example.jpg”);
//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);

Which is what I do, only after starting the “available” windowpane:

Really, there exists an inbuilt method because of this, also. It should function in most browsers and OS.

Selenium s/w 2 (WebDriver) Java instance:

// assuming driver is a healthy WebDriver instance
WebElement fileInput = driver.findElement(By.xpath(“//input[@type=’file’]”));
fileInput.sendKeys(“C:/path/to/file.jpg”);

The notion is always to directly deliver the complete path to the document to a component that you might normally click at to get the modal window – that’s component.

Java Robot API assisted me for posting a document. I was fed up with File Add using WebDriver. Following is the code I used (Little change to yours):

Robot robot = new Robot();
robot.delay(1000);

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);
robot.delay(1000);

I wanted this because I cannot get the sendKeys function to perform when employed via Perform framework 2.1 (fluentlenium wrapper). I will be s/w evaluation over Opera [17.0.7] for Apple Macintosh and needed to make some mods to get it functioning. Under is an approximation of the final little I utilized.

val file = new File(PATH_TO_IPA)
val stringSelection: StringSelection = new StringSelection(file.getAbsolutePath)
Toolkit.getDefaultToolkit.getSystemClipboard().setContents(stringSelection, null)
val robot: Robot = new Robot()
// Cmd + Tab is needed since it launches a Java app and the browser looses focus
robot.keyPress(KeyEvent.VK_META)
robot.keyPress(KeyEvent.VK_TAB)
robot.keyRelease(KeyEvent.VK_META)
robot.keyRelease(KeyEvent.VK_TAB)
robot.delay(500)
robot.keyPress(KeyEvent.VK_META)
robot.keyPress(KeyEvent.VK_SHIFT)
robot.keyPress(KeyEvent.VK_G)
robot.keyRelease(KeyEvent.VK_META)
robot.keyRelease(KeyEvent.VK_SHIFT)
robot.keyRelease(KeyEvent.VK_G)
robot.keyPress(KeyEvent.VK_META)
robot.keyPress(KeyEvent.VK_V)
robot.keyRelease(KeyEvent.VK_META)
robot.keyRelease(KeyEvent.VK_V)
robot.keyPress(KeyEvent.VK_ENTER)
robot.keyRelease(KeyEvent.VK_ENTER)
robot.delay(500)
robot.keyPress(KeyEvent.VK_ENTER)
robot.keyRelease(KeyEvent.VK_ENTER)

Leave a Reply

Your email address will not be published. Required fields are marked *