我想在属性中使用通配符。例如,这是我的常规XPath:
//input[@id='activation:j_idt84:voId:1']`
我想用通配符替换掉j_idt
中的数字,因为这个数字是动态的。我正在寻找像这样的东西:
//input[@id='activation:*:voId:1']
我不知道如何解决这个问题。我的想法是否可行?
不幸的是,XPath 中没有字符串通配符。但是,您可以使用多个 contains()
和 starts-with()
来过滤此类内容。
//input[starts-with(@id, 'activation:') and contains(@id, ':voId:1')]
另外,这个答案也可能很有用:selenium:是否可以在selenium定位器中使用正则表达式
您可以使用XPath 2.0中可用的matches
函数来使用字符串通配符:
//input[matches(@id, 'activation:.*:voId:1')]