20

我想在我的属性中使用通配符。例如,这是我的常规 XPath:

//input[@id='activation:j_idt84:voId:1']`

我想j_idt用通配符替换数字,因为数字是动态的。我正在寻找这样的东西:

//input[@id='activation:*:voId:1']

我不知道如何解决这个问题。我的想法甚至可能吗?

4

2 回答 2

35

不幸的是,XPath 中没有字符串通配符。但是,您可以使用多个contains()andstarts-with()来过滤这样的内容。

//input[starts-with(@id, 'activation:') and contains(@id, ':voId:1')]

此外,这个答案也可能有用:selenium: Is it possible to use the regexp in selenium locators

于 2012-09-19T13:41:07.890 回答
4

可以matches使用XPath 2.0 中提供的函数来使用字符串通配符:

//input[matches(@id, 'activation:.*:voId:1')]
于 2012-09-19T13:46:05.487 回答