Selenium 不支持选项卡(截至 2013 年 6 月,Selenium 2.33.0)并且总是打开新窗口。如果您的测试打开一个新选项卡,祝您好运。
也就是说,如果它正确打开一个新窗口,请使用Select Window
.
Select Window | url=https://twitter.com/expectedPage
我在Java中工作的 WebDriver (2.33.0) 代码,希望它会有所帮助。您描述的问题是我的机器人知识开始下降的地方。
@Test
public void targetBlankLinkTest() {
// load the website and make sure only one window is opened
driver.get(file("TargetBlankLinkTest.html"));
assertEquals(1, driver.getWindowHandles().size());
// click the link and assert that a new window has been opened
driver.findElement(By.linkText("Follow us on Twitter!")).click();
Set<String> windowHandles = driver.getWindowHandles();
assertEquals(2, windowHandles.size());
// switch to the new window and do whatever you like
// (Java doesn't have Switch Window functionality that can select by URL.
// I could write it, but have been using this trick instead)
for (String handle : windowHandles) {
if (handle != driver.getWindowHandle()) {
driver.switchTo().window(handle);
}
}
assertThat(driver.getCurrentUrl(), containsString("twitter.com"));
}