Exclude non visible elements with XPath when display: none; is set in CSS class?

Viewed 224

I need to exclude elements from XPath where display: none; is set in the CSS class. The solution suggested here will not work when the given element has a CSS class in which the display: none; is set as pointed out in this comment.

In my specific example, the element has a class named error-result with CSS set to:

.error-result {
    display: none;
}

Here's the XPath I tried:

.//*[contains(text(),'XXXXXX')][not(ancestor::div[contains(@style,'display: none')])][not(descendant::div[contains(@style,'display: none')])]

The problem is, Selenium still identifies the undesired element since display: none; is set only in CSS class.

How can I exclude such elements?

I'm using Selenium WebDriver with Java.

Thanks

1 Answers

Xpath executed against the page source scope. It will work only if you able to find the display property in style attribute for the element in page source text (the result value of driver.getPageSource())

Unfortunately, in many cases css styles not reflected by the page source, so you should execute additional driver commands like element.getCssValue('displayed'). (But for check visibility I suggest to use isDisplayed()).

It means, you should iterate by the elements list and get sublist by some condition, which increases driver requests amount, but it's the only way to solve this.

Related