Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Webdriver How to wait until the element is clickable in webdriver C#

There is a block Ui which covers all the elements for a few seconds after the Element have been generated in the browser because of this i facing a problem ,Since element has come into existence the web-driver try to click the element but the click is received by Block UI . I have tried to use the wait Until but i did not help ,Since i can find isClickAble in C# webdriver

   var example = _wait.Until<IWebElement>((d) => d.FindElement(By.XPath("Example")));
   var example2 = _wait.Until<IWebElement>(ExpectedConditions.ElementIsVisible(By.XPath("Example")));
example.click();
example2.click();

Is there C# equivalent for isClickAble ,Thanks in advance

like image 612
Anand S Avatar asked Apr 17 '13 09:04

Anand S


People also ask

How do you make Selenium wait for an element to appear?

The solution is, of course, to instruct Selenium to wait until the desired element is visible before trying to interact with it. var wait = new WebDriverWait(driver, TimeSpan.

What would you use if you are waiting for an element to be clickable in Selenium?

We can check if the element is clickable or not in Selenium webdriver using synchronization. In synchronization, there is an explicit wait where the driver waits till an expected condition for an element is met. To verify, if the element can be clicked, we shall use the elementToBeClickable condition.

What is Webdriver wait?

Selenium WebDriverWait is one of the Explicit waits. Explicit waits are confined to a particular web element. Explicit Wait is code you define to wait for a certain condition to occur before proceeding further in the code. Explicit wait is of two types: WebDriverWait.


2 Answers

Well taking a look into the Java source, tells me it is basically doing two things to determine if it's 'clickable':

https://code.google.com/p/selenium/source/browse/java/client/src/org/openqa/selenium/support/ui/ExpectedConditions.java

Firstly, it'll check if it's 'visible' by using the standard ExpectedConditions.visibilityOfElementLocated, it'll then simply check if the element.isEnabled() is true or not.

This can be condensed slightly, this basically means (simplified, in C#):

  1. Wait until the element is returned from the DOM
  2. Wait until the element's .Displayed property is true (which is essentially what visibilityOfElementLocated is checking for).
  3. Wait until the element's .Enabled property is true (which is essentially what the elementToBeClickable is checking for).

I would implement this like so (adding onto the current set of ExpectedConditions, but there are multiple ways of doing it:

/// <summary>
/// An expectation for checking whether an element is visible.
/// </summary>
/// <param name="locator">The locator used to find the element.</param>
/// <returns>The <see cref="IWebElement"/> once it is located, visible and clickable.</returns>
public static Func<IWebDriver, IWebElement> ElementIsClickable(By locator)
{
    return driver =>
    {
        var element = driver.FindElement(locator);
        return (element != null && element.Displayed && element.Enabled) ? element : null;
    };
}

Usable in something like:

var wait = new WebDriverWait(driver, TimeSpan.FromMinutes(1));
var clickableElement = wait.Until(ExpectedConditions.ElementIsClickable(By.Id("id")));

However, you might have a different idea of what clickable might mean, in which case, this solution may not work - but it is a direct translation of what the Java code is doing.

like image 171
Arran Avatar answered Oct 16 '22 22:10

Arran


Here's the code I use to check if it's clickable, else go to another URL.

if (logOutLink.Exists() && ExpectedConditions.ElementToBeClickable(logOutLink).Equals(true))
            {
                logOutLink.Click();
            }
            else
            {
                Browser.Goto("/");
            }
like image 42
Phil Hudson Avatar answered Oct 16 '22 20:10

Phil Hudson