Java 使用 webdriver 关闭除第一个选项卡/主选项卡之外的所有打开的选项卡
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18493572/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Closing all opened tabs except the first tab/main tab using webdriver
提问by Srikanth Nakka
Can anyone tell me how to close all opened tabs except the first tab/main tab using webdriver?
谁能告诉我如何使用 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");
}
}
Please help me.
请帮我。
采纳答案by Robbie Wareham
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.
获取所有窗口句柄,然后遍历它们,将 webdriver 切换到新句柄,然后调用 close 方法。显然对于原始手柄跳过此操作,然后切换回其余手柄。
Something like;
就像是;
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);
回答by Abhishek_Mishra
Try this code it should work:
试试这个代码它应该可以工作:
for(String winHandle : driver.getWindowHandles())
{
if (winHandle == driver.getWindowHandles().toArray()[driver.getWindowHandles().size()-1])
{
continue;
}
driver.switchTo().window(winHandle);
driver.close();
}
回答by Abhishek Singh
I have an utility method to switch to the required window as shown below
我有一个实用方法可以切换到所需的窗口,如下所示
public class Utility
{
public static WebDriver getHandleToWindow(String title){
//parentWindowHandle = WebDriverInitialize.getDriver().getWindowHandle(); // save the current window handle.
WebDriver popup = null;
Set<String> windowIterator = WebDriverInitialize.getDriver().getWindowHandles();
System.err.println("No of windows : " + windowIterator.size());
for (String s : windowIterator) {
String windowHandle = s;
popup = WebDriverInitialize.getDriver().switchTo().window(windowHandle);
System.out.println("Window Title : " + popup.getTitle());
System.out.println("Window Url : " + popup.getCurrentUrl());
if (popup.getTitle().equals(title) ){
System.out.println("Selected Window Title : " + popup.getTitle());
return popup;
}
}
System.out.println("Window Title :" + popup.getTitle());
System.out.println();
return popup;
}
}
It will take you to desired window once title of the window is passed as parameter. In your case you can do.
一旦窗口的标题作为参数传递,它将带您到所需的窗口。在你的情况下,你可以做到。
Webdriver childDriver = Utility.getHandleToWindow("titleOfChildWindow");
childDriver.close();
and then again switch to parent window using the same method
然后再次使用相同的方法切换到父窗口
Webdriver parentDriver = Utility.getHandleToWindow("titleOfParentWindow");
This method works effectively when dealing with multiple windows.
这种方法在处理多个窗口时有效。
回答by Yanir Calisar
Try this:
尝试这个:
for(int i = driver.getWindowHandles().size() -1 ; i > 0 ; i--){
String winHandle = driver.getWindowHandles().toArray()[i].toString();
driver.switchTo().window(winHandle);
driver.close();
}
回答by rfcordeiro
I did the following to close all windows but the main one:
我做了以下操作来关闭所有窗口,但主要窗口:
// Find out which handle is the one of the main window
String mainWindow = driver.CurrentWindowHandle;
// Get a list of all windows, except the main window
driver.WindowHandles.Where(w => w != mainWindow).ToList()
// For each window found
.ForEach(w =>
{
// switch to the window
driver.SwitchTo().Window(w);
// close the window
driver.Close();
});
// At the end, come back to the main window
driver.SwitchTo().Window(mainWindow);