5

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

But it didnot work

Nisha Nandagopan
  • 51
  • 1
  • 1
  • 3

5 Answers5

10

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']"
JRodDynamite
  • 12,325
  • 5
  • 43
  • 63
0

Your id is ending with _PHONE$1 and not _PHONE$. Notice that there is a 1 at the end.

(By.xpath("//input[ends-with(@id,'_PHONE$1']"));

If you still don't want to match that 1 use contains.

By.xpath("//input[contains(@id,'_PHONE$1']"));
Prateek
  • 1,145
  • 1
  • 8
  • 21
0
driver.findElement(By.xpath("//id[contains(text(),'PHONE$')]"));
Leon Barkan
  • 2,676
  • 2
  • 19
  • 43
0

You can use below XPath as well:-

//input[contains(@id,'PHONE$1')]

Hope it will help you :)

Shubham Jain
  • 16,610
  • 15
  • 78
  • 125
-1

Use contains Method

By.xpath("//input[contains(text(), '_PHONE$']");

Or use wait explicit method

Kuldeep Yadav
  • 107
  • 1
  • 14