切换到新窗口并再次进入java中的同一窗口(SeleniumWebDriver)
我使用此代码切换到新窗口。但它显示的错误如下:切换到新窗口并再次进入java中的同一窗口(SeleniumWebDriver),java,selenium,Java,Selenium,我使用此代码切换到新窗口。但它显示的错误如下: String Parentwindow = driver.getWindowHandle(); driver.findElement(By.xpath("//*[@id='ImageButton5']")).click(); Thread.sleep(3000); for(String winHandle : driver.getWindowHandles()) { driver.switchTo().window(winHandle)
String Parentwindow = driver.getWindowHandle();
driver.findElement(By.xpath("//*[@id='ImageButton5']")).click();
Thread.sleep(3000);
for(String winHandle : driver.getWindowHandles())
{
driver.switchTo().window(winHandle);
Thread.sleep(3000);
}
System.out.println("Title of the page after - switchingTo: " + driver.getTitle());
driver.findElement(By.id("txtEnterCptCode")).sendKeys("99219");
//Thread.sleep(3000);
driver.findElement(By.id("btnSearch")).click();
请任何人帮我解决这个问题。调用
getWindowHandles()
返回的Windows句柄不保证以任何顺序排列。您的代码始终假定新打开的窗口将位于返回的句柄列表的末尾。实际上,您需要以下内容:
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: 351 milliseconds
调用
getWindowHandles()
返回的Windows句柄不保证按任何顺序排列。您的代码始终假定新打开的窗口将位于返回的句柄列表的末尾。实际上,您需要以下内容:
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: 351 milliseconds
您还可以将这些方法添加到基类中,这对于此类事情非常有用-
// WARNING! Untested code written without benefit of an IDE.
// May not work exactly correctly, or even compile as-is.
String parentWindow = driver.getWindowHandle();
driver.findElement(By.xpath("//*[@id='ImageButton5']")).click();
Thread.sleep(3000);
for(String winHandle : driver.getWindowHandles()) {
if (!parentWindow.equals(winHandle)) {
driver.switchTo().window(winHandle);
Thread.sleep(3000);
break;
}
}
System.out.println("Title of the page after - switchingTo: " + driver.getTitle());
最后一个方法特别有用,因为如果窗口句柄在关闭前的大小为2,它将自动将上下文返回到基窗口。您还可以将这些方法添加到基类中,这对此类操作非常有用-
// WARNING! Untested code written without benefit of an IDE.
// May not work exactly correctly, or even compile as-is.
String parentWindow = driver.getWindowHandle();
driver.findElement(By.xpath("//*[@id='ImageButton5']")).click();
Thread.sleep(3000);
for(String winHandle : driver.getWindowHandles()) {
if (!parentWindow.equals(winHandle)) {
driver.switchTo().window(winHandle);
Thread.sleep(3000);
break;
}
}
System.out.println("Title of the page after - switchingTo: " + driver.getTitle());
最后一种方法特别有用,因为如果窗口句柄在关闭前的大小为2,它将自动将上下文返回到基本窗口。您可以发布一个指向您试图访问的页面的链接,或者发布一个html转储吗?您可以发布一个链接到您试图访问的页面,或者一个html转储吗?我尝试了你的代码,但发生了相同的错误。你能告诉我其他的解决方法吗?谢谢你的帮助JimI尝试了你的代码同样的错误发生了。你能告诉我其他的解决办法吗?谢谢你的帮助,吉姆