using System;
using OpenQA.Selenium;
namespace MyApplication.Selenium.Tests.Source
{
public sealed class MyExpectedConditions
{
private void ExpectedConditions()
{
}
public static Func<IWebDriver, IAlert> AlertIsPresent()
{
return (driver) =>
{
try
{
return driver.SwitchTo().Alert();
}
catch (NoAlertPresentException)
{
return null;
}
};
}
}
}
你可以像这样使用它:
new WebDriverWait(Driver, TimeSpan.FromSeconds(5)) { Message = "Waiting for alert to appear" }.Until(d => MyExpectedConditions.AlertIsPresent());
Driver.SwitchTo().Alert().Accept();