2

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

Bradford Griggs
  • 439
  • 2
  • 15
  • This sounds like an [X-Y problem](http://xyproblem.info/). Instead of asking for help with your solution to the problem, edit your question and ask about the actual problem. What are you trying to do? – undetected Selenium Dec 12 '21 at 06:49
  • @DebanjanB I need to identify a certain element but only if `display: none` is not set in the CSS class. – Bradford Griggs Dec 12 '21 at 06:50
  • 1
    @BradfordGriggs I'd advice you accepting and upvoting given you answers. This may increase your chances to get an answers.. – Prophet Dec 12 '21 at 07:39
  • @Prophet I have upvoted almost every answer given to my question (at least 1 upvote is mine for almost every answer). I have also accepted everyone except for one. One answer did not actually answer the question but was informative so I up voted but I didn't accept as answer. – Bradford Griggs Dec 12 '21 at 16:44
  • You did that not for me, my friend. i really don't care. It may help you. – Prophet Dec 12 '21 at 16:50
  • @Prophet Can you kindly share which answer you replied to that I did not accept? I apologize if I missed it. The only answer I did not accept was this one: https://stackoverflow.com/questions/70100400/remove-bot-identyfing-features-from-firefox-webdriver-source-code It was highly informative (so I up-voted it) but it addressed .net, not Java version as the OP stated. – Bradford Griggs Dec 12 '21 at 16:57
  • @Prophet If you are looking at my profile you will see several questions with unaccepted answers - that's because there were only comments to those questions, no actual answer - please take a look at the actual question page and you will see – Bradford Griggs Dec 12 '21 at 17:07

1 Answers1

2

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.

Max Daroshchanka
  • 2,698
  • 2
  • 10
  • 14