0

I have written a method IsElementPresent which returns true/false whether element is being displayed or not.

Here is my method

    public static bool IsElementPresent(this IWebElement element)
    {
        try
        {
            return element.Displayed;                                  
        }
        catch (Exception)
        {
            return false;
        }

    }

Now sometimes when it should return false, element.Displayed is waiting approx 20 seconds (found thru debugging) before catching Exception and returning false. If it finds element, it is working fine.

I also changed code to :

public static bool IsElementPresent(this IWebElement element)
{          
    try
    {
        WebDriverWait wait = new WebDriverWait(DriverContext.Driver, TimeSpan.FromSeconds(0.1));
        wait.Until(ExpectedConditions.ElementToBeClickable(element));                
        return true;
    }
    catch (Exception)
    {
        return false;
    }
}

Now same wait is happening in Wait.Until line. Code is working fine but just unwanted delays when it does not find element. Does it matter how element is found. This particular delay is happening when element is found by class. Most of other elements are found using xpath, css or id. Let me know if I missed any info. Using VS community 15.5.6

Fletcher
  • 422
  • 1
  • 6
  • 21
Rutwik
  • 1
  • 1
  • What is your exact question? Can you sum up the _Manual Steps_ which you are trying to _Automate_ ? – undetected Selenium Feb 23 '18 at 21:07
  • Would you be able to post just a bit of the HTML around this element? – Rescis Feb 23 '18 at 21:54
  • I am trying to automate an application. This problem is happening on one of our forms. On this page, I am performing some action by clicking on a button. If this action is not allowed, web app will throw some error on the page with some text. Now I have written a method (This method calls the above method) which will check whether that error text is present (a check to proceed only if there is no error). Now at this point when it reaches element.Displayed it is waiting too before throwing exception. Hope I answered all the questions. – Rutwik Feb 23 '18 at 21:59
  • Here is the HTML element. The following sheets have problems,
    "District"
    Please correct the errors in the excel File.
    – Rutwik Feb 23 '18 at 22:02

1 Answers1

0

As per the API Docs IWebElement.Displayed property gets a value indicating whether or not this element is displayed . There is no wait involved in it. So if any Exception is raised its instant.

But when you induce Wait.Until along with ExpectedConditions Class which supplies a set of common conditions that can be waited for using WebDriverWait Class the WebDriver instance waits as per the ExpectedConditions clause, which is ExpectedConditions.ElementToBeClickable Method (By) in your case.

ExpectedConditions.ElementToBeClickable Method (By) is defined as an expectation for checking an element is visible and enabled such that you can click it.

  • The syntax is :

    public static Func<IWebDriver, IWebElement> ElementToBeClickable(
        By locator
    )
    
  • Parameters :

    locator
    Type: OpenQA.Selenium.By
    The locator used to find the element.
    
  • Return Value :

    Type: Func<IWebDriver, IWebElement>
    The IWebElement once it is located and clickable (visible and enabled).
    

So functionally there is no delays involved while working with WebDriverWait and ExpectedConditions.

Finally, as you mentioned this particular delay is happening when element is found by class. Most of other elements are found using xpath, css or id , this is a fact related to Locator Strategy which you have used while selecting the locator from the list below :

  • css selector
  • link text
  • partial link text
  • tag name
  • xpath

There have been quite some experiments and benchmarking about the performance aspects of the locators. You can find some discussions here :

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352