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.
Asked
Active
Viewed 1.0k times
6 Answers
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());

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

Jose Paulo
- 1
- 2