Reputation: 683
I have CSS selector and was trying to apply it in selenium.
Css selector is .parentclass:not(:has(.childclass))
. I am trying to get all parent elements which do not have descendent element with class childclass
. It works perfect in jQuery. But in selenium seems it doesn't work.
So I decided to try XPath. What is the equivalent in XPath to the aforementioned CSS selector? I was able to get worked the following:
//*[contains(@class, 'parentclass')]
. But this is only first part of the condition. How can I say in XPath that I need only parents which don't contain children with CSS class childclass?
Upvotes: 3
Views: 3837
Reputation: 111601
This XPath,
//*[contains(@class, 'parentclass') and not(*[contains(@class, 'childclass')])]
will select all elements whose @class
attribute contains "parentclass"
and whose children do not have a @class
attribute containing "childclass"
.
Apply the space-padding trick to avoid false substring matches if necessary.
Update per OP comment:
The above XPath can easily be adapted to exclude those elements whose descendants, rather than children, meet the stated condition:
//*[contains(@class, 'parentclass') and not(.//*[contains(@class, 'childclass')])]
Upvotes: 4