本文介紹了使用 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 關閉除第一個選項卡/主選項卡之外的所有打開的選項卡的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!