7

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

Zackkenyon
  • 352
  • 1
  • 2
  • 18

5 Answers5

11

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.

Jeanne Boyarsky
  • 12,156
  • 2
  • 49
  • 59
aimbire
  • 3,697
  • 1
  • 15
  • 28
2

Before you accept() the alert you need to get the text. What you're doing right now is accepting (clicking "OK") on the alert then trying 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;
so cal cheesehead
  • 2,515
  • 4
  • 29
  • 50
1

Selenium webdriver does not wait for the alert. So it will try to switch to a non-existent alert and thats why it fails.

For a quick and not so good fix, put in a 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;
        }
   }
}
Community
  • 1
  • 1
Amey
  • 8,470
  • 9
  • 44
  • 63
  • 2
    Wow, you're advising people to use sleep really? – aimbire May 09 '13 at 12:01
  • I just can't get it, how can advocate usage of this. The code from isAlertPresent() is similar, but no need to use a sleep(). – aimbire May 09 '13 at 19:12
  • In my use case, the default wait in isAlertPresent() does not work. When running our tests on a bogged down server it fails and hangs trying to find the alert. The custom wait allows for multiple tries and has worked for us. – Justin Oct 10 '19 at 16:31
1

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;
    }

}
Kavan
  • 569
  • 4
  • 21
0

Here is the JavaScript answer. The documentation has examples for all languages. https://www.selenium.dev/documentation/en/webdriver/js_alerts_prompts_and_confirmations/

await driver.wait(until.alertIsPresent());    
el = driver.switchTo().alert();
await el.accept();
kizziah
  • 69
  • 1
  • 5