Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
320 views
in Technique[技术] by (71.8m points)

java - Selenium automatically accepting alerts

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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:

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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...