使用 Robot Class 的 ALT+S 按键在与 Selenium 一起使用时不起作用

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

我正在使用 Selenium Webdriver 自动化场景。当我在 IE 中使用 selenium java 脚本单击按钮时,它会下载一个 excel 文件(显示在图像弹出窗口中)。 但是,我需要单击该栏中的 "Save" 选项,以便将其下载到默认位置。 由于 selenium 不支持单击文件下载浏览器弹出窗口,因此我尝试使用机器人 class 功能。

我使用的代码是:

driver.findElement(By.xpath("//*[@id='btnGenerateExtract']/span/span")).click();
    //some wait of 4 seconds
    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);
}

但是,这不起作用,因为它无法单击 "save" 选项。 请推荐

您可以停止浏览器请求提示 "Save or Open" 或手动指定默认保存位置。

Link:

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

此问题已通过对脚本进行细微更改得到解决。 而不是

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

使用 JavascriptExecutor 作为,

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

其余的键盘或鼠标事件可以使用机器人 class 执行。