简体   繁体   English

带有Java的Selenium WebDriver:无法接受警报

[英]Selenium WebDriver with Java: Can't accept alert

When recording in selenium IDE I can click the "OK" button in a popup, and expected to be able to click it using 在Selenium IDE中录制时,我可以在弹出窗口中单击“确定”按钮,并希望能够使用以下命令单击它

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

but this was not the case. 但是事实并非如此。

Similarly this doesn't work. 同样,这不起作用。

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

Selenium throws a NoAlertPresent exception. Selenium引发NoAlertPresent异常。 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. 在这里您可以获得详细信息,也不要忘记逐步调试。

Hope this helps you. 希望这对您有所帮助。

It could be anything. 可能是任何东西。 You should be telling us that. 您应该告诉我们。

If it is a Java Script alert then, this should work 如果是Java脚本警报,则应该可以

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

At the very least you could try sending enter/return key stroke, if the "OK" button is autoselected/highlighted by the web app. 如果Web应用程序自动选择/突出显示了“确定”按钮,则至少可以尝试发送回车/回车按键。

import org.openqa.selenium.Keys
WebElement.sendKeys(Keys.RETURN);

Update 更新


It could also be because your alert is not present at the time you are trying to click/accept it. 也可能是因为您试图单击/接受警报时不存在警报。 For a quick check put in a sleep of 4-5 seconds and then try driver.switchTo().alert().accept(); 要进行快速检查,请进入4-5秒的睡眠状态,然后尝试driver.switchTo().alert().accept(); . Once it is ascertained, then put in a wait for alert present in a try and catch loop (any exception handling). 一旦确定,就在try and catch循环中等待警报(任何异常处理)。

if you are using latest version of webdriver, infact anything above 2.20 then 如果您使用的是最新版本的Webdriver,请在2.20以上的值上填写

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

should work provided the alert is a javascript alert similar to the one we get when we click 如果警报是与我们单击时收到的javascript警报类似的javascript警报,则应该可以正常工作

alert demo OR confirm pop-up demo 警报演示确认弹出演示

Updated 更新

here this code will help you accept the alert 此代码将帮助您接受警报

driver = new FirefoxDriver();
String baseUrl = "http://www.w3schools.com/js/tryit.asp?filename=tryjs_alert";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get(baseUrl);
driver.switchTo().frame(0);
driver.findElement(By.cssSelector("input[type=\"button\"]")).click();
driver.switchTo().alert().accept();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM