selenium: [🐛 Bug]: Opacity : 0 is not considered as "Displayed"

What happened?

In our React powered Web App, one of the checkboxes is getting isDisplayed() as false although everything is fine.

  1. The element is available in DOM.
  2. isEnabled() is true
  3. element.click() is working
  4. element.getSize() is having non-zero width and height.

When I checked, found that Selenium is considering opacity : 0 as invisible and hence making isDisplayed as false. This is happening in antd react library which is a common library among React Web Apps and this issue seems to be happening in all these areas.

Proposal is, we shall remove this check for opacity.

Additional Note: https://playwright.dev/docs/actionability#visible

In Playwright also, opacity 0 is not considered as invisible.

How can we reproduce the issue?

//As you could see here, only isDisplayed() is false, everything else is fine.

public class RadioTest {
    public static void main(String[] args) throws InterruptedException {
        WebDriver driver = new ChromeDriver();
        driver.get("https://ant.design/components/radio");
        WebElement radioButton = driver.findElement(By.xpath("(//input[@class='ant-radio-input'])[1]"));
        System.out.println("Displayed : " + radioButton.isDisplayed());
        System.out.println("Enabled : " + radioButton.isEnabled());
        System.out.println("Size : " + radioButton.getSize());
        Rectangle rect = radioButton.getRect();
        System.out.println("Rect : " + Arrays
                .stream(rect.getClass().getFields())
                .collect(Collectors
                        .toMap(
                                Field::getName,
                                field -> getValueFromField(field, rect)
                        )
                )
        );
        Thread.sleep(5000);
        driver.quit();
    }

    public static Object getValueFromField(Field field, Object rect) {
        try {
            return field.get(rect);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }
}

Relevant log output

NA

Operating System

Mac

Selenium version

4.20.0

What are the browser(s) and version(s) where you see this issue?

Chrome

What are the browser driver(s) and version(s) where you see this issue?

NA

Are you using Selenium Grid?

No

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 18 (11 by maintainers)

Most upvoted comments

I should point out that it’s entirely possible to use the Actions APIs to allow you to click as well, and this doesn’t do the same kind of checks.

We used to talk about APIs in Selenium being one of two kinds: “do what I mean” and “do what I say”. WebElement.click is definitely a “do what I mean” API – it has a number of checks it makes to ensure that clicking is a meaningful thing to do. OTOH, Actions.click is a “do what I say” API – it has far fewer checks, and just does what you tell it to.

@AutomatedTester is right that the behaviour we have in WebElement.click is designed to handle the common scenarios, and click-jacking was very definitely raised as a concern (and may have been what lead us to add the opacity check in the first place, IIRC). If you really, truly just want to click on “the space of the screen where the element is rendered (even if we calculate the element isn’t displayed)”, that Actions API is always available, and in this case is pretty easy to use.