0

If I have this DOM:

<div class="A others classes">

    other elements

    <div class="AB other classes"></div>

    other elements

</div>

I want to select the div with class A.

I have tried with //div[contains(@class, 'A')], but also select the div with class AB.

How I can select just div with class A? I can't use other classes in xpath, because are dynamic added to element.

KunLun
  • 3,109
  • 3
  • 18
  • 65
  • 1
    Another helpful duplicate: https://stackoverflow.com/questions/1390568/how-can-i-match-on-an-attribute-that-contains-a-certain-string – Daniel Haley Jun 03 '20 at 15:51
  • 1
    @DanielHaley: Thanks, added ahead of the original duplicate as it's better (albeit with a bit harder-to-find title). – kjhughes Jun 03 '20 at 16:33

1 Answers1

1

Two possibilities are

  1. Match with the space following A:

    //div[contains(@class, 'A ')]
    
  2. Match A and exclude AB explicitly:

    //div[contains(@class, 'A') and not(contains(@class, 'AB'))]
    

Maybe this fits your needs.

zx485
  • 28,498
  • 28
  • 50
  • 59
  • 1
    There's an idiom to make such a differentiation more robustly. See duplicate link. (Your solution would select `div` elements with `class="xxxA "`, for example -- probably not desired.) – kjhughes Jun 03 '20 at 15:28