Jeff
Jeff

Reputation: 8431

Finding all the elements by partial css class with Selenium in Java

I have seen this question, but it is not exactly what i need.

I am trying to automate 2048 using Selenium. As you start to play, the class of elements changes. The Tiles which has the value have 3, or 4 different classes with below patterns:

tile tile-1024 tile-position-1-2 
tile tile-2 tile-position-4-1 tile-new
tile tile-8 tile-position-1-4 tile-merged

I am wondering how can i find all the elements which has this pattern in their class:

tile tile-[TEIL SCORE] tile-position-[Any Number]-[Any Number]

Here is my my HTML. Sorry for image, the chrome console does not allow me to copy it without expanding it:

enter image description here

Upvotes: 2

Views: 731

Answers (2)

Kushal Bhalaik
Kushal Bhalaik

Reputation: 3384

Adding regex is not possible in CSS. ONLY Available options are *,$,^. But you can search based on following, which is as per your criteria, but probably it will match almost all tiles:

div[class^='tile-'][class^='tile-position]

Instead you can be more specific by providing number and row of tile which you are specifically looking for, for ex tile having number 4 in row 2:

div[class^='tile-4'][class^='tile-position-2]

Upvotes: 1

undetected Selenium
undetected Selenium

Reputation: 193308

To find all the elements as per the pattern you can use the following cssSelector:

.tile[class^='tile-'][class^='tile-position-']

Upvotes: 1

Related Questions