link a1

Selenium> Web Drive: how to find a nested element by xpath (ruby)

I have a nested <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> 

How can I get all the links under the "international map"?

I tried two approaches and could not :(

  • div= @driver.find_element(:id => 'international-map')
  • e=@driver.find _elements(:xpath => "//div[@id='international-map']//div[@tag_name='a']")

Thank you ( even c # and java code )

+6
source share
2 answers

Good! You can also use #css or #xpath as shown below:

 @driver.find_elements(:css,"div#international-map a").map(&:text) # => [" link a1 ", " link a2 ", " link a3 ", " link b1 ", " link b2 "] 

or

 @driver.find_elements(:xpath,"//div[@id = 'international-map']//a").map(&:text) # => [" link a1 ", " link a2 ", " link a3 ", " link b1 ", " link b2 "] 
+14
source

Correct XPath Expression

 //div[@id = 'international-map']//a/string() 
+1
source

Source: https://habr.com/ru/post/947119/


All Articles