74

在 Java 中使用 Selenium WebDriver。我正在尝试自动化一项功能,在该功能中我必须打开一个新选项卡并在那里执行一些操作并返回上一个选项卡(父级)。我使用了开关手柄,但它不起作用。一件奇怪的事情是两个选项卡具有相同的窗口句柄,因此我无法在选项卡之间切换。

但是,当我尝试使用不同的 Firefox 窗口时,它可以工作,但对于选项卡它不起作用。

如何切换标签?或者,如何在不使用窗口句柄的情况下切换选项卡,因为在我的情况下,两个选项卡的窗口句柄相同?

(我观察到,当您在同一个窗口中打开不同的选项卡时,窗口句柄保持不变)

4

24 回答 24

122
    psdbComponent.clickDocumentLink();
    ArrayList<String> tabs2 = new ArrayList<String> (driver.getWindowHandles());
    driver.switchTo().window(tabs2.get(1));
    driver.close();
    driver.switchTo().window(tabs2.get(0));

这段代码非常适合我。试试看。在你想在新标签上做某事之前,你总是需要将你的驱动程序切换到新标签。

于 2014-04-08T17:31:56.227 回答
26

这是一个简单的解决方案,用于打开新选项卡、将焦点更改为它、关闭选项卡并将焦点返回到旧/原始选项卡:

@Test
public void testTabs() {
    driver.get("https://business.twitter.com/start-advertising");
    assertStartAdvertising();

    // considering that there is only one tab opened in that point.
    String oldTab = driver.getWindowHandle();
    driver.findElement(By.linkText("Twitter Advertising Blog")).click();
    ArrayList<String> newTab = new ArrayList<String>(driver.getWindowHandles());
    newTab.remove(oldTab);
    // change focus to new tab
    driver.switchTo().window(newTab.get(0));
    assertAdvertisingBlog();

    // Do what you want here, you are in the new tab

    driver.close();
    // change focus back to old tab
    driver.switchTo().window(oldTab);
    assertStartAdvertising();

    // Do what you want here, you are in the old tab
}

private void assertStartAdvertising() {
    assertEquals("Start Advertising | Twitter for Business", driver.getTitle());
}

private void assertAdvertisingBlog() {
    assertEquals("Twitter Advertising", driver.getTitle());
}
于 2013-05-14T11:27:25.137 回答
14

Web 驱动程序处理不同窗口的方式和处理不同选项卡的方式有所不同。

案例1:
如果有多个窗口,那么以下代码可以提供帮助:

//Get the current window handle
String windowHandle = driver.getWindowHandle();

//Get the list of window handles
ArrayList tabs = new ArrayList (driver.getWindowHandles());
System.out.println(tabs.size());
//Use the list of window handles to switch between windows
driver.switchTo().window(tabs.get(0));

//Switch back to original window
driver.switchTo().window(mainWindowHandle);


情况2:
如果同一个窗口有多个选项卡,那么只有一个窗口句柄。因此,在窗口句柄之间切换会使控件保持在同一个选项卡中。
在这种情况下,使用 Ctrl + \t (Ctrl + Tab) 在选项卡之间切换更有用。

//Open a new tab using Ctrl + t
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"t");
//Switch between tabs using Ctrl + \t
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"\t");

详细的示例代码可以在这里找到:http:
//design-interviews.blogspot.com/2014/11/switching-between-tabs-in-same-browser-window.html

于 2014-11-27T15:06:09.537 回答
7

解决方法

假设:通过单击网页上的某些内容会打开一个新选项卡。

使用以下逻辑切换到第二个选项卡。

new Actions(driver).sendKeys(driver.findElement(By.tagName("html")), Keys.CONTROL).sendKeys(driver.findElement(By.tagName("html")),Keys.NUMPAD2).build().perform();

以同样的方式,您可以再次切换回第一个选项卡。

new Actions(driver).sendKeys(driver.findElement(By.tagName("html")), Keys.CONTROL).sendKeys(driver.findElement(By.tagName("html")),Keys.NUMPAD1).build().perform();
于 2012-10-04T15:43:29.650 回答
6

由于driver.window_handles不是 in order ,因此这是一个更好的解决方案。

首先使用快捷方式切换到第一个选项卡,Control + X以切换到浏览器窗口中的第“x”个选项卡。

driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "1");
# goes to 1st tab

driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "4");
# goes to 4th tab if its exists or goes to last tab.
于 2018-06-10T04:54:24.020 回答
4
String selectLinkOpeninNewTab = Keys.chord(Keys.CONTROL, Keys.RETURN);
    WebElement e = driver.findElement(By
            .xpath("html/body/header/div/div[1]/nav/a"));
e.sendKeys(selectLinkOpeninNewTab);//to open the link in a current page in to the browsers new tab

    e.sendKeys(Keys.CONTROL + "\t");//to move focus to next tab in same browser
    try {
        Thread.sleep(8000);
    } catch (InterruptedException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    //to wait some time in that tab
    e.sendKeys(Keys.CONTROL + "\t");//to switch the focus to old tab again

希望对你有帮助。。

于 2015-04-24T07:11:41.553 回答
3

您需要做的第一件事是打开一个新选项卡并保存它的句柄名称。最好使用 javascript 而不是键 (ctrl+t) 来执行此操作,因为自动化服务器上​​的键并不总是可用的。例子:

public static String openNewTab(String url) {
    executeJavaScript("window.parent = window.open('parent');");
    ArrayList<String> tabs = new ArrayList<String>(bot.driver.getWindowHandles());
    String handleName = tabs.get(1);
    bot.driver.switchTo().window(handleName);
    System.setProperty("current.window.handle", handleName);
    bot.driver.get(url);
    return handleName;
}

您需要做的第二件事是在选项卡之间切换。仅通过切换窗口句柄来执行此操作并不总是有效,因为您将处理的选项卡不会始终处于焦点,并且 Selenium 会不时出现故障。正如我所说,使用键有点问题,而且 javascript 并不真正支持切换选项卡,所以我使用警报来切换选项卡,它就像一个魅力:

public static void switchTab(int tabNumber, String handleName) {
        driver.switchTo().window(handleName);
        System.setProperty("current.window.handle", handleName);
        if (tabNumber==1)
            executeJavaScript("alert(\"alert\");");
        else
            executeJavaScript("parent.alert(\"alert\");");
        bot.wait(1000);
        driver.switchTo().alert().accept();
    }
于 2016-10-19T12:55:30.450 回答
2
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL,Keys.SHIFT,Keys.TAB);

此方法有助于在多个窗口之间切换。这种方法的限制性问题是它只能使用这么多次,直到达到所需的窗口。希望能帮助到你。

于 2016-04-28T17:17:33.650 回答
2

对我有用的简单答案:

for (String handle1 : driver1.getWindowHandles()) {
        System.out.println(handle1); 
        driver1.switchTo().window(handle1);     
}
于 2017-10-09T18:31:35.493 回答
2
Set<String> tabs = driver.getWindowHandles();
Iterator<String> it = tabs.iterator();
tab1 = it.next();
tab2 = it.next();
driver.switchTo().window(tab1);
driver.close();
driver.switchTo().window(tab2);

尝试这个。它应该工作

于 2020-07-10T19:16:47.830 回答
2

Selenium 2.53.1 使用 firefox 47.0.1 作为 Java 中的 WebDriver:无论我打开多少个选项卡,“driver.getWindowHandles()”都只会返回一个句柄,因此无法在选项卡之间切换。

一旦我开始使用 Chrome 51.0,我就可以得到所有的句柄。以下代码显示了如何访问多个驱动程序和每个驱动程序中的多个选项卡。

// INITIALIZE TWO DRIVERS (THESE REPRESENT SEPARATE CHROME WINDOWS)
driver1 = new ChromeDriver();
driver2 = new ChromeDriver();

// LOOP TO OPEN AS MANY TABS AS YOU WISH
for(int i = 0; i < TAB_NUMBER; i++) {
   driver1.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "t");
   // SLEEP FOR SPLIT SECOND TO ALLOW DRIVER TIME TO OPEN TAB
   Thread.sleep(100);

// STORE TAB HANDLES IN ARRAY LIST FOR EASY ACCESS
ArrayList tabs1 = new ArrayList<String> (driver1.getWindowHandles());

// REPEAT FOR THE SECOND DRIVER (SECOND CHROME BROWSER WINDOW)

// LOOP TO OPEN AS MANY TABS AS YOU WISH
for(int i = 0; i < TAB_NUMBER; i++) {
   driver2.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "t");
   // SLEEP FOR SPLIT SECOND TO ALLOW DRIVER TIME TO OPEN TAB
   Thread.sleep(100);

// STORE TAB HANDLES IN ARRAY LIST FOR EASY ACCESS
ArrayList tabs2 = new ArrayList<String> (driver1.getWindowHandles());

// NOW PERFORM DESIRED TASKS WITH FIRST BROWSER IN ANY TAB
for(int ii = 0; ii <= TAB_NUMBER; ii++) {
   driver1.switchTo().window(tabs1.get(ii));
   // LOGIC FOR THAT DRIVER'S CURRENT TAB
}

// PERFORM DESIRED TASKS WITH SECOND BROWSER IN ANY TAB
for(int ii = 0; ii <= TAB_NUMBER; ii++) {
   drvier2.switchTo().window(tabs2.get(ii));
   // LOGIC FOR THAT DRIVER'S CURRENT TAB
}

希望这能让您很好地了解如何在多个浏览器窗口中操作多个选项卡。

于 2016-08-24T14:38:11.403 回答
1

Please see below:

WebDriver driver = new FirefoxDriver();

driver.manage().window().maximize();
driver.get("https://www.irctc.co.in/");
String oldTab = driver.getWindowHandle();

//For opening window in New Tab
String selectLinkOpeninNewTab = Keys.chord(Keys.CONTROL,Keys.RETURN); 
driver.findElement(By.linkText("Hotels & Lounge")).sendKeys(selectLinkOpeninNewTab);

// Perform Ctrl + Tab to focus on new Tab window
new Actions(driver).sendKeys(Keys.chord(Keys.CONTROL, Keys.TAB)).perform();

// Switch driver control to focused tab window
driver.switchTo().window(oldTab);

driver.findElement(By.id("textfield")).sendKeys("bangalore");

Hope this is helpful!

于 2014-01-31T11:39:29.807 回答
1

这是一个非常简单的过程:假设您有两个选项卡,因此您需要先关闭当前选项卡,client.window(callback)因为 switch 命令“切换到第一个可用的选项卡”。然后您可以使用 轻松切换选项卡client.switchTab

于 2015-04-14T06:17:06.583 回答
1

我最近遇到了一个问题,链接是在一个新选项卡中打开的,但是 selenium 仍然专注于初始选项卡。

我正在使用 Chromedriver,专注于标签的唯一方法是让我使用switch_to_window().

这是Python代码:

driver.switch_to_window(driver.window_handles[-1])

所以提示是找出您需要的窗口句柄的名称,它们作为列表存储在

driver.window_handles
于 2013-04-29T05:00:29.720 回答
1

如何在浏览器中的选项卡之间切换的一个简短示例(如果有一个窗口):

// open the first tab
driver.get("https://www.google.com");
Thread.sleep(2000);

// open the second tab
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "t");
driver.get("https://www.google.com");
Thread.sleep(2000);

// switch to the previous tab
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "" + Keys.SHIFT + "" + Keys.TAB);
Thread.sleep(2000);

我写Thread.sleep(2000)只是为了有一个超时来查看选项卡之间的切换。

您可以使用 CTRL+TAB 切换到下一个选项卡,使用 CTRL+SHIFT+TAB 切换到上一个选项卡。

于 2016-04-22T13:09:59.890 回答
1

获取父窗口句柄。

String parentHandle = driverObj.getWindowHandle();
public String switchTab(String parentHandle){
    String currentHandle ="";
    Set<String> win  = ts.getDriver().getWindowHandles();   

    Iterator<String> it =  win.iterator();
    if(win.size() > 1){
        while(it.hasNext()){
            String handle = it.next();
            if (!handle.equalsIgnoreCase(parentHandle)){
                ts.getDriver().switchTo().window(handle);
                currentHandle = handle;
            }
        }
    }
    else{
        System.out.println("Unable to switch");
    }
    return currentHandle;
}
于 2017-08-09T06:36:14.683 回答
1

所选答案的缺陷是它不必要地假定webDriver.getWindowHandles(). 该getWindowHandles()方法返回 a Set,它不保证顺序。

我使用以下代码更改选项卡,它不假定任何顺序。

String currentTabHandle = driver.getWindowHandle();
String newTabHandle = driver.getWindowHandles()
       .stream()
       .filter(handle -> !handle.equals(currentTabHandle ))
       .findFirst()
       .get();
driver.switchTo().window(newTabHandle);
于 2018-02-28T11:21:51.547 回答
1
WebDriver driver = new FirefoxDriver();

driver.switchTo().window(driver.getWindowHandles().toArray()[numPage].toString());

numPage - int (0,1..)
于 2021-11-24T15:01:34.470 回答
1

公共类 TabBrowserDemo {

public static void main(String[] args) throws InterruptedException {
    System.out.println("Main Started");
    System.setProperty("webdriver.gecko.driver", "driver//geckodriver.exe");
    WebDriver driver = new FirefoxDriver();
    driver.get("https://www.irctc.co.in/eticketing/userSignUp.jsf");
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

    driver.findElement(By.xpath("//a[text()='Flights']")).click();
    waitForLoad(driver);
    Set<String> ids = driver.getWindowHandles();
    Iterator<String> iterator = ids.iterator();
    String parentID = iterator.next();
    System.out.println("Parent WIn id " + parentID);
    String childID = iterator.next();
    System.out.println("child win id " + childID);

    driver.switchTo().window(childID);
    List<WebElement> hyperlinks = driver.findElements(By.xpath("//a"));

    System.out.println("Total links in tabbed browser " + hyperlinks.size());

    Thread.sleep(3000);
//  driver.close();
    driver.switchTo().window(parentID);
    List<WebElement> hyperlinksOfParent = driver.findElements(By.xpath("//a"));

    System.out.println("Total links " + hyperlinksOfParent.size());

}

public static void waitForLoad(WebDriver driver) {
    ExpectedCondition<Boolean> pageLoadCondition = new
            ExpectedCondition<Boolean>() {
                public Boolean apply(WebDriver driver) {
                    return ((JavascriptExecutor)driver).executeScript("return document.readyState").equals("complete");
                }
            };
    WebDriverWait wait = new WebDriverWait(driver, 30);
    wait.until(pageLoadCondition);
}
于 2018-06-25T07:28:27.477 回答
1

这适用于 Firefox 和 Chrome 的 MacOS:

// opens the default browser tab with the first webpage
driver.get("the url 1");
thread.sleep(2000);

// opens the second tab
driver.findElement(By.cssSelector("Body")).sendKeys(Keys.COMMAND + "t");
driver.get("the url 2");
Thread.sleep(2000);

// comes back to the first tab
driver.findElement(By.cssSelector("Body")).sendKeys(Keys.COMMAND, Keys.SHIFT, "{");
于 2016-09-08T10:12:29.567 回答
1
protected void switchTabsUsingPartOfUrl(String platform) {
    String currentHandle = null;
    try {
        final Set<String> handles = driver.getWindowHandles();
        if (handles.size() > 1) {
            currentHandle = driver.getWindowHandle();
        }
        if (currentHandle != null) {
            for (final String handle : handles) {
                driver.switchTo().window(handle);
                if (currentUrl().contains(platform) && !currentHandle.equals(handle)) {
                    break;
                }
            }
        } else {
            for (final String handle : handles) {
                driver.switchTo().window(handle);
                if (currentUrl().contains(platform)) {
                    break;
                }
            }
        }
    } catch (Exception e) {
        System.out.println("Switching tabs failed");
    }
}

调用此方法并将参数传递给要切换到的选项卡的 url 的子字符串

于 2018-03-15T09:45:31.800 回答
1
    public void switchToNextTab() {
        ArrayList<String> tab = new ArrayList<>(driver.getWindowHandles());
        driver.switchTo().window(tab.get(1));
    }
    
    public void closeAndSwitchToNextTab() {
        driver.close();
        ArrayList<String> tab = new ArrayList<>(driver.getWindowHandles());
        driver.switchTo().window(tab.get(1));
    }

    public void switchToPreviousTab() {
        ArrayList<String> tab = new ArrayList<>(driver.getWindowHandles());
        driver.switchTo().window(tab.get(0));
    }

    public void closeTabAndReturn() {
        driver.close();
        ArrayList<String> tab = new ArrayList<>(driver.getWindowHandles());
        driver.switchTo().window(tab.get(0));
    }

    public void switchToPreviousTabAndClose() {
        ArrayList<String> tab = new ArrayList<>(driver.getWindowHandles());
        driver.switchTo().window(tab.get(1));
        driver.close();
    }
于 2020-06-24T15:01:51.243 回答
0

使用 Java,我使用它来将 selenium 焦点切换到新选项卡。

//Before the action that redirect to the new tab:
String windHandleCurrent = driver.getWindowHandle();
// code that click in a btn/link in order to open a new tab goes here
// now to make selenium move to the new tab 
ArrayList<String> windows = new ArrayList<String>(driver.getWindowHandles());
    for(int i =0;i<windows.size();i++ ) {
        String aWindow = windows.get(i);
        if(aWindow != windHandleCurrent) {
            driver.switchTo().window(aWindow);
        }
    }
// now you can code your AssertJUnit for the new tab.
于 2022-02-16T16:30:29.107 回答
0

字符串 mainWindow = driver.getWindowHandle(); seleniumHelper.switchToChildWindow(); .. ..//你的断言步骤

seleniumHelper.switchToWindow(mainWindow);

于 2022-02-10T13:25:05.167 回答