看来您实际上并没有切换到任何新窗口。您应该获得原始窗口的窗口句柄,将其保存,然后获取新窗口的窗口句柄并切换到该窗口。完成新窗口的操作后,您需要将其关闭,然后切换回原始窗口句柄。请参阅下面的示例:
即
String parentHandle = driver.getWindowHandle(); // get the current window handledriver.findElement(By.xpath("//*[@id='someXpath']")).click(); // click some link that opens a new windowfor (String winHandle : driver.getWindowHandles()) { driver.switchTo().window(winHandle); // switch focus of WebDriver to the next found window handle (that's your newly opened window)}//pre to do something on new windowdriver.close(); // close newly opened window when done with itdriver.switchTo().window(parentHandle); // switch back to the original window


