0

I have a window with two tabs. I am trying to close the tab with a specific title and switch the control to the other tab. Here is my code:

public static void closeTheWindowWithTitle(String title) {

    ArrayList<String> tabs = new ArrayList<String> (driver.getWindowHandles());
    String mainWindow = driver.getWindowHandle();

    for(int i = 0; i < tabs.size(); i++) {

        log.debug("switched to " + driver.getTitle() + " Window");
        if(driver.getTitle().contains(title))
        {
            driver.switchTo().window(tabs.get(i));
            driver.close();
            log.debug("Closed the  " + driver.getTitle() + " Window");
        }
    }
    driver.switchTo().window(mainWindow);
}

When I run my code, I am getting the following exception:

org.openqa.selenium.NoSuchWindowException: no such window: target window already closed
from unknown error: web view not found

I am unable to figure out what is the problem. Please help.

frianH
  • 7,295
  • 6
  • 20
  • 45
Connoisseur
  • 35
  • 1
  • 6
  • What is the size of driver.getWindowHandles() ? – TheCoder Oct 08 '19 at 11:08
  • 1
    On which line do you get the error? BTW: Your logging is on the wrong line, because you did not switch tabs when you log it. The if condition is probably never true, because you will always compare the current tab title with the expected tab title. You must switch before executing `driver.getTitle()` – AndiCover Oct 08 '19 at 11:11
  • @TheCoder: Size of driver.getWindowHandles is 2. – Connoisseur Oct 09 '19 at 10:24

3 Answers3

1

I'm guessing, that the WindowHandle of your Main-Window got changed, somewhere along the way. You should be able to get your problem solved by doing something similar to the suggested solution here, i.e. getting all WindowHandles, and iterating over them and in the end switching to [0], which should be the only one left, after closing the second one.

T3rminat0r
  • 21
  • 4
1

I hope this will help you to fix your issue, i dont want to provide code fix and i want to explain you the details process step by step.

Open firefox/IE/Chrome browser and Navigate to https://www.bbc.co.uk

WebDriver driver = new AnyDriveryourusing();
// set implicit time to 30 seconds
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
// navigate to the url
driver.get("https://www.bbc.co.uk");

Get the GU ID of the current (parent) window using getWindowHandle() method present in the webdriver and store the value in a String

// get the Session id of the Parent
String parentGUID = driver.getWindowHandle();

Click on the Open New Window button, application open new window with google page.

// click the button to open new window
driver.findElement(By.id("two-window")).click();
Thread.sleep(5000);

Get the GU IDs of the two windows (parent + google), using getWindowHandles() method present in the webdriver. Store the GU IDs in a Set Collection, this Set will have GU IDs of both parent and Child Browsers

// get the All the session id of the browsers
Set allGUID = driver.getWindowHandles();

iterate the Set of GUID values, and if the value is parent value skip it if not switch to the new window

// iterate the values in the set
for(String guid : allGUID){
    // one enter into if block if the GUID is not equal to parent window's GUID
    if(! guid.equals(parentGUID)){
        //todo
    }
}

Switch to window using switchTo().window() method, pass the GU ID of the child browser to this method.

// switch to the guid
driver.switchTo().window(guid);

Find the search bar in Google.com and search for "success"

driver.findElement(By.name("q")).sendKeys("success");

Close the Google tab/Window and return to parent tab/browser window

// close the browser
driver.close();
// switch back to the parent window
driver.switchTo().window(parentGUID);
stacktome
  • 790
  • 2
  • 9
  • 32
0

You were close but you weren't switching windows before checking the title. I've updated the code to something that should work.

public static void closeTheWindowWithTitle(String title)
{
    Set<String> tabs = driver.getWindowHandles();
    String mainWindow = driver.getWindowHandle();

    for(int i = 0; i < tabs.size(); i++)
    {
        // you need to switch to the window before checking title
        driver.switchTo().window(tabs.get(i));
        log.debug("switched to " + driver.getTitle() + " Window");
        if(driver.getTitle().contains(title))
        {
            driver.close();
            log.debug("Closed the  " + driver.getTitle() + " Window");
            break; // this breaks out of the loop, which I'm assuming is what you want when a match is found
        }
    }
    driver.switchTo().window(mainWindow);
}
JeffC
  • 22,180
  • 5
  • 32
  • 55