aman_coder aman_coder - 1 month ago 13
Java Question

ALT+S key press using Robot Class not working when used along with Selenium

I am automating a scenario using Selenium Webdriver. When I click on the button using the selenium java script in IE, it downloads an excel file(shown in image-popup).
However I need to click on the "Save" option in that bar, so that it gets downloaded at the default location.
Since selenium does not provide the support for clicking the file download browser popup, so i have tried using robot class fuctionality.

The code which i have used is:

driver.findElement(By.xpath("//*[@id='btnGenerateExtract']/span/span")).click();
//some wait of 4 seconds
clickOnSave();


Code for clickOnSave():

{
Robot robot=new Robot();
robot.setAutoDelay(250);
robot.keyPress(KeyEvent.VK_ALT);
Thread.sleep(1000);
robot.keyPress(KeyEvent.VK_S);
robot.keyRelease(KeyEvent.VK_ALT);
robot.keyRelease(KeyEvent.VK_S);
}


However, this is not working as it is not able to click on the "save" option.
Please suggest

Answer

This issue was resolved with a minor change in the script. Instead of

driver.findElement(By.xpath("//*[@id='btnGenerateExtract']/span/span")).click();

use JavascriptExecutor as,

executor.executeScript("arguments[0].click();", driver.findElement(By.xpath("//*[@id='btnGenerateExtract']/span/span")));

Rest of the keyboard or mouse events could be performed using Robot class.