简体   繁体   English

Xpath 包含正则表达式模式

[英]Xpath Contains with Regex pattern

def xpath ="//span[contains(text(),'0')]"

with the above XPath, the text contains only numbers and this is dynamic.对于上面的 XPath,文本只包含数字,这是动态的。 is it possible to apply the regex pattern in the above Xpath?是否可以在上述 Xpath 中应用正则表达式模式?

regEx pattern: ^[0-9]正则表达式模式: ^[0-9]

def myXpath = //span[contains(text(),'^[0-9]')]'
testObject.addProperty('xpath',ConditionType.EQUALS,myXpath)
def action = WebUI.getText(testObject)

I'm getting the below error我收到以下错误

Web element with id: 'objectName' located by '//span[contains(text(),'^[0-9]')]' not found (Root cause: com.kms.katalon.core.exception.StepFailedException: com.kms.katalon.core.webui.exception.WebElementNotFoundException: Web element with id: 'objectName' located by '//span[contains(text(),'^[0-9]')]' not found

Let me know how can I apply the regex pattern in XPath?让我知道如何在 XPath 中应用正则表达式模式?

Selenium unfortunately only supports the very old XPath 1.0 standard, which has no support for regular expressions. Selenium 不幸的是只支持非常老的 XPath 1.0 标准,它不支持正则表达式。 Regular expression support came with XPath 2.0 but Selenium has yet to upgrade. XPath 2.0 附带正则表达式支持,但 Selenium 尚未升级。

It's sometimes possible to devise workarounds using translate() , for example translate(x, '0123456789', '')=x is true if x contains no digits.有时可以使用translate()来解决 devise 变通办法,例如translate(x, '0123456789', '')=x如果 x 不包含数字则为真。 (But I'm not sure what condition you're actually trying to test for, since you haven't stated the precise requirement, you've only shown us code that doesn't work.) (但我不确定您实际上要测试什么条件,因为您没有说明确切的要求,您只向我们展示了不起作用的代码。)

If you want to select element with number as text you can use workaround如果您想使用数字作为文本的 select 元素,您可以使用解决方法

//span[number(.) >= 0]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM