javaseleniumxpathxpath-2.0xpath-1.0

Xpath ends-with expression does not work while starts-with does


I'm looking for some hint how to use xpath "ends-with" expression. I've got such element to find:

<div class="osaiowrhg29875n345" id="react-select-4-listbox">

Previous selector I used was:

//div[starts-with(@id,'react-select-4')]

and its working properly but there are more of such elements and the number is changing so I wanted to catch only the ending of this ID using

//div[ends-with(@id,'listbox')]

Why its not working for me?


Solution

  • The issue is: starts-with is available in XPath 1.0 while ends-with was only introduced by XPath 2.0 .
    Selenium supports XPath 1.0 only.
    That's why ends-with will not work with Selenium.
    But you still can use contains with Selenium.
    So //div[ends-with(@id,'listbox')] can be changed by

    "//div[contains(@id,'listbox')]"