Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to locate dynamic element using xpath(ends-with function not working)

Tags:

selenium

xpath

I have used ends-with function as (By.xpath("//input[ends-with(@id,'_PHONE$']"));

But it didnot work

like image 247
Nisha Nandagopan Avatar asked Mar 17 '16 06:03

Nisha Nandagopan


1 Answers

The ends-with function is part of XPath 2.0 but browsers generally only support 1.0.

So, if you strictly want to fetch all elements that ends with a specific pattern, you can either fetch all elements that contain that pattern (using the contains() function) and then strictly check the suffix of the id (fetch the attribute value by .getAttribute("id")) using Java's .endsWith() method.

Or

You can use string-length function, substring function and equals to get this XPath:

"//input[substring(@id, string-length(@id) - string-length('_PHONE$1') +1) = '_PHONE$1']"
like image 172
JRodDynamite Avatar answered Oct 18 '22 22:10

JRodDynamite