3

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

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

4

4 回答 4

1

when you deal with alerts firstly you have to check whether alert is present. I would use this approach:

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;

for more in click here, Also do not forget about debug step by step to get to know on what step alert appears/not appears. Hope this helps you.

于 2013-09-27T09:17:44.180 回答
1

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

//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....
于 2015-06-09T07:17:41.763 回答
0

您可以使用以下方法从父窗口转到警报窗口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 弹出窗口的问题。

希望这可以帮助。

于 2013-09-27T13:01:45.310 回答
0

你可以试试下面的代码:driver.navigate.back();

于 2013-09-27T09:41:54.983 回答