我有一个嵌套的< div>:
<div id="international-map">
<div id='a'>
<a> link a1 </a>
<a> link a2 </a>
<a> link a3 </a>
</div>
<div id='b'>
<a> link b1 </a>
<a> link b2 </a>
</div>
</div>
如何获取“international-map”下的所有链接?
我尝试了两种方法但都失败了:(
div= @driver.find_element(:id => 'international-map')
e=@driver.find_elements(:xpath => "//div[@id='international-map']//div[@tag_name='a']")
谢谢(即使是C#和Java代码帮助)
答案 0 :(得分:15)
好!您也可以使用#css
或#xpath
,如下所示:
@driver.find_elements(:css,"div#international-map a").map(&:text)
# => [" link a1 ", " link a2 ", " link a3 ", " link b1 ", " link b2 "]
或
@driver.find_elements(:xpath,"//div[@id = 'international-map']//a").map(&:text)
# => [" link a1 ", " link a2 ", " link a3 ", " link b1 ", " link b2 "]
答案 1 :(得分:1)
正确的XPath表达式是
//div[@id = 'international-map']//a/string()