Here is the Answer to your Question:
Here is the working code block which opens the URL http://www.google.com
, prints Working on Google
on the console, opens http://facebook.com/
in a new tab, prints Working on Facebook
in the console, closes the tab which opened the URL http://facebook.com/
, gets you back to the tab which opened the URL http://www.google.com
and finally closes it.
System.setProperty("webdriver.gecko.driver", "C:\\your_directory\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");
String first_tab = driver.getWindowHandle();
System.out.println("Working on Google");
((JavascriptExecutor) driver).executeScript("window.open('http://facebook.com/');");
Set<String> s1 = driver.getWindowHandles();
Iterator<String> i1 = s1.iterator();
while(i1.hasNext())
{
String next_tab = i1.next();
if (!first_tab.equalsIgnoreCase(next_tab))
{
driver.switchTo().window(next_tab);
System.out.println("Working on Facebook");
driver.close();
}
}
driver.switchTo().window(first_tab);
driver.close();
Let me know if this Answers your Question.