代码之家  ›  专栏  ›  技术社区  ›  Prophet

regex可以用于XPATH属性定义吗?[副本]

  •  0
  • Prophet  · 技术社区  · 7 年前

    sometext12345_text 使用下面的正则表达式。

    String expr = "//*[contains(@id, 'sometext[0-9]+_text')]";
    driver.findElement(By.xpath(expr));
    

    但似乎不起作用。有人能帮忙吗?

    0 回复  |  直到 6 年前
        1
  •  73
  •   Robin    12 年前

    //*[starts-with(@id, 'sometext') and ends-with(@id, '_text')]
    

    (正如保罗t, //*[boolean(number(substring-before(substring-after(@id, "sometext"), "_text")))]

    在XPath2.0中,请尝试

    //*[matches(@id, 'sometext\d+_text')]
    
        2
  •  5
  •   paul trmbrth    12 年前

    EXSLT extensions ,和 regexp:test()

    这对你有用吗?

    String expr = "//*[regexp:test(@id, 'sometext[0-9]+_text')]";
    driver.findElement(By.xpath(expr));
    
        3
  •  3
  •   Harish Kiran    6 年前

    在xpath 1.0中也不支持Robins的答案以结尾。。只支持以开头。。。 所以如果你的情况不是很特别的话…你可以用这样对我有用的

    //*[starts-with(@id,'sometext') and contains(@name,'_text')]`\