代码之家  ›  专栏  ›  技术社区  ›  pebble unit

如何检查以前缀开头、以后缀结尾的属性(不是属性值)

  •  0
  • pebble unit  · 技术社区  · 3 月前
    <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>
    

    我有上述内容,我想提取硒中的td元素。然而,ngcontent和数字之间的三个字母似乎是随机化的。我根据SO上的类似问题尝试了以下方法 ref :

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

    尽管我不太确定如何继续,因为@_ngcontent甚至不匹配任何属性,而且我认为starts-with()和ends-with(。

    有办法解决这个问题吗?仅仅通过td元素是不可能的,因为页面上还有很多其他元素。

    1 回复  |  直到 3 月前
        1
  •  2
  •   S A    3 月前

    浏览器只支持XPath 1.0,而XPath 1.0不支持 ends-with() . 你不能在硒中使用它。所以你必须使用 contains() 相反。

    您试图做的事情可以通过以下XPath来实现。

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

    它选择所有具有以开头的属性的td元素 _ngcontent- 并包含 -63 .