无法使用 java 在 IE 中获取新打开的 window 的句柄

Unable to get the handle of a newly opened window in IE using java

我在我的代码 "Go" 中单击一个按钮,它会打开一个新的 window。 当我检查任务管理器时,我可以看到 IE 的实例为 2,但是 getWindowHandles() 无法获取新打开的 window 的句柄,而只是 returns 父 window处理。

我用来执行此操作的代码是:

SeleniumUtils.clickOnElement(webDriver, By.xpath("//input[@name='Go' and @value='Go']"), "Click on Go", reporter);
SeleniumUtils.waitLoading(SeleniumUtils.WAIT_LONG);

List<String> windowsList = new ArrayList<>();
String parentWindow = webDriver.getWindowHandle();
windowsList.add(parentWindow);

String emailWindow = SeleniumUtils.changeDriverToTheNewWindow(webDriver, windowsList, windowsList.size()+1);
windowsList.add(emailWindow);

要改成新的window,我写了一个函数:

public static String changeDriverToTheNewWindow(WebDriver webDriver, List<String> previousWindows, int numberOfWindows) {
    try {
        JavascriptExecutor jsExecuter = (JavascriptExecutor) webDriver; 
        Set<String> windows = Collections.emptySet();
        try 
        {
            WebDriverWait wait = new WebDriverWait (webDriver, 5);
            wait.until(ExpectedConditions.numberOfWindowsToBe(numberOfWindows));    
            windows = webDriver.getWindowHandles();
        }catch(Exception e) {
            lLogger.error("Error getting the window handles.", e);
            windows = webDriver.getWindowHandles();
        }

        for (String windowId : windows) {
            if (!previousWindows.contains(windowId)) {
                webDriver.switchTo().window(windowId);
                jsExecuter.executeScript("window.focus");
                //changeDriverToWindow(webDriver, windowId);
                return windowId;
            }
        }
    } catch (Exception e) {
        lLogger.error("Error changing driver to the new window!", e);
    }
    return null;
}

打开window的HTML&JS代码是:

<td class="xyz-column">

<link href="../Content/css/XYZ.css" rel="stylesheet" type="text/css">

<script type="text/javascript">
    function onMouseOver(control) {
        control.src = '../Content/images/lookup.png';
    }
    function onMouseOut(control) {
        control.src = '../Content/images/btn_lookup.png';
    }
</script>

<span class="xyz-link" id="body_XYZSelector_XYZLookup_LookupName" style="width: 175px; height: 18px; display: inline-block;" onclick="OpenXYZ(GetCurrentLookupId(this),'42e7a262-36ef-e911-80f1-005056aea4ce','10001'); return false;"> SHARANA </span>
<img class="xyz-lookup" id="body_XYZSelector_XYZLookup_LookupImage" onmouseover="onMouseOver(this);" onmouseout="onMouseOut(this);" onclick="OpenLookUp(this);" alt="Browse" src="../Content/images/btn_lookup.png" entitytype="xyz">

<input name="ctl00$body$XYZSelector$XYZLookup$LookupHiddenId" id="body_XYZSelector_XYZLookup_LookupHiddenId" type="hidden" value="56765">

<input name="ctl00$body$XYZSelector$XYZLookup$LookupHiddenName" id="body_XYZSelector_XYZLookup_LookupHiddenName" type="hidden" value="SHARANA">

<input name="ctl00$body$XYZSelector$XYZLookup$LookupHiddenId" id="body_XYZSelector_XYZLookup_LookupHiddenId" type="hidden" value="fd878f2b-90a2-4bf0-8a00-daa007b64b7d">


</td>

我找到了解决问题的办法。只要确保按照@DebanjanB 回答的 Solution 中看到的所有步骤进行操作即可。

更具体地说,在我的例子中是 ... InternetExplorerOptions options = new InternetExplorerOptions(); options.merge(cap); WebDriver driver = new InternetExplorerDriver(options);

如您在

中所见

ieCapabilities.setCapability("requireWindowFocus", false);

如您所见

加上这两个后,驱动就可以正确获取到window的句柄了。