如何切换到新的浏览器窗口,单击按钮后打开该窗口?
问题描述:
我有情况,当点击按钮打开带有搜索结果的新浏览器窗口时。
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)