0

I have a set of classes underneath the of the HTML document which all share the same set of class names. These are all date pickers but the one that is currently on screen lacks a class name that the others have which is how I need to select it.

How do I write my CSS Selector so that it picks the that has two class names but not the third?

I.E.:

<body>
    <div class="class-one class-two class-three"></div>
    <div class="class-one class-two class-three"></div>
    <div class="class-one class-two class-three"></div>
    <div class="class-one class-two class-three"></div>
    <div class="class-one class-two"></div>
</body>

I want a selector that picks the last div here. So I want it to say that it has "class-one" and "class-two" class names but does NOT have "class-three"?

2 Answers 2

2

Also will be worked the following CSS Selector:

div[class='class-one class-two']:not(.class-three)

PS: To disregard the spaces and the order of the classes use the following CSS Selector: div.class-one.class-two:not(.class-three).

Sign up to request clarification or add additional context in comments.

2 Comments

@Ratmir Asanov, :not(.class-three) is useless since you are selecting the exact class. So either div[class='class-one class-two'] for an exact match on the class or div.class-one.class-two:not(.class-three) to disregard the spaces and the order of the classes.
@FlorentB., I agree with you! I have updated my answer with remarks. Thank you!
2

Instead of selector

div.class-one.class-two

which will fetch all div nodes, you can use

div[class="class-one class-two"]

which will return you the last node only

There is also :not() syntax:

div:not(.class-three)

but I'm not sure whether it's applicable for Selenium

1 Comment

Thanks! I was trying to get the 'not' working but wasn't getting the syntax the right way round, this is great!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.