【问题标题】:Regex in Selenium Xpath Not Working //*[contains(@content-desc,"Resend in .*?[0-9a-zA-Z]*[0-9][0-9a-zA-Z].* sec")]Selenium Xpath 中的正则表达式不起作用 //*[contains(@content-desc,"Resend in .*?[0-9a-zA-Z]*[0-9][0-9a-zA-Z].* sec ")]
【发布时间】:2021-09-06 21:03:49
【问题描述】:

我在 UI 中的文本是 "Resend in 48 sec" 其中 "48" 是一个动态计时器,因此我想使用包含和内部包含我正在使用正则表达式,但仍然无法找到元素

//*[contains(@content-desc,"Resend in .*?[0-9a-zA-Z]*[0-9][0-9a-zA-Z].* sec")]

【问题讨论】:

    标签: selenium selenium-webdriver xpath appium appium-android


    【解决方案1】:

    Selenium 仍在使用 XPath 1.0,它是 20 多年前定义的,不支持正则表达式。

    【讨论】:

      【解决方案2】:

      您可以改用这个 XPath:

      //*[contains(@content-desc,"Resend in") and(contains(@content-desc,"sec"))]
      

      现在你可以获取这个元素,获取它的content-desc属性值,然后验证该属性的内容

      【讨论】:

      • 部分正确,但我想确定 Number 是否显示。这就是我使用 Regex 的原因
      • 我们在哪里有正则表达式?
      【解决方案3】:

      你可能想这样做:

      String regEx = ".*?[0-9a-zA-Z]*[0-9][0-9a-zA-Z].*";
      driver.findElement(By.xpath("//*[contains(@content-desc,'Resend in') + '"+regEx+"'  + and(contains(@content-desc, 'sec')) ]"));
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-12-11
        • 1970-01-01
        • 2018-08-27
        • 2022-08-15
        • 2013-08-24
        • 1970-01-01
        相关资源
        最近更新 更多