Xpath: select div that contains class AND whose specific child element contains text

后端 未结 4 1830
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-13 04:02

With the help of this SO question I have an almost working xpath:

//div[contains(@class, \'measure-tab\') and contains(., \'someText\')]

Ho

4条回答
  •  清歌不尽
    2020-12-13 04:23

    To find a div of a certain class that contains a span at any depth containing certain text, try:

    //div[contains(@class, 'measure-tab') and contains(.//span, 'someText')]
    

    That said, this solution looks extremely fragile. If the table happens to contain a span with the text you're looking for, the div containing the table will be matched, too. I'd suggest to find a more robust way of filtering the elements. For example by using IDs or top-level document structure.

提交回复
热议问题