3

How can I find out if the element display is none or not?

Currently, I am using the IWebElement GetAttribute("Style") method to get the style which returns everything in the style property. I can then parse through the string and find display:none.

I am just wondering if there is an easier and cleaner way?

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
john doe
  • 9,220
  • 23
  • 91
  • 167

1 Answers1

3

You can also execute javascript and get the .style.display value of an element:

IJavaScriptExecutor js = driver as IJavaScriptExecutor;
string display = (string)js.ExecuteScript("return arguments[0].style.display;", element);

If you just want to test an element's visibility - then don't reinvent the wheel and let the webdriver handle it. There is Displayed property:

element.Displayed

FYI, behind Displayed webdriver has a quite complicated logic built-in, see:

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • Thanks! But when you say visibility don't you mean if the element is present in the DOM or not. I am usually checking for display:none where the element is always in the DOM but with no display. – john doe May 13 '15 at 14:01
  • @johndoe when I speak about visibility, I refer to what is `perceptually visible to the human eye`. To check if element is present, one would usually check if selenium can find an element (no exception is thrown), or use `FindElements` and check the size of the result to be more than 0. (example: http://stackoverflow.com/questions/7991522/selenium-webdriver-test-if-element-is-present) – alecxe May 13 '15 at 14:07
  • 1
    @johndoe good, see if the answer deserves to be accepted. Thanks. – alecxe May 13 '15 at 14:24