如何切换到新的浏览器窗口,该窗口在单击按钮后打开?我有情况,当点击按钮打开新的浏览器窗口与搜索结果。是否有任何方法连接和聚焦到新打开的浏览器窗口?并使用它,然后返回到原始(第一)窗口。
3 回答
慕田峪7331174
TA贡献1828条经验 获得超13个赞
您可以在窗口之间切换,如下所示:
// 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)
隔江千里
TA贡献1906条经验 获得超10个赞
String parentWindow = driver.getWindowHandle();Set<String> handles = driver.getWindowHandles(); for(String windowHandle : handles) { if(!windowHandle.equals(parentWindow)) { driver.switchTo().window(windowHandle); <!--Perform your operation here for new window--> driver.close(); //closing child window driver.switchTo().window(parentWindow); //cntrl to parent window } }
添加回答
举报
0/150
提交
取消