gpt4 book ai didi

java - 如何使用 Java 处理 Selenium WebDriver 中的新窗口?

转载 作者:太空狗 更新时间:2023-10-29 22:37:05 27 4
gpt4 key购买 nike

这是我的代码:

driver.findElement(By.id("ImageButton5")).click();
//Thread.sleep(3000);
String winHandleBefore = driver.getWindowHandle();
driver.switchTo().window(winHandleBefore);
driver.findElement(By.id("txtEnterCptCode")).sendKeys("99219");

现在我有下一个错误:

Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to find element with id == txtEnterCptCode (WARNING: The server did not provide any stacktrace information) Command duration or timeout: 404 milliseconds.

有什么想法吗?

最佳答案

看起来您实际上并没有切换到任何新窗口。您应该获取原始窗口的窗口句柄,保存它,然后获取新窗口的窗口句柄并切换到该窗口。完成新窗口后,您需要将其关闭,然后切换回原始窗口句柄。请参阅下面的示例:

String parentHandle = driver.getWindowHandle(); // get the current window handle
driver.findElement(By.xpath("//*[@id='someXpath']")).click(); // click some link that opens a new window

for (String winHandle : driver.getWindowHandles()) {
driver.switchTo().window(winHandle); // switch focus of WebDriver to the next found window handle (that's your newly opened window)
}

//code to do something on new window

driver.close(); // close newly opened window when done with it
driver.switchTo().window(parentHandle); // switch back to the original window

关于java - 如何使用 Java 处理 Selenium WebDriver 中的新窗口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19112209/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com