Wednesday, September 5, 2018

How to work with Windows image upload or file browser window on selenium with Jave

There are are several methods to handle this kinds of situation but not all are working ,Because most of them are to use with HTML file browser windows ,
This solution is specially for Windows file browser window AKA Windows file browser dialog box.

User first need to click on the file browser button,The there will be a Windows file browser dialog box where you can browse for a file.

In a nut shell what we are doing is just Copy and Past the file path at the Windows file browser dialog box

try {
    StringSelection s = new StringSelection("C:\\Users\\XXX\\Pictures\\w.png");
    Toolkit.getDefaultToolkit().getSystemClipboard().setContents(s, null);
    Robot robot = new Robot();

    robot.keyPress(java.awt.event.KeyEvent.VK_CONTROL); //Press CTRL KEY
    robot.keyPress(java.awt.event.KeyEvent.VK_V);//Press V KEY
    robot.keyRelease(java.awt.event.KeyEvent.VK_CONTROL); //RELEASE CTRL KEY
    Thread.sleep(3000);
    robot.keyPress(java.awt.event.KeyEvent.VK_ENTER);
} catch (AWTException e) {
    e.printStackTrace();
} catch (InterruptedException e) {
    e.printStackTrace();
}

try {
    Thread.sleep(60000);
} catch (InterruptedException e) {
    e.printStackTrace();
}

No comments:

Post a Comment