【问题标题】:XPath - not able to find an element with children textXPath - 无法找到带有子文本的元素
【发布时间】:2016-09-23 05:09:57
【问题描述】:
<a>
    This is <var>Me</var> and That is <var> You</var>
</a>

我可以通过以下代码找到包含“This is”的元素“a”:

//a[contains(text(),'This is')]

但我找不到包含“This is Me and That is You”的元素“a”。

//a[contains(text(),'This is Me and That is You')]

有没有办法找到带有子文本的元素?

【问题讨论】:

    标签: selenium xpath selenium-webdriver xpath-2.0


    【解决方案1】:

    我不确定这是否是你需要的,但你可以使用 string() 来获得所需的结果,

    //a[string()='This is Me and That is You']
    

    但需要注意的是,您需要准确了解正在使用的字符串。

    查看工作示例here

    【讨论】:

      【解决方案2】:

      这也可以使用xpathnormalize-space() 函数找到,该函数从字符串中去除前导和尾随空白,用单个空格替换空白字符序列,并返回结果字符串,如下所示:-

      //a[normalize-space()='This is Me and That is You']
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-06-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-09-03
        相关资源
        最近更新 更多