How do I use selenium webdriver and Java to close a specific window?
•
Java
I use selenium webdriver I open the first page and then the second - do something and go back to the 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 do I make selenium close a specific window?
Partial 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();
Solution
String base = driver.getWindowHandle();
String base = driver.getWindowHandle();
Set <String> set = driver.getWindowHandles();
set.remove(base);
assert set.size()==1;
driver.switchTo().window(set.toArray(new String[0]));
driver.close();
driver.switchTo().window(base);
It works for me
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码
