且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用WebDriver单击新打开的选项卡中的链接

更新时间:2022-05-22 07:56:03

您需要使用 .switchTo(windowHandle); 命令用于访问第二个选项卡。

You will need to use the .switchTo(windowHandle); command to access your second tab.

在打开第二个选项卡之前 - 获取打开选项卡的windowHandle:

Before opening the second tab - get the windowHandle of the open tab:

String mainWindow = driver.getWindowHandle();

然后执行打开第二个标签的操作。现在你需要知道第二个标签的句柄并将控制权切换到它:

Then do your action that opens the second tab. Now you'll need to know the handle of the second tab and switch control to it:

Set<String> handles = driver.getWindowHandles();  
for (String handle : handles) {
    if (!handle.equals(mainWindow)) {
          driver.switchTo().window(handle);
          break;
    }
}

您对第二个标签的操作现在将在第二个窗口。当你完成并需要再次与第一个标签交互时: driver.switchTo()。defaultContent();

Your actions for the second tab will now happen in that second window. When you're finished and need to interact with the first tab again: driver.switchTo().defaultContent();