11

Is there a way in the latest version of Selenium DotNet Webdriver (2.22.0) to check to see if an element is visible before clicking/interacting with it?

The only way I've found is to try to handle the ElementNotVisible exception that occurs when you try to send keys, or click on it. Unfortunately this only occurs after an attempt to interact with the element has been made. I'm using a recursive function to find elements with a certain value, and some of these elements are only visible in certain scenarios (but their html is still there no matter what, so they can be found).

It's my understanding that the RenderedWebElement class is deprecated as well other variants. So no casting to that.

Thanks.

user1442482
  • 113
  • 1
  • 1
  • 4
  • There should be a WebElement.isDisplayed() to check whether element is visible – A.J Jun 07 '12 at 15:20
  • 1
    Unfortunately catching exception is way to go. Displayed and Enabled do not always render actionable element, which means that item can be both displayed and enabled but still not clickable for example. This happens when element is out of the view (in some browsers), and what you need typically to do is to scroll to the element so it becomes visible. – ljgww Aug 18 '17 at 05:51

4 Answers4

30

For Java there is isDisplayed() on the RemoteWebElement - as well is isEnabled()

In C#, there is a Displayed & Enabled property.

Both must be true for an element to be on the page and visible to a user.

In the case of "html is still there no matter what, so they can be found", simply check BOTH isDisplayed (Java) / Displayed (C#) AND isEnabled (Java) / Enabled (C#).

Example, in C#:

public void Test()
{
    IWebDriver driver = new FirefoxDriver();
    IWebElement element = null;
    if (TryFindElement(By.CssSelector("div.logintextbox"), out element)
    {
        bool visible = IsElementVisible(element);
        if  (visible)
        {
            // do something
        }
    }
}

public bool TryFindElement(By by, out IWebElement element)
{
    try
    {
        element = driver.FindElement(by);
    }
    catch (NoSuchElementException ex)
    {
        return false;
    }
    return true;
}

public bool IsElementVisible(IWebElement element)
{
    return element.Displayed && element.Enabled;
}
BenWurth
  • 790
  • 1
  • 7
  • 23
Arran
  • 24,648
  • 6
  • 68
  • 78
  • 4
    I must disagree, because `element.Enabled` will only return **false** for explicitly disabled input elements, so the visibility can be simply determined by `element.Displayed` property. – Gucu112 Mar 21 '19 at 12:42
0

It seems the current answer to this question is outdated: With WebDriver 3.13 both the Displayed and Enabled properties will return true as long as the element exists on the page, even if it is outside of the viewport. The following C# code works for WebDriver 3.13 (from this StackOverflow answer):

{
    return (bool)((IJavaScriptExecutor)Driver).ExecuteScript(@"
        var element = arguments[0];
        var boundingBox = element.getBoundingClientRect();
        var cx = boundingBox.left + boundingBox.width/2, cy = boundingBox.top + boundingBox.height/2;
        return !!document.elementFromPoint(cx, cy);
        ", element);
}
bgh
  • 1,986
  • 1
  • 27
  • 36
0

There is a simple way to do that, follow below:

public bool ElementDisplayed(By locator)
{
     new WebDriverWait(driver, TimeSpan.FromSeconds(timeOut)).Until(condition: ExpectedConditions.PresenceOfAllElementsLocatedBy(locator));
     return driver.FindElement(locator).Displayed ;
}
0

You can use the following:

WebDriver web = new FirefoxDriver(;
String visibility = web.findElement(By.xpath("//your xpath")).getCssValue("display");
Altaf Patel
  • 1,351
  • 1
  • 17
  • 28