How can I close a specific window using Selenium WebDriver with Java?

后端 未结 4 762
耶瑟儿~
耶瑟儿~ 2021-01-03 21:27

I use Selenium WebDriver. I open the first page then open the second page - perform some action and go back to first page. Before I want to close the second page I use the c

4条回答
  •  不知归路
    2021-01-03 22:17

    You can close a specific window by it's title or identifying a specific unique element of that window..

    private void SwitchTabandClose()
    {
        Set  windows = driver.getWindowHandles();
        String mainwindow = driver.getWindowHandle();
    
        for (String handle: windows)
        {
            driver.switchTo().window(handle);
            System.out.println("switched to "+driver.getTitle()+"  Window");
            String pagetitle = driver.getTitle();
            if(pagetitle.equalsIgnoreCase("XYZ Title"))
            {
                driver.close();
                System.out.println("Closed the  '"+pagetitle+"' Tab now ...");
            }
        }
    
        driver.switchTo().window(mainwindow);
     }
    

提交回复
热议问题