Selenium自动接受警报

编程入门 行业动态 更新时间:2024-10-10 05:23:02
本文介绍了Selenium自动接受警报的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

有谁知道如何禁用此功能?或者如何从已经自动接受的警报中获取文本?

Does anyone know how to disable this? Or how to get the text from alerts that have been automatically accepted?

此代码需要工作,

driver.findElement(By.xpath("//button[text() = \"Edit\"]")).click();//causes page to alert() something Alert alert = driver.switchTo().alert(); alert.accept(); return alert.getText();

但反过来会出现此错误

No alert is present (WARNING: The server did not provide any stacktrace information) Command duration or timeout: 2.14 seconds

我正在使用FF 20和Selenium 2.32

I am using FF 20 with Selenium 2.32

推荐答案

其他那天我回答了类似的事情,所以它还很新鲜。你的代码失败的原因是,如果代码处理时没有显示警报,它将大部分失败。

Just the other day i've answered something similar to this so it's still fresh. The reason your code is failing is if the alert is not shown by the time the code is processed it will mostly fail.

谢天谢地,来自Selenium WebDriver的人已经等待它了。对于你的代码就像这样简单:

Thankfully, the guys from Selenium WebDriver have a wait already implemented for it. For your code is as simple as doing this:

String alertText = ""; WebDriverWait wait = new WebDriverWait(driver, 5); // This will wait for a maximum of 5 seconds, everytime wait is used driver.findElement(By.xpath("//button[text() = \"Edit\"]")).click();//causes page to alert() something wait.until(ExpectedConditions.alertIsPresent()); // Before you try to switch to the so given alert, he needs to be present. Alert alert = driver.switchTo().alert(); alertText = alert.getText(); alert.accept(); return alertText;

您可以从 ExpectedConditions 找到所有API 这里,如果您想要此方法背后的代码这里。

You can find all the API from ExpectedConditions here, and if you want the code behind this method here.

此代码也解决了这个问题,因为你无法返回alert.getText( )关闭警报后,我会为你存储一个变量。

This code also solves the problem because you can't return alert.getText() after closing the alert, so i store in a variable for you.

更多推荐

Selenium自动接受警报

本文发布于:2023-11-27 17:58:59,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1639026.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:警报   Selenium

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!