亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

無法使用java獲取IE中新打開的窗口的句柄

無法使用java獲取IE中新打開的窗口的句柄

Smart貓小萌 2023-09-27 15:10:42
我單擊代碼“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>
查看完整描述

1 回答

?
藍山帝景

TA貢獻1843條經驗 獲得超7個贊

具體地說,就我而言,它是?... InternetExplorerOptions options = new InternetExplorerOptions(); options.merge(cap); WebDriver driver = new InternetExplorerDriver(options);

解決方案 1

ieCapabilities.setCapability("requireWindowFocus", false);

解決方案 2

添加這兩個之后,驅動程序就能夠正確獲取窗口的句柄了。


查看完整回答
反對 回復 2023-09-27
  • 1 回答
  • 0 關注
  • 150 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號