【问题标题】:Selenium SwitchWindow in IE IssueIE 问题中的 Selenium SwitchWindow
【发布时间】:2017-08-01 15:50:55
【问题描述】:

我正在尝试在 IE 上使用 selenium Web 驱动程序切换到弹出窗口。当我到达切换到我的弹出窗口的行时,代码只是挂起。我正在寻找可以尝试切换窗口 10 次并在 20 秒后尝试另一次尝试的方法,就像我在下面尝试做的那样,或者是一种更好的方法来确保窗口正确切换。如果我手动关闭弹出窗口,我会得到 noSuchWindow Exceptions 并且代码会爆炸。在发布此之前,我已经查看了其他 stackoverflow 文章,但我相信我的问题是独一无二的。这是我的场景:

    Scenario:
    1. Retrieve parent window handle
    2. Perform action launching popup window
    3. Get the popup window handles and store them in a string set
    4. Loop through window handles until there are no more. Retrieve Popup window handle
    5. Loop until the popup window does not match the parent windows handle and if 20 seconds has passed
    6. Switch to popup ---Code Hangs Here---
    7. Retrieve popup title
    8. Close popup
    9. Switch to parent
    10. Verification of title

以下是上述场景的所有相关代码:

    String popupWindow = "";
    String parentWindow = "";
    int saveCount = 0;
    int getPopupCount = 0;
    int tryCount = 0;

    // Get Parent window handle
    parentWindow = driver.getWindowHandle();
    System.out.println("parentWindow: " + parentWindow);
    Thread.sleep(500);

    //Perform Action launching popup window

    //Get the popup window handles and store them in a string set
    Set<String> popups =  driver.getWindowHandles();
    saveCount = getPopupCount;
    try {
        tryCount = 0;
        //Loop until the popup count does not equal the save count or until 10 tries (20 seconds) have passed
        while (saveCount == getPopupCount && tryCount++ < 10) {
            //Wait 2 second
            Thread.sleep(2000);

            getPopupCount = popups.size();
            System.out.println("getPopupCount: -" + getPopupCount + "-");
        }//end while
        if (tryCount >= 10) {
            System.out.println("Failed after 10 tries");
        }//end if

        //Loop through window handles until there are no more. Retrieve Popup window handle
        Iterator<String> myIterator = popups.iterator();
        while (myIterator.hasNext()) {
            popupWindow = myIterator.next();
            System.out.println("popupWindow: " + popupWindow);
            System.out.println("Boolean should be false: " + parentWindow.equalsIgnoreCase(popupWindow));
            Thread.sleep(5000);

            //fetch starting time
            long startTime = System.currentTimeMillis(); 

            //Loop until the popup window does not match the parent windows handle and if 20 seconds has passed
            while(!parentWindow.equalsIgnoreCase(popupWindow) && (System.currentTimeMillis()-startTime) < 20000) {
                try{
                    Thread.sleep(500);

                    //Switch to the Popup window            
                    //TODO - This is where it fails
                    driver.switchTo().window(popupWindow);
                    Thread.sleep(500);
                    System.out.println(driver.getTitle());
                    popupTitle = driver.getTitle();

                    //Close the Popup Window
                    driver.close();
                } catch (Exception e) {
                    throw new RuntimeException(Thread.currentThread().getStackTrace()[1].getMethodName()
                            + "...Error: " + e.getMessage());
                }//end catch
            }//end if
        }//end while
    } catch(Exception e) {
        throw new RuntimeException(Thread.currentThread().getStackTrace()[1].getMethodName()
                + "...Error switching to and closing popup: " + e.getMessage());
    }//end catch

    //Switch to parent window.
    driver.switchTo().window(parentWindow);
    driver.manage().window().maximize();
    Thread.sleep(2000);

    //Verification of title
    Assert.assertTrue(popupTitle.contains("MYTITLE"));

CI 信息:

JDK:1.8.0_66

Java:版本 8

IE:11

其他没有回答我的问题的类似问题:

switchWindow does not work in IE

How to switch to the new browser window, which opens after click on the button?

How to exit a while loop after a certain time?

非常感谢任何帮助或反馈!

【问题讨论】:

  • 如果它是公开的,你能给我们提供 URL 吗?谢谢
  • 这是一个私人网址。我可以去寻找一个有类似弹出窗口的网站并在那里进行测试并报告是否有帮助?
  • 如果你能分享一个类似的 URL,那就太好了。谢谢
  • 我也在那个网站上运行了我的代码,但它没有工作。我可能用循环给自己​​挖了一个洞。本网站提供的代码在 IE 中适用于我,因此我将尝试在我的私有代码上实现它。我会让你知道它是否有效。这将让我知道我的私人网站上的弹出窗口是否可以使用 Selenium 进行操作,因此现在不需要您提供任何东西。感谢您不经意间为我指明了正确的思路/方向。

标签: java loops selenium internet-explorer selenium-webdriver


【解决方案1】:

使用下面的代码,我针对我的私有代码和http://demo.guru99.com/popup.php 演示弹出站点进行了测试。我的代码在该站点上运行良好,但在我的私人站点上失败。我实施了等待期,但我不认为这是一个时间问题。我只是相信弹出窗口在 IE 上与我私人网站上的 Selenium 不兼容。发布我在虚拟站点上工作的代码作为答案,以防其他人遇到与代码有效的类似问题。

    //Retrieve parent window handle
    parentWindow = driver.getWindowHandle();        

    //Loop through the window handles until you are on the popup window
    for(String popupWindow : driver.getWindowHandles()){
       if (driver.switchTo().window(popupWindow).getTitle().equals(myTitle)) {
         break;
       } 
       else {
          driver.switchTo().window(parentWindow);
       } 
    }

    //Store the title
    popupTitle = driver.getTitle();

    //Close the Popup Window
    driver.close();     

    //switch to parent window.
    driver.switchTo().window(parentWindow);
    driver.manage().window().maximize();

    //Verification of title
    Assert.assertTrue(popupTitle.toUpperCase().contains(myTitle));

【讨论】:

  • 我们能不能至少给你的私人网站上的弹出窗口截图?
猜你喜欢
  • 2016-01-04
  • 2011-04-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-05
  • 2011-02-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多