【问题标题】:Webpage >> Frame >> popup window handling网页>>框架>>弹出窗口处理
【发布时间】:2015-06-12 14:25:04
【问题描述】:

我有这个网页,我正在尝试自动化。以下是应用程序的步骤和功能。

  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

【问题讨论】:

  • 最后一步肯定失败了,因为在关闭日历弹出窗口后,控件必须只返回框架而不是主窗口。
  • 但即使我评论了 Driver.switchTo().frame(0);然后脚本也会崩溃。
  • 是否有任何进一步的步骤导致脚本崩溃?
  • if(afterPopup.size() == 1) { Driver.switchTo().window((String)afterPopup.toArray()[0]); Driver.findElement(By.id("submitButton")).click(); // 切换窗口并在日历弹出窗口中单击“确定”会自动关闭它 } Driver.switchTo().frame(0);
  • Driver.findElement(By.name("submitorderform")).click(); - 这是我试图在框架中执行的下一步

标签: java selenium selenium-webdriver


【解决方案1】:

试试这样:

        String mainWindow = driver.getWindowHandle();
        Set<String> 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();
        }

【讨论】:

    猜你喜欢
    • 2020-11-25
    • 2021-01-11
    • 2018-06-07
    • 1970-01-01
    • 2021-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多