Java Selenium 自动接受警报
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16448173/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Selenium automatically accepting alerts
提问by Zackkenyon
Does anyone know how to disable this? Or how to get the text from alerts that have been automatically accepted?
有谁知道如何禁用它?或者如何从已自动接受的警报中获取文本?
This code needs to work,
这段代码需要工作,
driver.findElement(By.xpath("//button[text() = \"Edit\"]")).click();//causes page to alert() something
Alert alert = driver.switchTo().alert();
alert.accept();
return alert.getText();
but instead gives this error
而是给出了这个错误
No alert is present (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 2.14 seconds
I am using FF 20 with Selenium 2.32
我正在使用 FF 20 和 Selenium 2.32
采纳答案by aimbire
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.
就在前几天,我回答了类似的问题,所以它仍然很新鲜。您的代码失败的原因是,如果在处理代码时未显示警报,则大多数情况下都会失败。
Thankfully, the guys from Selenium WebDriver have a wait already implemented for it. For your code is as simple as doing this:
谢天谢地,来自 Selenium WebDriver 的人已经为它实现了一个等待。因为你的代码就像这样做一样简单:
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;
You can find all the API from ExpectedConditions
here, and if you want the code behind this method here.
您可以从ExpectedConditions
此处找到所有 API ,如果您想要此方法背后的代码,请在此处找到。
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.
这段代码也解决了这个问题,因为你不能在关闭警报后返回 alert.getText(),所以我为你存储在一个变量中。
回答by so cal cheesehead
Before you accept() the alert you need to get the text. What you're doing right now is accepting (clicking "OK") on the alert thentrying to get the alerts text after it's out of the screen, i.e. no alert present.
在接受()警报之前,您需要获取文本。您现在正在做的是接受(单击“确定”)警报,然后在它离开屏幕后尝试获取警报文本,即不存在警报。
Try the following, I just added a String that retrieves the alert text then return that string instead.
尝试以下操作,我只是添加了一个字符串来检索警报文本,然后返回该字符串。
driver.findElement(By.xpath("//button[text() = \"Edit\"]")).click();//causes page to
Alert alert = driver.switchTo().alert();
String alertText = alert.getText();
alert.accept();
return alertText;
回答by Amey
Selenium webdriver does not wait
for the alert.
So it will try to switch to a non-existent alert and thats why it fails.
Selenium webdriver 不wait
用于警报。所以它会尝试切换到一个不存在的警报,这就是它失败的原因。
For a quick and not so good fix, put in a sleep
.
要快速但不太好的修复,请放入sleep
.
A better solution would be to implement your own wait for alert method, before trying to switch to the alert.
更好的解决方案是在尝试切换到警报之前实现您自己的等待警报方法。
UPDATE
更新
Something like this, copy pasted from here
像这样的东西,从这里复制粘贴
waitForAlert(WebDriver driver)
{
int i=0;
while(i++<5)
{
try
{
Alert alert3 = driver.switchTo().alert();
break;
}
catch(NoAlertPresentException e)
{
Thread.sleep(1000)
continue;
}
}
}
回答by Santhosh Raja
Following method with synchronized option will add more stability
以下带有同步选项的方法将增加更多稳定性
protected Alert getAlert(long wait) throws InterruptedException
{
WebDriverWait waitTime = new WebDriverWait(driver, wait);
try
{
synchronized(waitTime)
{
Alert alert = driver.switchTo().alert();
// if present consume the alert
alert.accept();
return alert;
}
}
catch (NoAlertPresentException ex)
{
// Alert not present
return null;
}
}
回答by kizziah
Here is the JavaScript answer. The documentation has examples for all languages. https://www.selenium.dev/documentation/en/webdriver/js_alerts_prompts_and_confirmations/
这是 JavaScript 的答案。该文档包含所有语言的示例。 https://www.selenium.dev/documentation/en/webdriver/js_alerts_prompts_and_confirmations/
await driver.wait(until.alertIsPresent());
el = driver.switchTo().alert();
await el.accept();