C#: Wait for element to contain specific attribute in Selenium

Viewed 478
2 Answers

Additionally to the way presented in the post you have mentioned in your question you can also do the following:
Let's say that element can be uniquely located by the following XPath //tag[@class='theClass']
So when this element will have aria-sort attribute equals to descending, then this element can be located by this XPath: //tag[@class='theClass' and(@aria-sort='descending')]
So you can simply use regular ElementExists ExpectedConditions as following:

boolean status = new WebDriverWait(driver, 20).until(ExpectedConditions.ElementExists(By.xpath("//tag[@class='theClass' and(@aria-sort='descending')]"));

However this approach is not really good since it is not general enough, so it's much better to create custom ExpectedConditions according to the element attribute as described here

I have Written an extension method in C# Selenium , this will help to wait until a particular element / attribute is visible.

Call This method like : WaitUntilElementIsVisible(driver, By.Id("DemoId")); // example

public static bool WaitUntilElementIsVisible(IWebDriver browser, By by)
{
            int attemptToFindElement = 0;
            bool elementFound = false;
            IWebElement elementIdentifier = null;
            do
            {
                attemptToFindElement++;
                try
                {
                    elementIdentifier = browser.FindWebElement(by);
                    elementFound = (elementIdentifier.Displayed && elementIdentifier.Enabled) ? true : false;
                }
                catch (Exception)
                {
                    elementFound = false;
                }

            }
            while (elementFound == false && attemptToFindElement < 100);

            return elementFound;
}
Related