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 command driver.close();, but it closes the first page instead of the second. How can I make Selenium to close a specific window?
Part of code
String HandleBefore = driver.getWindowHandle();
driver.findElement(By.xpath("...")).click();
for (String twohandle : driver.getWindowHandles()) {
driver.switchTo().window(twohandle);
}
driver.findElement(By.linkText("001")).click();
driver.close();
This works for me…