admin管理员组

文章数量:1245841

<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.

<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.

Share Improve this question edited Feb 17 at 7:11 pebble unit asked Feb 17 at 7:02 pebble unitpebble unit 1,2144 gold badges10 silver badges27 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

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.

本文标签: