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.
<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:
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?