Selenium WebDriver with Java:无法接受警报 [英] Selenium WebDriver with Java: Can't accept alert

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

问题描述

在selenium IDE中录制时,我可以在弹出窗口中点击确定按钮,并希望能够使用

When recording in selenium IDE I can click the "OK" button in a popup, and expected to be able to click it using

driver.findElement(By.linkText("OK")).click();

但事实并非如此。

同样这不起作用。

driver.switchTo().alert().accept();

Selenium会抛出NoAlertPresent异常。如果弹出的不是警报,那么它是什么?我如何点击是!

Selenium throws a NoAlertPresent exception. If what's popping up is not an alert, then what is it? And how do I click yes!

推荐答案

在这种情况下,我更愿意检查(验证)页面上的警报状态然后如果存在 - 接受它。
它类似于:

in such case I'd prefer to check(verify) the alert presence on the page and then if is present - accept it. It be somthing like:

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.

希望这可以帮到你。

这篇关于Selenium WebDriver with Java:无法接受警报的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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