gpt4 book ai didi

java - 在 selenium Webdriver 中从父窗口切换到子窗口时出错,反之亦然

转载 作者:行者123 更新时间:2023-12-01 14:02:33 25 4
gpt4 key购买 nike

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();
Thread.sleep(3000);
driver.findElement(By.id("chklstAllprocedure_0")).click();
Thread.sleep(3000);
driver.findElement(By.id("btnOk")).click();
Thread.sleep(5000);

/*ALL MODIFIERS 1*/

driver.switchTo().window(Parentwindow);
System.out.println(driver.getTitle());
//Thread.sleep(4000);
driver.switchTo().frame("ctl00_ContentPlaceHolder1_iframeID1");
Thread.sleep(3000);
driver.findElement(By.xpath("//*[@id='ImageButton17']")).click();
Thread.sleep(3000);
for(String winHandle1 : driver.getWindowHandles()){
driver.switchTo().window(winHandle1);
Thread.sleep(3000);
}
System.out.println("Title of the page after - switchingTo: " + driver.getTitle());
driver.findElement(By.id("txtEnterModifier")).sendKeys("RT");
Thread.sleep(3000);
driver.findElement(By.id("btnSearch")).click();
Thread.sleep(3000);
driver.findElement(By.id("chklstAllModifiers_0")).click();
Thread.sleep(3000);
driver.findElement(By.id("btnOk")).click();
Thread.sleep(3000);

这是我的代码。有时,当我尝试在父窗口和子窗口之间切换时,我的代码运行良好,没有错误,有时会显示以下错误消息:

"Exception in thread "main" org.openqa.selenium.NoSuchWindowException: Unable to get browser (WARNING: The server did not provide any stacktrace information).
Command duration or timeout: 14 milliseconds".

请帮我解决这个问题。提前致谢。

最佳答案

您的方法不能保证您的驱动程序将切换到哪个窗口。我有一个实用方法可以切换到所需的窗口,如下所示

public class Utility 
{
public static WebDriver getHandleToWindow(String title){

//parentWindowHandle = WebDriverInitialize.getDriver().getWindowHandle(); // save the current window handle.
WebDriver popup = null;
Set<String> windowIterator = WebDriverInitialize.getDriver().getWindowHandles();
System.err.println("No of windows : " + windowIterator.size());
for (String s : windowIterator) {
String windowHandle = s;
popup = WebDriverInitialize.getDriver().switchTo().window(windowHandle);
System.out.println("Window Title : " + popup.getTitle());
System.out.println("Window Url : " + popup.getCurrentUrl());
if (popup.getTitle().equals(title) ){
System.out.println("Selected Window Title : " + popup.getTitle());
return popup;
}

}
System.out.println("Window Title :" + popup.getTitle());
System.out.println();
return popup;
}
}

一旦窗口标题作为参数传递,它将带您到所需的窗口。根据您的情况,您可以这样做。

Webdriver childDriver = Utility.getHandleToWindow("titleOfChildWindow");

然后使用相同的方法再次切换到父窗口

Webdriver parentDriver = Utility.getHandleToWindow("titleOfParentWindow");

此方法在处理多个窗口时有效

关于java - 在 selenium Webdriver 中从父窗口切换到子窗口时出错,反之亦然,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19240064/

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