硒> Web Drive:如何通过xpath(ruby)查找嵌套元素

时间:2013-06-12 14:40:01

标签: ruby xpath selenium

我有一个嵌套的< 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”下的所有链接?

我尝试了两种方法但都失败了:(

  1. div= @driver.find_element(:id => 'international-map')
  2. e=@driver.find_elements(:xpath => "//div[@id='international-map']//div[@tag_name='a']")
  3. 谢谢(即使是C#和Java代码帮助)

2 个答案:

答案 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()