0
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".  

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

4

1 回答 1

1

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

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");

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

于 2013-10-08T07:53:42.450 回答