如何切换到单击按钮后打开的新浏览器窗口?
问题描述:
我遇到了情况,点击按钮会打开带有搜索结果的新浏览器窗口.
I have situation, when click on button opens the new browser window with search results.
有没有办法连接并聚焦到新打开的浏览器窗口?
并使用它,然后返回到原始(第一个)窗口.
And work with it, then return back to original(first) window.
答
您可以在窗口之间切换,如下所示:
You can switch between windows as below:
// Store the current window handle
String winHandleBefore = driver.getWindowHandle();
// Perform the click operation that opens new window
// Switch to new window opened
for(String winHandle : driver.getWindowHandles()){
driver.switchTo().window(winHandle);
}
// Perform the actions on new window
// Close the new window, if that window no more required
driver.close();
// Switch back to original browser (first window)
driver.switchTo().window(winHandleBefore);
// Continue with original browser (first window)