C# 如何在C中实现ExpectedConditions.AlertIsPresent

C# 如何在C中实现ExpectedConditions.AlertIsPresent,c#,selenium,selenium-webdriver,C#,Selenium,Selenium Webdriver,如果在所需等待期内未找到警报,WebDriverWait将引发WebDriverTimeoutException异常 在WebDriverWait周围使用try catch块捕获WebDriverTimeoutException 我使用如下扩展方法: new WebDriverWait(Driver, TimeSpan.FromSeconds(5)) { Message = "Waiting for alert to appear" }.Until(d => MyExpectedCondi

如果在所需等待期内未找到警报,WebDriverWait将引发WebDriverTimeoutException异常

在WebDriverWait周围使用try catch块捕获WebDriverTimeoutException

我使用如下扩展方法:

new WebDriverWait(Driver, TimeSpan.FromSeconds(5)) { Message = "Waiting for alert to appear" }.Until(d => MyExpectedConditions.AlertIsPresent());
Driver.SwitchTo().Alert().Accept();
然后像这样使用它:

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

    }
}
public static IAlert WaitGetAlert(this IWebDriver driver, int waitTimeInSeconds = 5)
{
    IAlert alert = null;

    WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(waitTimeInSeconds));

    try 
    {
        alert = wait.Until(d =>
            {
                try
                {
                    // Attempt to switch to an alert
                    return driver.SwitchTo().Alert();
                }
                catch (NoAlertPresentException)
                {
                    // Alert not present yet
                    return null;
                }
            });
    }
    catch (WebDriverTimeoutException) { alert = null; }

    return alert;
}
如果在所需等待期内未找到警报,WebDriverWait将引发WebDriverTimeoutException异常

在WebDriverWait周围使用try catch块捕获WebDriverTimeoutException

我使用如下扩展方法:

new WebDriverWait(Driver, TimeSpan.FromSeconds(5)) { Message = "Waiting for alert to appear" }.Until(d => MyExpectedConditions.AlertIsPresent());
Driver.SwitchTo().Alert().Accept();
然后像这样使用它:

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

    }
}
public static IAlert WaitGetAlert(this IWebDriver driver, int waitTimeInSeconds = 5)
{
    IAlert alert = null;

    WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(waitTimeInSeconds));

    try 
    {
        alert = wait.Until(d =>
            {
                try
                {
                    // Attempt to switch to an alert
                    return driver.SwitchTo().Alert();
                }
                catch (NoAlertPresentException)
                {
                    // Alert not present yet
                    return null;
                }
            });
    }
    catch (WebDriverTimeoutException) { alert = null; }

    return alert;
}

WebDriverWait是否在下一行返回OK并出错?WebDriverWait是否在下一行返回OK并出错?