使用 Selenium 和 Robot 在 IE11 中下载文件 class
Download a file in IE11 with Selenium and Robot class
我尝试使用 Selenium WebDriver 和 Robot class 在 IE11 中下载文件,我正在使用 IntelliJ 并 运行 在带有 IE11 的 Selenium Grid 上进行测试。
我不使用 element.click() 函数,因为控件停在那里,因此我使用 sendKeys 专注于下载按钮。出现下载弹出窗口,机器人 class 来了。我尝试在 Robot 的帮助下按 Alt+S
来保存文件,但它不会在 IE 上按 Alt+S
,而是在我的 IntelliJ 上按 Alt+S
!!!这是我的代码:
if (webBrowser.equalsIgnoreCase("ie")) {
WebElement downloadReport = webDriver.findElement(By.id("clientReportDownload"));
try {
Robot robot = new Robot();
// sendKeys to focus on Download button and press Enter to download
downloadReport.sendKeys("");
downloadReport.sendKeys(Keys.ENTER);
waitSeconds(2);
// wait for Download popup
robot.setAutoDelay(250);
// simulate presse Alt + S to save file -> It presses Alt+S on IntelliJ instead !!!
robot.keyPress(KeyEvent.VK_ALT);
robot.keyPress(KeyEvent.VK_S);
robot.keyRelease(KeyEvent.VK_ALT);
robot.keyRelease(KeyEvent.VK_S);
waitSeconds(2);
} catch (AWTException e) {
e.printStackTrace();
}
}
有人对此有解决方案吗?
您应该先使用点击。你可以试试,对我来说效果很好
driver.findElement(By.id("element_id")).click();
Robot robot = new Robot(); // Robot class throws AWT Exception
Thread.sleep(2000); // Thread.sleep throws InterruptedException
robot.keyPress(KeyEvent.VK_DOWN); // press arrow down key of keyboard to navigate and select Save radio button
Thread.sleep(2000); // sleep has only been used to showcase each event separately
robot.keyPress(KeyEvent.VK_TAB);
Thread.sleep(2000);
robot.keyPress(KeyEvent.VK_TAB);
Thread.sleep(2000);
robot.keyPress(KeyEvent.VK_TAB);
Thread.sleep(2000);
robot.keyPress(KeyEvent.VK_ENTER);
所以,我意识到,在单击“下载”按钮后,网络浏览器的焦点不知何故丢失了,所以我需要在 Robo 命令开始之前将焦点重新设置到网络浏览器上,例如:
(JavascriptExecutor)webDriver.executeScript("window.focus();");
然后按键模拟就可以了!
Robot robot;
try {
// pressing download button
dr.findElement(By.xpath("//a[@class='btn btn-primary']")).sendKeys("""");
robot = new Robot();
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
// handling download
Thread.sleep(2000);
robot.keyPress(KeyEvent.VK_ALT);
robot.keyPress(KeyEvent.VK_S);
Thread.sleep(2000);
robot.keyRelease(KeyEvent.VK_S);
robot.keyRelease(KeyEvent.VK_ALT);
Thread.sleep(2000);
robot.keyPress(KeyEvent.VK_TAB);
System.out.println("tab entered ");
System.out.println("Download completed");
} catch (Exception e) {
e.printStackTrace();
}
我尝试使用 Selenium WebDriver 和 Robot class 在 IE11 中下载文件,我正在使用 IntelliJ 并 运行 在带有 IE11 的 Selenium Grid 上进行测试。
我不使用 element.click() 函数,因为控件停在那里,因此我使用 sendKeys 专注于下载按钮。出现下载弹出窗口,机器人 class 来了。我尝试在 Robot 的帮助下按 Alt+S
来保存文件,但它不会在 IE 上按 Alt+S
,而是在我的 IntelliJ 上按 Alt+S
!!!这是我的代码:
if (webBrowser.equalsIgnoreCase("ie")) {
WebElement downloadReport = webDriver.findElement(By.id("clientReportDownload"));
try {
Robot robot = new Robot();
// sendKeys to focus on Download button and press Enter to download
downloadReport.sendKeys("");
downloadReport.sendKeys(Keys.ENTER);
waitSeconds(2);
// wait for Download popup
robot.setAutoDelay(250);
// simulate presse Alt + S to save file -> It presses Alt+S on IntelliJ instead !!!
robot.keyPress(KeyEvent.VK_ALT);
robot.keyPress(KeyEvent.VK_S);
robot.keyRelease(KeyEvent.VK_ALT);
robot.keyRelease(KeyEvent.VK_S);
waitSeconds(2);
} catch (AWTException e) {
e.printStackTrace();
}
}
有人对此有解决方案吗?
您应该先使用点击。你可以试试,对我来说效果很好
driver.findElement(By.id("element_id")).click();
Robot robot = new Robot(); // Robot class throws AWT Exception
Thread.sleep(2000); // Thread.sleep throws InterruptedException
robot.keyPress(KeyEvent.VK_DOWN); // press arrow down key of keyboard to navigate and select Save radio button
Thread.sleep(2000); // sleep has only been used to showcase each event separately
robot.keyPress(KeyEvent.VK_TAB);
Thread.sleep(2000);
robot.keyPress(KeyEvent.VK_TAB);
Thread.sleep(2000);
robot.keyPress(KeyEvent.VK_TAB);
Thread.sleep(2000);
robot.keyPress(KeyEvent.VK_ENTER);
所以,我意识到,在单击“下载”按钮后,网络浏览器的焦点不知何故丢失了,所以我需要在 Robo 命令开始之前将焦点重新设置到网络浏览器上,例如:
(JavascriptExecutor)webDriver.executeScript("window.focus();");
然后按键模拟就可以了!
Robot robot;
try {
// pressing download button
dr.findElement(By.xpath("//a[@class='btn btn-primary']")).sendKeys("""");
robot = new Robot();
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
// handling download
Thread.sleep(2000);
robot.keyPress(KeyEvent.VK_ALT);
robot.keyPress(KeyEvent.VK_S);
Thread.sleep(2000);
robot.keyRelease(KeyEvent.VK_S);
robot.keyRelease(KeyEvent.VK_ALT);
Thread.sleep(2000);
robot.keyPress(KeyEvent.VK_TAB);
System.out.println("tab entered ");
System.out.println("Download completed");
} catch (Exception e) {
e.printStackTrace();
}