【问题标题】:How to ignore a specific character in an Xpath?如何忽略 Xpath 中的特定字符?
【发布时间】:2021-11-18 02:31:21
【问题描述】:

我试图在DOM 中找到以下元素 使用硒:

<button>
  App\u00ADly
</button>

如您所见,Apply 一词之间有一个软连字符 我想知道是否有任何方法可以忽略此字符并使以下 Xpath 起作用。

//button[text()='Apply']

提前致谢。

【问题讨论】:

    标签: java xml selenium selenium-webdriver xpath


    【解决方案1】:

    我认为文本 \u00AD 是动态的,或者您想跳过。您可以在xPath 中使用contains 以及and 条件。

    //button[contains(text(),'App') and contains(text(),'ly')]
    

    【讨论】:

      【解决方案2】:

      contains 在很多地方都被错误地使用了。还有更多xpath函数如starts-withends-with(xpath v2.0)。

      但由于 selenium 被标记为使用 xpath v1.0,您将无法使用 ends-with

      你可以试试这样的:

      //input[starts-with(text(),'App') and contains(text(),'ly')]
      

      【讨论】:

        【解决方案3】:

        “\u00AD”是 DOM 文本节点中的 6 个字符序列,还是向我们显示 DOM 文本中单个字符的一种方式?

        如果是单个字符,您可以使用translate() 将其从比较中消除://button[translate(., 'X', '') = 'Apply'] 其中 X 是 xAD 字符(我不知道在 Selenium 中有哪些转义可能性)。

        【讨论】:

        • 天才!!!这正是我想要的,它奏效了。谢谢大佬。
        • @Andres,SO 中的约定是通过单击答案旁边的勾号/复选标记将答案标记为已接受。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-09-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-06-19
        相关资源
        最近更新 更多