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();
handleBefore
, then youswitchTo()
to all windows in a loop. I guess that the lastswitchTo
gets you to the first window instead of the second. I recommend you to start over, read the docs once again and read the javadocs ofswitchTo()
. – Disenfranchise