【问题标题】:Window is closed while exporting documents and unable to switch to parent window导出文档时窗口关闭,无法切换到父窗口
【发布时间】:2018-10-15 14:55:43
【问题描述】:

我正在 IE 浏览器上进行测试。单击子窗口中的导出按钮后,子窗口关闭并在 IE 中弹出保存并打开下载。此后,我使用机器人类切换到弹出并单击保存选项以下载文件,如图所示。但是单击后我想返回父窗口,但它显示“窗口关闭”并且无法这样做。

     String parent= driver.getWindowHandle();

    for (String child : driver.getWindowHandles()) {

        if(!parent.equalsIgnoreCase(child))
        {

        driver.switchTo().window(child);


        if(browser.equals("chrome"))
        {
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//a[contains(.,'Export')]"))).click();
        }
        else if(browser.equals("IE"))
        {
            wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//a[contains(.,'Export')]")));

            WebElement ele=driver.findElement(By.xpath("//a[contains(.,'Export')]"));

            myRobot.DownloadFileInIEBrowser(driver, ele);   
        }
        }
    }
   System.out.println("current handles"+driver.getWindowHandle());
   driver.switchTo().window(parent);

 public class myRobot {

public static void DownloadFileInIEBrowser(WebDriver driver,WebElement ele) throws AWTException, InterruptedException
{

    Robot robot = new Robot();
   //get the focus on the element..don't use click since it stalls the driver          
    new Actions(driver).moveToElement(ele).perform();

    Thread.sleep(3000);
    Actions action = new Actions(driver);
    action.contextClick(ele).build().perform(); //right click on focus element
    Thread.sleep(5000);



    for(int i=0;i<2;i++)
    {
   robot.keyPress(KeyEvent.VK_ENTER);         
   robot.keyRelease(KeyEvent.VK_ENTER);
    }
   robot.delay(2000);
   /*robot command to move cursor to dialog pop up window
    * Ctrl+F6 pressed to move cursor to pop up dailog window in IE while downloading
    * Three times Tab button is pressed to focus cursor on Save button
    * Hit Enter again to save file
    */
   robot.keyPress(KeyEvent.VK_CONTROL);
   robot.keyPress(KeyEvent.VK_F6);
   robot.keyRelease(KeyEvent.VK_F6);
   robot.keyRelease(KeyEvent.VK_CONTROL);
   for(int i=0;i<3;i++)
   {
   robot.keyPress(KeyEvent.VK_TAB);
   robot.keyRelease(KeyEvent.VK_TAB);
   }
   robot.keyPress(KeyEvent.VK_ENTER);
   robot.keyRelease(KeyEvent.VK_ENTER);

}

}

【问题讨论】:

    标签: java selenium testing


    【解决方案1】:

    首先 - 根据我自己的经验,getWindowHandles() 需要一些时间。

    第二个 - 最好将 getWindowHandles() 设为 ArrayList

    第三 - 切换到 WindowHandle 而不是父级。

    ArrayList<String> winHandles = new ArrayList<String> (driver.getWindowHandles());
    Thread.sleep(500);
    driver.switchTo().window(winHandles.get(1));
    // driver.switchTo().window(winHandles.get(0));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-15
      • 1970-01-01
      • 2011-05-15
      • 1970-01-01
      相关资源
      最近更新 更多