网页>>框架>>弹出窗口处理

我有这个网页,我试图自动化。 这是应用程序的步骤和function。

  1. 使用Selenium启动应用程序
  2. 在单击某个按钮时,将在主网页上弹出一个框架
  3. 我能够通过selenium web驱动程序执行框架操作
  4. 但是在同一帧中有一个日历图标。
  5. 一旦您点击日历图标,弹出一个带有日历和确定按钮的弹出窗口。
  6. 我可以在弹出窗口中选择日期,只要单击“确定”按钮,弹出窗口就会自动关闭。
  7. 现在我想再次切换到FRame以执行进一步的操作。 但我不能这样做。
  8. 我的脚本崩溃,错误“未找到窗口。浏览器窗口可能已关闭。”

下面是我的代码的一部分

Driver.findElement(By.xpath("//input[@id='VDC_VM_ScheduledTask.RequestedDateTime.ControlImage']")).click(); // clicking on calendar icon Set afterPopup = Driver.getWindowHandles(); System.out.println(afterPopup); afterPopup.removeAll(beforePopup); System.out.println(afterPopup.size()); if(afterPopup.size() == 1) { Driver.switchTo().window((String)afterPopup.toArray()[0]); Driver.findElement(By.id("submitButton")).click(); // Switching windows and clicking Ok in calendar pop up window which closes it automatically } Driver.switchTo().frame(0); // this step fails 

试试这样:

  String mainWindow = driver.getWindowHandle(); Set windows= driver.getWindowHandles(); if (windows.size() > 1) { //first remove main window windows.remove(mainWindow); //switching to new/child window and perform some action on new window if required. Then close it. driver.switchTo().window(windows.iterator().next()); driver.close(); //switching back to mainWindow, Then continue with your actions. driver.switchTo().window(mainWindow); windows.clear(); }