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

时间:2016-11-01 10:39:43

标签: java selenium awtrobot

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

我使用的代码是:

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);
}

但是,由于无法单击“保存”选项,因此无法正常工作。 请建议

2 个答案:

答案 0 :(得分:0)

您可以停止浏览器要求提示"保存或打开"或手动指定默认保存位置。

链接:

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

答案 1 :(得分:0)

此问题已通过脚本中的微小更改得以解决。 而不是

driver.findElement(By.xpath( “// * [@ id中= 'btnGenerateExtract'] /量程/跨度”))上单击();

使用JavascriptExecutor,

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

可以使用Robot类执行其余的键盘或鼠标事件。