10

In order to check if an Element is exists and clickble i'm trying to write a boolean method which will wait for the element to be enabled and displyed using C# selenium's webDriverWait as follow:

webDriverWait wait = new webDriverWait(driver, timeSpan.fromSeconds(60));

Wait.untill( d => webElement.enabled() && webElement.displayed());

In case the above conditions do not happen, I want the method to return 'false'. The problem is that I get exceptions thrown. How can I ignore exceptions such as noSuchElementException and timeOutException in case they are thrown? I have tried to use try catch block but it didn't help and exceptions were thrown.

6
  • Can you provide some more specific information as: 1. You want the element to be visible or clickable? 2. Some how have you measured what's the minimum & maximum time the element requires to be visible/clickable? Commented Mar 29, 2017 at 21:00
  • Hi, (1)I want the element to be visble and clickble. (2) I want to wait untill 60 seconds. Commented Mar 29, 2017 at 21:22
  • I don't know the the exact min and max time since I would like to implement the above code not for specific element. I need to write a method which I can use not for a specific element. Commented Mar 29, 2017 at 21:28
  • can you update exactly what type of exception is it throwing? Commented Mar 29, 2017 at 21:30
  • OpenQA.Selenium.NoSuchElementException and 'OpenQA.Selenium.WebDriverTimeoutException' occurred in WebDriver.Support.dll Commented Mar 29, 2017 at 21:31

2 Answers 2

8

WebDriverWait implements DefaultWait class that contains public void IgnoreExceptionTypes(params Type[] exceptionTypes) method.

You can use this method for defining all the exception types you want to ignore while waiting for element to get enabled before clicking.

For example :

WebDriverWait wdw = new WebDriverWait(driver, TimeSpan.FromSeconds(120));
wdw.IgnoreExceptionTypes(typeof(NoSuchElementException), typeof(ElementNotVisibleException));

In the preceding code wait will ignore NoSuchElementException and ElementNotVisibleException exceptions

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

2 Comments

Nope. I'm using this exact code, but it still throws an exception of "No such element, unable to locate element... [etc]..." Grrrr.....
Same problem here hence why I am on this thread. I can clearly see the wait instance using the IgnoreExceptionTypes statement while debugging but it doesn't actually ignore them.
2

If you wait for the element to be clickable, it will also be displayed and enabled. You can simply do

public bool IsElementClickable(By locator, int timeOut)
{
    try
    {
        new WebDriverWait(Driver, TimeSpan.FromSeconds(timeOut)).Until(ExpectedConditions.ElementToBeClickable(locator));

        return true;
    }
    catch (WebDriverTimeoutException)
    {
        return false;
    }
}

and it will wait for 60s and click the element once it is found. It still may throw an exception if the element is not found, doesn't become clickable, etc. after the timeout expires.

EDIT: Wrapped this up in a function based on OPs comment.

14 Comments

Hi, wait.Until(ExpectedConditions.ElementToBeClickable() method exists in Java, not on c#.
Not true. I have VS up and typed and formatted that code and it works just fine not to mention I'm using it in the project that I'm working on right now.
Thanks, I'll try. What about the exceptions? Is there any way to ignore them?
You can just pass in the IWebElement instead. You should read the docs so you can see how to do this stuff yourself. It's pretty straightforward. seleniumhq.github.io/selenium/docs/api/dotnet/html/…
If you found this (or any) answer helpful, please upvote it. If this answered your question, please mark it as the accepted answer. Thanks!
|

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.