这个 xpath 看起来正确吗?
page.getByXPath("//*[@href='http://www.example.com/index.do/abc/1_*'");
我需要转义任何字符吗?
我试图获取页面上具有以下模式的所有 ahref 链接:
http://www.example.com/index.do/abc/1_
所以这些都应该被检索:
http://www.example.com/index.do/abc/1_asdf-asdfasdf
http://www.example.com/index.do/abc/1_223
http://www.example.com/index.do/abc/1_as.php
http://www.example.com/index.do/abc/1_2222233
page.getByXPath("//*[@href='http://www.example.com/index.do/abc/1_*'");
Do I need to escape any characters?
I am trying to get all ahref links on the page that have the pattern of:
http://www.example.com/index.do/abc/1_
so these should all be retrieved:
http://www.example.com/index.do/abc/1_asdf-asdfasdf
http://www.example.com/index.do/abc/1_223
http://www.example.com/index.do/abc/1_as.php
http://www.example.com/index.do/abc/1_2222233
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
XPath 中没有通配符。您想要这样的东西:
这依赖于
contains
函数。您还可以使用starts-with
函数:There are no wildcards in XPath. You want something like this instead:
This relies on the
contains
function. You can also use thestarts-with
function:如果您使用的是 XPath 1.0,则无法以这种方式进行通配符(或正则表达式)匹配。 (升级到 2.0 可能允许)
对于这种情况,我建议对前缀
//a[contains(@href, 'http://www.example.com/index.do/abc/1_')]
(注意,我将选择限制为仅一个标签)
If you are using XPath 1.0, you cannot do wildcard (or regular expression) matches in that way. (Upgrading to 2.0 may allow that)
For this case, I'd suggest doing a 'contains' test for the prefix
//a[contains(@href, 'http://www.example.com/index.do/abc/1_')]
(Note, I limited the select to just a tags)
查看您的 XPath 库是否支持
starts-with(string1,string2)
并使用:另外,你不能用
a
替换*
吗?See if your XPath library supports
starts-with(string1,string2)
and use:Also, can't you replace
*
bya
?