【问题标题】:Unable to get the handle of a newly opened window in IE using java无法使用 java 在 IE 中获取新打开的窗口的句柄
【发布时间】:2019-10-04 11:14:47
【问题描述】:

我正在单击代码“Go”中的一个按钮,该按钮会打开一个新窗口。 当我检查任务管理器时,我可以看到 IE 的实例为 2,但 getWindowHandles() 无法获取新打开的窗口的句柄,只返回父窗口句柄。

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

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

要切换到新窗口,我写了一个函数:

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

打开窗口的 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>

【问题讨论】:

  • 在使用 getWindowHandles 之前请稍等 .. 先用 Thread.sleep 试试
  • @Shubham 我在getwindowHandles() 之前尝试过Thread.sleep(1000) 仍然无法正常工作。
  • 尝试更新你的iedriver,可能是版本问题
  • @ShubhamJain 我认为版本不是问题,因为我可以使用同一个窗口在其他窗口之间成功切换。
  • 您是否尝试调试代码并验证您是否获得了正确的窗口句柄?如果没有,你可以试一试。该窗口是否需要更多时间才能完全加载?如果是这样,请尝试增加一些额外的时间来检查它是否有帮助。你说,你可以切换到其他窗口。那么这些窗口之间有什么区别吗?让我们知道这可能有助于缩小问题范围。

标签: java selenium internet-explorer


【解决方案1】:

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

更具体地说,就我而言,它是 ... InternetExplorerOptions options = new InternetExplorerOptions(); options.merge(cap); WebDriver driver = new InternetExplorerDriver(options);

正如您在Solution 1

看到的

ieCapabilities.setCapability("requireWindowFocus", false);

你可以在Solution 2看到

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

【讨论】:

    猜你喜欢
    • 2016-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-24
    相关资源
    最近更新 更多