使用webdriver关闭除第一个选项卡/主选项卡以外的所有打开的选项卡

编程入门 行业动态 更新时间:2024-10-27 16:37:39
本文介绍了使用webdriver关闭除第一个选项卡/主选项卡以外的所有打开的选项卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

任何人都可以告诉我如何使用webdriver关闭除第一个标签/主标签之外的所有打开的标签吗?

Can anyone tell me how to close all opened tabs except the first tab/main tab using webdriver?

我在下面尝试过,但它关闭所有标签,包括第一个选项卡。

I tried below, but it is closing all tabs including first tab as well.

public static void closeTabs() { String wh1=driver.getWindowHandle(); String cwh=null; while(wh1!=cwh) { new Actions(driver).sendKeys(Keys.CONTROL).sendKeys(Keys.NUMPAD1).perform(); driver.findElement(By.tagName("body")).sendKeys(Keys.CONTROL, Keys.TAB); cwh=driver.getWindowHandle(); driver.findElement(By.tagName("body")).sendKeys(Keys.CONTROL+"w"); } }

请帮助我。

推荐答案

获取所有窗口句柄然后遍历它们,将webdriver切换到新句柄,然后调用close方法。显然跳过这个原始手柄,然后切换回剩余的手柄。

Get all the window handles then iterate through them, switching webdriver to the new handle, then calling the close method. Obviously skip this for the original handle, then switch back to the remaining handle.

类似的东西;

String originalHandle = driver.getWindowHandle(); //Do something to open new tabs for(String handle : driver.getWindowHandles()) { if (!handle.equals(originalHandle)) { driver.switchTo().window(handle); driver.close(); } } driver.switchTo().window(originalHandle);

更多推荐

使用webdriver关闭除第一个选项卡/主选项卡以外的所有打开的选项卡

本文发布于:2023-11-06 21:58:56,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1564784.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:选项卡   第一个   webdriver

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!