0

On my browser I have elements that share same classes with each other like

<span class="a8Pemb OFFNJ Jz5Gae">...</span>

and

<span class="a8Pemb OFFNJ">...</span>

However when I used page.getByXPath("//span[@class='a8Pemb OFFNJ']"); it only returns me the second elements with exact same class names. I want to get all of them IN ORDER, which means I don't want to call a separate query for the first elements. How can I do this?

1 Answers1

0

Note that the XPath expression //span[@class='a8Pemb OFFNJ'] is only going to return span elements whose class attribute is literally exactly equal to the string 'a8Pemb OFFNJ', and the string value 'a8Pemb OFFNJ Jz5Gae' is not equal.

I believe you are limited to XPath version 1 in HTMLUnit, so you're not easily able to tokenize the class attributes into a list of separate tokens that you can compare. You could generate a more complex XPath expression that uses the XPath contains() function to check that the class attribute contains each of the classes you want, but probably it's easier and more robust to retrieve the span elements by CSS selector instead, using the querySelectorAll method and a CSS selector like span.a8Pemb.OFFNJ

Conal Tuohy
  • 2,561
  • 1
  • 8
  • 15
  • The expression `//span[contains(@class, 'a8Pemb OFFNJ')]` would work for the specific example you gave, but it would also pick up `` and it would miss ``. That's why it's more robust to use CSS which understands that `class` attributes contain a set of independent tokens, not just a string. – Conal Tuohy Jul 15 '22 at 23:53