javaselenium-webdriverxpath

How can I check against attribute (not attribute value) starting with prefix and ends with suffix


<tr _ngcontent-hse-63="">
    <td _ngcontent-hse-63="">
        <span _ngcontent-hse-63="">1</span>
    </td>
    <td _ngcontent-hse-63="">
        <span _ngcontent-hse-63="" class="noBr">CKH HOLDINGS</span>
    </td>
</tr>

I have the above and I am looking to extract the td elements in Selenium. However, it seems the three alphabets between ngcontent and the number is randomized. I have tried the following based on similar questions on SO ref:

By.xpath("//td[starts-with(@_ngcontent, '_ngcontent-') and ends-with(@_ngcontent, '-63')]");

though I'm not too sure how to continue since the @_ngcontent wouldnt even match any attributes, and I think the starts-with() and ends-with() would be matching against the attribute value instead of the attribute itself.

Is there anyway to resolve this? Just getting by the td element is not possible because there are a lot of other elements on the page.


Solution

  • Browsers only support XPath 1.0, which doesn't support ends-with(). You can't use it in selenium. So you have to use contains() instead.

    What you are trying to do can be achieved by the following XPath.

    //@*[starts-with(name(),'_ngcontent-') and contains(name(),'-63')]/parent::td
    

    It selects all the td elements that has attribute that start with _ngcontent- and contains -63.