有人能告诉我如何使用webdriver关闭除第一个标签/主标签之外的所有打开的标签吗?
我在下面尝试过,但它也关闭了所有标签,包括第一个标签。
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");
}
}
请帮帮我。
答案 0 :(得分:21)
获取所有窗口句柄然后遍历它们,将webdriver切换到新句柄,然后调用close方法。显然,跳过这个原始手柄,然后切换回剩余的手柄。
喜欢的东西;
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);
答案 1 :(得分:1)
尝试使用此代码:
for(String winHandle : driver.getWindowHandles())
{
if (winHandle == driver.getWindowHandles().toArray()[driver.getWindowHandles().size()-1])
{
continue;
}
driver.switchTo().window(winHandle);
driver.close();
}
答案 2 :(得分:1)
试试这个:
for(int i = driver.getWindowHandles().size() -1 ; i > 0 ; i--){
String winHandle = driver.getWindowHandles().toArray()[i].toString();
driver.switchTo().window(winHandle);
driver.close();
}
答案 3 :(得分:1)
我做了以下操作来关闭所有窗口但主要窗口:
// 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);
答案 4 :(得分:0)
我有一个实用工具方法可以切换到所需的窗口,如下所示
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;
}
}
一旦窗口的标题作为参数传递,它将带您进入所需的窗口。在你的情况下,你可以做到。
Webdriver childDriver = Utility.getHandleToWindow("titleOfChildWindow");
childDriver.close();
然后再次使用相同的方法切换到父窗口
Webdriver parentDriver = Utility.getHandleToWindow("titleOfParentWindow");
此方法在处理多个窗口时有效。
答案 5 :(得分:0)
如果有人在寻找VBA SeleniumBasic ChromeDriver解决方案,请按此。
Dim TabA as String
TabA = MyChromeDriver.Windows.Item(1).Title
Do While MyChromeDriver.Windows.Count > 1
MyChromeDriver.Windows.Item(MyChromeDriver.Windows.Count).Close
Loop
On Error Resume Next
MyChromeDriver.SwitchToWindowByTitle TabA
On Error GoTo 0
答案 6 :(得分:0)
这会给你清晰的画面, 将按此顺序:
// save a reference to our original tab's window handle
var originalTabInstance = myWebDriverInstance.CurrentWindowHandle;
// execute some JavaScript to open a new window
myWebDriverInstance.ExecuteJavaScript("window.open();");
// save a reference to our new tab's window handle, this would be the last entry in the WindowHandles collection
var newTabInstance = myWebDriverInstance.WindowHandles[Driver.Instance.WindowHandles.Count - 1];
// switch our WebDriver to the new tab's window handle
myWebDriverInstance.SwitchTo().Window(newTabInstance);
// lets navigate to a web site in our new tab
myWebDriverInstance.Navigate().GoToUrl("www.crowbarsolutions.com");
// now lets close our new tab
myWebDriverInstance.ExecuteJavaScript("window.close();");
// and switch our WebDriver back to the original tab's window handle
myWebDriverInstance.SwitchTo().Window(originalTabInstance);
// and have our WebDriver focus on the main document in the page to send commands to myWebDriverInstance.SwitchTo().DefaultContent();