请问如何切换到新的浏览器窗口,该窗口在单击按钮后打开?

如何切换到新的浏览器窗口,该窗口在单击按钮后打开?

我有情况,当点击按钮打开新的浏览器窗口与搜索结果。

是否有任何方法连接和聚焦到新打开的浏览器窗口?

并使用它,然后返回到原始(第一)窗口。



红糖糍粑
浏览 454回答 3
3回答

冉冉说

您可以在窗口之间切换,如下所示:// Store the current window handleString winHandleBefore = driver.getWindowHandle();// Perform the click operation that opens new window// Switch to new window openedfor(String winHandle : driver.getWindowHandles()){    driver.switchTo().window(winHandle);}// Perform the actions on new window// Close the new window, if that window no more requireddriver.close();// Switch back to original browser (first window)driver.switchTo().window(winHandleBefore);// Continue with original browser (first window)

POPMUISE

只是为了增加内容.。返回主窗口(默认窗口)。使用driver.switchTo().defaultContent();

白衣非少年

此脚本帮助您从父窗口转到子窗口并将CNTRL返回到父窗口。String&nbsp;parentWindow&nbsp;=&nbsp;driver.getWindowHandle();Set<String>&nbsp;handles&nbsp;=&nbsp;&nbsp;driver.getWindowHandles(); &nbsp;&nbsp;&nbsp;for(String&nbsp;windowHandle&nbsp;&nbsp;:&nbsp;handles) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(!windowHandle.equals(parentWindow)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;driver.switchTo().window(windowHandle); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<!--Perform&nbsp;your&nbsp;operation&nbsp;here&nbsp;for&nbsp;new&nbsp;window--> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;driver.close();&nbsp;//closing&nbsp;child&nbsp;window &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;driver.switchTo().window(parentWindow);&nbsp;//cntrl&nbsp;to&nbsp;parent&nbsp;window &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java