Ignoring exceptions when using c# selenium webdriverWait wait.untill() function

前端 未结 2 834
一向
一向 2021-01-02 08:41

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

相关标签:
2条回答
  • 2021-01-02 09:17

    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.

    0 讨论(0)
  • 2021-01-02 09:38

    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

    0 讨论(0)
提交回复
热议问题