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
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);
}