0

I'm trying to configure a regex to find all elements with such attributes. The HTML tag is something like:

<img class="my-img-12">

where the latest 2 numbers are always different. I tried these ways but no one seems to work:

    List<WebElement> allElement = driver.findElements(By.cssSelector("img[class^='my-img-\\d{1,}']"));

or

    List<WebElement> allElement = driver.findElements(By.xpath(".//img[starts-with(@class, 'my-img-') and  'my-img-' = translate(@class, '0123456789', '') and string-length(@class) > 1])"));

the latest option was suggested here: Stack question

if you're thinking about this:

    List<WebElement> allElement = driver.findElements(By.cssSelector("img[class^='my-img-']"));

It's not gonna works because there could be some attributes like this:

<img class="my-img-wordsOccurrences">
Virgula
  • 318
  • 4
  • 12

1 Answers1

0

You can try using XPath contains:

List<WebElement> allElement = driver.FindElements(By.Xpath(".//<define root element here>[contains(@class, 'my-img-')]")
Sparkle
  • 681
  • 5
  • 10