Skip to content

Integrating regex in selenium xpath?

enter image description hereHello I just start using selenium with java framework :

I have this case : I need to count all the check boxes in my view which contains multiple pages (1,2,3) and in each web page i have a bunch of check boxes with the same xpath with different number see the exemple below:

xpath fir the second checkbox in page 1 : "//*[@id='mat-checkbox-2']/label/div" xpath fir the second checkbox in page 40 : //*[@id="mat-checkbox-57"]/label/div"

driver.findElements(By.xpath("//*[@id='mat-checkbox-" + regex +"']/label/div")).size()

How can I count all the present checkboxes in my current view without refering to the order number ?

Answer

You can use partial id in the xpath with contains or starts-with

"//*[starts-with(@id,'mat-checkbox')]/label/div"