2

After clicking search on a webpage, alert pop up will appear. I need to wait until the pop up appears. i have to do without using Thread.sleep.

user3679951
  • 21
  • 1
  • 1
  • 2

6 Answers6

3

ExpectedConditions class has specific wait for alert pop-up.

WebDriverWait wait = new WebDriverWait(webDriver, timeoutInSeconds);
wait.until(ExpectedConditions.alertIsPresent());
Artem Yakovlev
  • 493
  • 7
  • 22
1

ExpectedConditions is obsolete, so try to use this:

        WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(15));
        wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.AlertIsPresent());

C# Selenium 'ExpectedConditions is obsolete'

CarolCiola
  • 111
  • 10
0

You could use wait command:

WebDriverWait wait = new WebDriverWait(webDriver, timeoutInSeconds);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id<locator>));
Morvader
  • 2,317
  • 3
  • 31
  • 44
0

try this one, the drawback is there is no timeout for this one. it must be ensured that the popup will appear.

while (driver.WindowHandles.Count == 1)
{
    //leave it blank
}
Daanzel
  • 987
  • 7
  • 6
0

What about this ???

public static class SeleniumExtensions
{
    public static IAlert WaitAlert(this ITargetLocator locator, int seconds = 10)
    {
        while (true) 
        {
            try { return locator.Alert(); }
            catch (NoAlertPresentException) when (--seconds >= 0) { Thread.Sleep(1000); }
        }
    }
}

works for me:

var alert = _driver.SwitchTo().WaitAlert();
iojancode
  • 610
  • 6
  • 7
-1

it's a little bit rude, but work for me.

public IAlert WaitAlert(int timeOut)
    {
        for (int cont = timeOut; cont > 0; cont--)
        {
            try
            {
                return driver.SwitchTo().Alert();
            }
            catch (NoAlertPresentException erro)
            {
                Thread.Sleep(TimeSpan.FromSeconds(0.5));
            }
        }
        throw new NoAlertPresentException();
    }