1

I have been working on making my Selenium Framework a Page Factory, however i am struggling to get the Wait.Until commands working in my Extension Class.

public static void Wait(this IWebElement element, IWebDriver driver, float TimeOut)
{
    WebDriverWait Wait = new WebDriverWait(driver, TimeSpan.FromSeconds(TimeOut));
    return Wait.Until(ExpectedConditions.ElementIsVisible(element));
}

If I use the above code I get the error Cannot Convert from OpenQA.Selenium.IWebElement to Open.Qa.Selenium.By

Any suggestions how can I amend the code above to make it work in the By model I am using?

2 Answers 2

6

There is no ExpectedConditions.ElementIsVisible(IWebElement). Unfortunately, you can only use ElementIsVisible with By objects.

If appropriate, you could substitute with ExpectedConditions.ElementToBeClickable(IWebElement), which is a slightly different case that also checks that the element is enabled in addition to being visible. But this may satisfy your requirement.

Alternatively, you could just call element.Displayed in a custom WebDriverWait, making sure to ignore or catch the NoElementException

Here is an old implementation of this I've used and changed for your case, there may be a cleaner way to do it now:

new WebDriverWait(driver, TimeSpan.FromSeconds(TimeOut))
{
    Message = "Element was not displayed within timeout of " + TimeOut + " seconds"
}.Until(d => 
{
    try
    {
        return element.Displayed;
    }
    catch(NoSuchElementException)
    {
        return false;
    }
}

A quick explanation for the code above... It will try to execute element.Displayed over and over until it returns true. When the element does not exist, it will throw a NoSuchElementException which will return false so the WebDriverWait will continue to execute until both the element exists, and element.Displayed returns true, or the TimeOut is reached.

Sign up to request clarification or add additional context in comments.

Comments

0

Replace

return Wait.Until(ExpectedConditions.ElementIsVisible(element));

with the below line of code and check, as this worked for me

wait.Until(ExpectedConditions.ElementIsVisible(By.Id("ctlLogin_UserName")));

wherein "ctlLogin_UserName" is the ID of your web element.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.