【问题标题】:Select element with a changing Id string using XPath使用 XPath 选择具有不断变化的 Id 字符串的元素
【发布时间】:2011-12-19 03:44:12
【问题描述】:

我有一个文本区域控件,其 Id 如下所示:

<textarea id="NewTextArea~~51887~~1" rows="2"/>

而之前工作的xpath一直是

//textarea[@id, "NewTextArea~~51887~~1"]

但现在 id 的 '51887' 部分变得多样化(每次都在变化),所以我需要选择 NewtextArea~~*~~1 元素而不实际指定数字。有没有办法可以通配字符串的一部分,以便它匹配特定的模式?我尝试使用starts-with 和ends-with 但无法正常工作:

//textarea[starts-with(@id, 'NewTextArea~~') and ends-with(@name, '~~1')]

请记住,还有其他字段,区别在于末尾的数字。

任何建议或指导将不胜感激:)

【问题讨论】:

    标签: xpath


    【解决方案1】:

    我尝试使用starts-with 和ends-with,但无法使用:

    //textarea[starts-with(@id, 'NewTextArea~~') and ends-with(@name, '~~1')]
    

    ends-with() 仅在 XPath 2.0 中作为标准函数提供,您似乎使用的是 XPath 1.0

    使用

    //textarea
       [starts-with(@id, 'NewTextArea~~')
      and
       substring(@id, string-length(@id) - 2) = '~~1'
       ]
    

    解释

    查看这个问题的答案,了解如何在 XPath 1.0 中实现ends-with()

    https://stackoverflow.com/a/405507/36305

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-02-04
      • 1970-01-01
      • 1970-01-01
      • 2019-09-20
      • 1970-01-01
      • 2012-03-24
      • 2011-08-05
      • 1970-01-01
      相关资源
      最近更新 更多