2010-01-05 112 views
1
page.getByXPath("//*[@href='http://www.example.com/index.do/abc/1_*'"); 

我是否需要转义任何字符?这个xpath看起来是否正确?

我试图让有格局网页上的所有链接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 

回答

4

有XPath中没有通配符。你想要这样的代替:

page.getByXPath("//*[contains(@href,'http://www.example.com/index.do/abc/1_')]"); 

这依靠the contains function。您也可以使用starts-with功能:

//*[starts-with(@href,'http://www.example.com/index.do/abc/1_')] 
+1

是的,这就是为什么它在我的答案旁边是'contains'。 – Welbog 2010-01-05 20:18:11

0

如果您正在使用XPath 1.0,你不能做通配符(或正则表达式)以这种方式相匹配。 (升级到2.0可允许)

对于这种情况,我建议做一个 '包含' 用于测试的前缀

//一个[含有(@href, 'http://www.example.com/index.do/abc/1_')]

(注意,我限制了选择,只是一个标签)

0

看看你的XPath库支持starts-with(string1,string2)及用途:

page.getByXPath("//*[starts-with(@href, 'http://www.example.com/index.do/abc/1_')"); 

而且,你不能代替*作者:a

+0

我正在使用java 1.6 – mrblah 2010-01-05 20:36:05