Why does isDisplayed return true for option element with display none?

91 views
Skip to first unread message

Lee Read

unread,
Jul 13, 2022, 11:32:44 PM7/13/22
to Selenium Users
First, my apologies, I am guessing this has been answered before. I did try many searches but I failed to find an answer.

I've noticed that isDisplayed returns true for an option element that has a CSS styling of display: none;.
I'm not at all suggesting this is wrong, I just don't understand the rationale for this behavior yet and am hoping to learn.

I do see a comment in the Selenium source code on isShown that says option elements have special treatment:

* Options and Optgroup elements are treated as special cases: they are 
* considered shown iff they have a enclosing select element that is shown.

but I don't see an explanation as to why.

Selenium's selectPage.html test file has an example to illustrate:

<select id="visibility">
  <option value="regular" class="regular">Regular</option>
  <option value="disabled" disabled="disabled" class="disabled">Disabled</option>
  <option value="hidden" style="visibility: hidden;" class="hidden">Hidden</option>
  <option value="invisible" style="display: none;" class="invisible">Invisible</option> 
</select>

If hit the above with the following Java code (using Selenium 4.3.0, chromedriver 103.0.5060.53 and Chrome 103.0.5060.114 on macOS 10.15.7 with OpenJDK Temurin-11.0.15+10):

package com.dlread.seleniumplay;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class Play {

  public static void main (String args[]) {
    WebDriver driver = new ChromeDriver();
    driver.get("file:///Users/lee/proj/oss/SeleniumHQ/selenium/common/src/web/selectPage.html");
    System.out.println ("regular displayed: " + driver.findElement(By.cssSelector("[class='regular']")).isDisplayed());
    System.out.println ("hidden displayed: " + driver.findElement(By.cssSelector("[class='hidden']")).isDisplayed());
    System.out.println ("invisible displayed: " + driver.findElement(By.cssSelector("[class='invisible']")).isDisplayed());
    driver.close();
  }
}


I see the following output:

regular displayed: true
hidden displayed: true
invisible displayed: true

Here's what I see if I manually open selectPage.html in Chrome:
selenium-selectpage-example.png
The Regular and the Hidden items are shown, so a return of true from isDisplayed makes sense to me.
I was (probably naively) expecting isDiplayed to return false for the Invisible item.

I'm sure this is probably Selenium 101, but would somebody be so kind to educate me here?

Lee Read

unread,
Jul 15, 2022, 4:08:07 PM7/15/22
to Selenium Users
Forgot to mention: If there is any interest, I'd be happy to submit a PR with updates to Selenium docs in this area,  once I understand the rationale for the current behavior.

Lee Read

unread,
Jul 19, 2022, 3:36:34 PM7/19/22
to Selenium Users
I'm digging a bit into the commit history.
If I understand correctly (which I might not) this behavior was introduced way back in August of 2010.

The commit message is brief:
SimonStewart: Updating the atoms.
   
r9490

I am guessing r9490 is likely an issue reference. A little more digging tells me Selenium used to be hosted on (the now retired) Google Code, and that its issues are archived here.
Alas, I don't see an issue that maps to 9490 in the archive (it could be that I don't understand the mapping).
Reply all
Reply to author
Forward
0 new messages