【问题标题】:Download a file in IE11 with Selenium and Robot class在 IE11 中使用 Selenium 和 Robot 类下载文件
【发布时间】:2018-05-23 09:43:57
【问题描述】:

我尝试使用 Selenium WebDriver 和 Robot 类在 IE11 中下载文件,我正在使用 IntelliJ 并使用 IE11 在 Selenium Grid 上运行测试。

我不使用 element.click() 函数,因为控件停在那里,因此我使用 sendKeys 专注于下载按钮。出现下载弹窗,Robot 类来了。我尝试在 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();
        }
    }

有人对此有解决方案吗?

【问题讨论】:

    标签: java selenium selenium-webdriver automated-tests awtrobot


    【解决方案1】:

    所以,我意识到,点击下载按钮后,网络浏览器的焦点不知何故丢失了,所以我需要在 Robo 命令开始之前将焦点重新设置在网络浏览器上,例如:

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

    然后模拟按键工作!

    【讨论】:

      【解决方案2】:

      您应该先使用点击。你可以试试,对我来说很好用

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

      【讨论】:

      • 感谢您调查此问题。但是正如我在问题中所描述的那样,我已经尝试了 click() 函数,但它对我不起作用,当我在下载按钮上调用 click() 时,光标/硒控件卡住了,这就是我使用 sendKeys.ENTER 的原因下载按钮。
      【解决方案3】:
          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();
              }
      

      【讨论】:

      • 希望它能解决问题,但请添加对代码的解释,以便用户完全理解他/她真正想要的。
      猜你喜欢
      • 2020-04-16
      • 1970-01-01
      • 1970-01-01
      • 2021-03-31
      • 1970-01-01
      • 2018-07-23
      • 2017-06-01
      • 2021-08-19
      • 2020-05-17
      相关资源
      最近更新 更多