我尝试索引由xpath返回的结果.例如:
xpath = '//a[@id="someID"]'
可以返回一些结果.我想得到他们的清单.我认为这样做:
numOfResults = sel.get_xpath_count(xpath)
l = []
for i in range(1,numOfResults+1):
l.append(sel.get_text('(%s)[%d]'%(xpath, i)))
之所以可行,是因为使用firefox的Xpath检查器执行类似的操作:
(//a[@id='someID'])[2]
返回第二个结果.
为什么行为会有所不同以及如何用硒来做这种事的想法
谢谢
解决方法:
您可以尝试使用xpath / html / descendant :: a [@ id =“ someID”]吗?您可以将/ html替换为其他链接祖先,例如id(‘content’).然后,您应该能够使用[1],[2]等定位各个链接.
从位于http://www.w3.org/TR/xpath#path-abbrev的XPath TR:
NOTE: The location path
//para[1]
does not mean the same as the location path/descendant::para[1]
. The latter selects the first descendant para element; the former selects all descendant para elements that are the first para children of their parents.