Selenium - 单击链接会打开一个新选项卡

Tre*_*opz 6 java selenium automation selenium-webdriver

我已经看到很多关于如何在新选项卡中打开链接的线索,但是如果您有一个创建新选项卡的链接并且您需要验证标题呢?我需要做的就是单击链接 - >验证新选项卡是否具有正确的标题 - >关闭选项卡并继续在原始选项卡上.在此先感谢您的帮助!

小智 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       
}
Run Code Online (Sandbox Code Playgroud)