【问题标题】:How to switch back to main window after handling alert in Selenium Web Driver在 Selenium Web Driver 中处理警报后如何切换回主窗口
【发布时间】:2013-09-27 08:48:32
【问题描述】:

我必须编写一个 selenium 自动化测试脚本,其中测试必须创建一个模板,在表单中填写 Generalize 数据并手动输入特定详细信息(尝试在此处等待手动输入完成),然后单击 SAVE 按钮。但是,如果留下一些必填字段,系统会显示 JavaScript 的验证警报。我正在使用处理此警报 警报警报 = driver.switchTo().alert(); 警报.accept(); 在此之后,我想回到主页并等待几分钟来为缺少的字段写描述,然后点击保存按钮。

如何在 Selenium 网络驱动程序中实现这一点?

【问题讨论】:

  • 仅仅因为您已切换到警报并不意味着控件会永久保留在警报中。一旦你执行点击 ok 控件就会返回到主页。

标签: selenium alerts


【解决方案1】:

当您首先处理警报时,您必须检查是否存在警报。我会使用这种方法:

public boolean isAlertPresent() {

  boolean presentFlag = false;

  try {

   // Check the presence of alert
   Alert alert = driver.switchTo().alert();
   // Alert present; set the flag
   presentFlag = true;
   // if present consume the alert
   alert.accept();

  } catch (NoAlertPresentException ex) {
   // Alert not present
   ex.printStackTrace();
  }

  return presentFlag;

click here 中了解更多信息,也不要忘记逐步调试以了解出现/不出现的步骤警报。希望对您有所帮助。

【讨论】:

    【解决方案2】:

    接受警报框后。我们想通过获取窗口句柄详细信息来切换到主窗口。

    //Stores the handle details
    
        String windowHandle=driver.getWindowHandles();
    
    //Alert portion
    
        Alert alert = driver.switchTo().alert();
    
        alert.accept();
    
    //After switching back to main window..
    
        driver.navigate.windows(windowHandle);
    
    //It takes to the main window....
    

    【讨论】:

      【解决方案3】:

      您可以尝试以下代码: driver.navigate.back();

      【讨论】:

        【解决方案4】:

        您可以使用switchTo() 方法从父窗口转到警报窗口:

        <html>
        Alert alrt = driver.switchTo().alert();   // Switching the control to Alert window
        
        alrt.accept(); // to accept Yes to delete the task
        
        //alrt.dismiss(); 
        
        // to dismiss the action - once you click on YES, of course you don't have option to dismiss
        </html>
        

        现在,回答你的问题 -

        一旦您从警报窗口中选择任何选项(例如 - 是/否),控件就会自动返回到父窗口或主窗口。你知道吗,隐藏的 div 也是如此。切换回父窗口是 HTML 弹出窗口的问题。

        希望这会有所帮助。

        【讨论】:

        • alert.accept() 之后;控制确实返回到主窗口。但是脚本中的下一条语句不会执行。另外 driver.manage().timeouts().implicitlywait(20, timeunit.MINUTES) 似乎不起作用。(尝试等待 20 分钟。让用户手动输入特定数据,并在 20 分钟后。自动脚本将点击保存按钮)
        • 我和@Mitaleek 有同样的问题,有人找到解决方案了吗?
        猜你喜欢
        • 2015-03-06
        • 2015-12-16
        • 1970-01-01
        • 1970-01-01
        • 2021-02-13
        • 1970-01-01
        • 2015-09-21
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多