I have bunch of elements with class X.

some elements have class Y also (ie both X and Y)

how do I select set of elements only have X with partial class name?

below is my example HTML

<td class="dxeCalendarDay_Office2010Blue dxeCalendarOtherMonth_Office2010Blue" savedcursor="[object Object]" style="cursor: pointer;">29</td>

<td class="dxeCalendarDay_Office2010Blue" savedcursor="[object Object]" style="cursor: pointer;">4</td>

I could select all of them using


The class prefix "_Office2010Blue" will change at run time

But I need to exclude elements which having "dxeCalendarOtherMonth..."

probably something like.....


what is the correct syntax?

  • Also ..."_Office2010Blue" is subjects to change.
    – 1234
    Commented Jul 23, 2018 at 4:56
  • Do you have class Z in picture which you don't want to exclude? Commented Jul 23, 2018 at 9:10
  • with help of @Alexey R answer came up with (By.cssSelector("[class^='dxeCalendarDay']:not([class*='dxeCalendarOtherMonth'])")
    – 1234
    Commented Jul 23, 2018 at 23:33

3 Answers 3


Assume you want to take all the elements having class X but having no class Y. Then your code will look like:


For example for the below html:

  <div class="x y">xy</div>
  <div class="x y z">xyz</div>
  <div class="x z">xz</div>
  <div class="x">x</div>

The mentioned selector will select xz and x.


Why not just select:


This will simply exclude the "Y" class objects without any NOT operators assuming there are NO "Z" class objects in the picture.

  • yeah, simplicity is the key
    – Yu Zhang
    Commented Jul 23, 2018 at 9:44
  • 2
    Matching the class attribute is less robust than matching the class itself. [class="X"] would not match the perfectly valid attributes class="X " or class="X Z", therefore it’s preferrable to use the built-in class matching: .X (combined with :not(.Y) in this case).
    – Martin
    Commented Jul 23, 2018 at 11:44
  • Your solution is simple and elegant...but doesn't work with [class ^=X] which would not exclude "Y"
    – 1234
    Commented Jul 23, 2018 at 23:19

The simplest solution is perhaps selecting class X straight away:

  • By.cssSelector("[class='dxeCalendarDay_Office2010Blue dxeCalendarOtherMonth_Office2010Blue']");

Alternatively, you can go with select a class that ends with syntax:

  • By.cssSelector("[class$='dxeCalendarOtherMonth_Office2010Blue']");
  • may be I'm not clear....so changed wordings. I need to exclude dxeCalendarOtherMonth
    – 1234
    Commented Jul 23, 2018 at 4:50

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.