【问题标题】:Selenium - Clicking a link opens up a new tabSelenium - 单击链接会打开一个新选项卡
【发布时间】:2016-05-06 04:14:11
【问题描述】:

我已经看到很多关于如何在新标签页中打开链接的帖子,但是当您有一个创建新标签页的链接并且您需要验证标题的情况下呢?我需要做的就是单击链接 --> 验证新选项卡的标题是否正确 --> 关闭选项卡并继续原来的选项卡。提前感谢您的帮助!

【问题讨论】:

    标签: java selenium selenium-webdriver automation


    【解决方案1】:
    //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       
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-06
      • 1970-01-01
      • 1970-01-01
      • 2011-08-31
      • 1970-01-01
      • 2021-12-21
      • 2012-05-07
      • 2016-03-26
      相关资源
      最近更新 更多