简体   繁体   English

与Selenium一起使用时,使用Robot Class的ALT + S键无法正常工作

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

I am automating a scenario using Selenium Webdriver. 我正在使用Selenium Webdriver自动化方案。 When I click on the button using the selenium java script in IE, it downloads an excel file(shown in image-popup). 当我在IE中使用Selenium Java脚本单击按钮时,它会下载一个excel文件(显示在图片弹出窗口中)。 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(): 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 请建议

You can rather stop the browser asking for prompt to "Save or Open" or specify the default save location manually. 您可以停止浏览器,询问是否提示“保存或打开”,也可以手动指定默认的保存位置。

Link: 链接:

https://superuser.com/questions/273372/how-to-get-ie8-to-auto-save-downloaded-files-to-a-specific-directory https://superuser.com/questions/273372/how-to-get-ie8-to-auto-save-downloaded-files-to-a-specific-directory目录

This issue was resolved with a minor change in the script. 通过对脚本进行较小的更改即可解决此问题。 Instead of 代替

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

use JavascriptExecutor as, 使用JavascriptExecutor作为,

executor.executeScript("arguments[0].click();", driver.findElement(By.xpath("//*[@id='btnGenerateExtract']/span/span"))); 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. 其余的键盘或鼠标事件可以使用Robot类执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM