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

编程入门 行业动态 更新时间:2024-10-27 22:30:33
本文介绍了使用 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 22:00:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1564787.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:选项卡   第一个   webdriver

发布评论

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

>www.elefans.com

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