我需要知道何时ISearchContext是可点击的

时间:2016-12-21 14:01:27

标签: c# visual-studio selenium selenium-webdriver

我一直在很多地方搜索这个话题,我在这里找到了类似的答案,但我还想多一点。

我的代码:

var element = myWebDriver.FindElement("User_Login");
var finalelement = element.FindElement(By.XPath("//button[contains(text(),'OK')]"));

问题在于,有时它被点击的按钮才可用,我想明确等待。现在我有这些:

public static class WebDriverExtensions
    {
        public static IWebElement NGFindElementWhenClickable(this IWebDriver driver, By by)
        {
            var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(int.Parse(ConfigurationManager.AppSettings["WebDriverExplicitWait"])));
            return wait.Until(ExpectedConditions.ElementToBeClickable(by));
        }

        public static IWebElement NGFindElementWhenVisible(this IWebDriver driver, By by)
        {
            var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(int.Parse(ConfigurationManager.AppSettings["WebDriverExplicitWait"])));
            return wait.Until(ExpectedConditions.ElementIsVisible(by));
        }

        public static IWebElement NGElementExist(this IWebDriver driver, By by)
        {
            var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(int.Parse(ConfigurationManager.AppSettings["WebDriverExplicitWait"])));
            return wait.Until(ExpectedConditions.ElementExists(by));
        }

        public static void NGElementInvisible(this IWebDriver driver, By by)
        {
            var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(int.Parse(ConfigurationManager.AppSettings["WebDriverExplicitWait"])));
            wait.Until(ExpectedConditions.InvisibilityOfElementLocated(by));
        }

我想在该课程中添加类似的内容:

public static IWebElement NGFindElement(this ISearchContext context, By by)
        {
            var wait = new DefaultWait<ISearchContext>(context);
            wait.Timeout = TimeSpan.FromSeconds(int.Parse(ConfigurationManager.AppSettings["WebDriverExplicitWait"]));            
            return wait.Until(ctx => {
                var elem = ctx.FindElement(by);
                return elem;
            });
        }

...但是有明确的等待(有条件的等待)。

1 个答案:

答案 0 :(得分:0)

我解决了这个问题,我知道有点晚了:)

我是怎么做到的:

public static IWebElement NGFindElement(this ISearchContext context, By by, int timeOut = 0)
{
        //-> If the timeOut remain the same ( = 0) use the time defined on "ConfigurationManager.AppSettings["WebDriverExplicitWait"]"
        //-> on the other hand if the value is defined by the user, will use the defined value
        int totalTime = (timeOut == 0) ? int.Parse(ConfigurationManager.AppSettings["WebDriverExplicitWait"]) : timeOut;
        var wait = new DefaultWait<ISearchContext>(context);
        wait.Timeout = TimeSpan.FromSeconds(totalTime);
        //-> To avaid exceptions related to page refreshed
        wait.IgnoreExceptionTypes(typeof(StaleElementReferenceException));
        return wait.Until(ctx => (ctx.FindElement(by).Displayed && ctx.FindElement(by).Enabled) ? ctx.FindElement(by) : null);            
}