警报处理 + Java + webdriver [英] Alert Handling + Java + webdriver

查看:23
本文介绍了警报处理 + Java + webdriver的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 switch 来提醒并执行操作,但我遇到了错误.

I am trying to use switch to alert and perform an action but i face error.

现在真正的问题是当我将下面的代码放在 try 中时,它可以完美运行.我的意思是它完美地处理了警报.但是当我在没有尝试的情况下使用相同的代码时,它会抛出以下异常

Now the real issue is when i put the below code in try,catch it works perfectly. i mean it handles the alert perfectly. But when i use the same without try, catch code it throws the below exception

  Alert alert = driver.switchTo().alert();
            String AlertText = alert.getText();
System.out.println(javascriptconfirm.getText());
            alert.accept();

请找出下面的错误

No alert is present (WARNING: The server did not provide any stacktrace information)

推荐答案

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

The idea is when you deal with alerts you have to check whether alert is present first. 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;

 }

这里你可以得到详细信息也不要忘记逐步调试以了解出现/不出现警报的步骤.希望这对您有所帮助.

here you can get details Also do not forget about debug step by step to get to know on what step alert appears/not appears. Hope this helps you.

这篇关于警报处理 + Java + webdriver的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆