简体   繁体   中英

Download a file in IE11 with Selenium and Robot class

I try to download a file in IE11 with Selenium WebDriver and Robot class, I am using IntelliJ and running test on a Selenium Grid with IE11.

I don't use element.click() function, because the controls stop there, hence I use sendKeys to focus on donwload button. Download popup appears, here comes Robot class. I try to press Alt+S to save file with help from Robot, but it doesn't press Alt+S on IE, it presses Alt+S on my IntelliJ instead !!!. Here is my code:

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

Does anyone have a solution for this?

You should use click first. you can try it, its working fine for me

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

So, I realized that, after clicking on Download button, the focus on web browser gets lost somehow, so I need to set focus back on web browser before Robo commands start, with this for example:

(JavascriptExecutor)webDriver.executeScript("window.focus();");

then simulation of key press works!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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