我见过很多关于如何在新标签页面中打开链接的主题,但是当你有一个创建新标签的链接而你需要验证标题的情况呢?我需要做的就是点击链接 - >验证新标签是否具有正确的标题 - >关闭选项卡并继续在原始选项卡上。在此先感谢您的帮助!
答案 0 :(得分:7)
//Get Current Page
String currentPageHandle = driver.getWindowHandle();
linkToClick.click();
//Add Logic to Wait till Page Load
// Get all Open Tabs
ArrayList<String> tabHandles = new ArrayList<String>(driver.getWindowHandles());
String pageTitle = "ThePageTitleIhaveToCheckFor";
boolean myNewTabFound = false;
for(String eachHandle : tabHandles)
{
driver.switchTo().window(eachHandle);
// Check Your Page Title
if(driver.getTitle().equalsIgnoreCase(pageTitle))
{
// Report ur new tab is found with appropriate title
//Close the current tab
driver.close(); // Note driver.quit() will close all tabs
//Swithc focus to Old tab
driver.switchTo().window(currentPageHandle);
myNewTabFound = true;
}
}
if(myNewTabFound)
{
// Report page not opened as expected
}