示例#1
0
        // Method to find element along with explicit wait
        public static IWebElement WaitFindElement(By locator)
        {
            // Set null as a default value for element expected to return
            IWebElement expectedElement = null;

            // Disable implicit wait in order to don't mix with explicit wait
            DriverContext.TurnOffImplicitWait();

            // Initialize instance of explicit wait
            WebDriverWait wait = new WebDriverWait(DriverContext.Driver, TimeSpan.FromSeconds(10));

            // Set custom polling interval
            wait.PollingInterval = TimeSpan.FromMilliseconds(200);

            // Set condition for explicit wait
            wait.Until(condition =>
            {
                try
                {
                    expectedElement = DriverContext.Driver.FindElement(locator);
                    return(expectedElement.Displayed);
                }
                catch (StaleElementReferenceException ex)
                {
                    Debug.WriteLine(ex.Message);
                    return(false);
                }
                catch (NoSuchElementException ex)
                {
                    Debug.WriteLine(ex.Message);
                    return(false);
                }
            });

            // Turn implicit wait back on
            DriverContext.Driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(5);

            // Return expected element
            return(expectedElement);
        }