0

Am a novice selenium programmer please help me with this...

I have below html and am trying to count the child nodes of element <div class="result-controls"> which is 4 (1 div and 3 li elements)

<div class="result-controls">
   <div class="attachment">
      <li class="add-attachment-button-column"><a><button>ADD ATTACHMENTS</button></a></li><input type="file" multiple="" style="display: none;">
   </div>
   <li class="sign-button-column"><a href="javascript:void(0)"><button>SIGN</button></a></li>
   <li class="draft-button-column"><a><button>DRAFT</button></a></li>
   <li class="delete-column"> <a href="javascript:void(0)"><svg class="glyphicon-trash" viewBox="0 0 100 100"><use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#trash"></use></svg> </a></li>
</div>

currently am calculating the count separately as below (row variable has above html)

  li_actions_count = row.find_elements(:xpath => "./li").length
  div_actions_count = row.find_elements(:xpath => "./div").length

can anyone help me with a simple way to do this in Ruby using selenium web driver

OTUser
  • 3,788
  • 19
  • 69
  • 127

2 Answers2

2

There are some possible ways depending on how specific you want the XPath to be, f.e. if you simply want to count child elements of any name, then you can use * :

row.find_elements(:xpath => "*").length

and if you want to specifically count child elements of certain names i.e li and div :

row.find_elements(:xpath => "*[self::li|self::div]").length
har07
  • 88,338
  • 12
  • 84
  • 137
  • Thanks @har07, also is it possible to get the child element name like for 1st and 2nd `li` elements I want child element of path `li/a` as `button` and for 3rd `li` element of path `li/a` as `svg` is it possible? – OTUser Apr 06 '17 at 21:00
  • No. XPath only able to return something that exists in the source XML, other than that there only very limited possibilities, especially using XPath 1.0 which selenium support. – har07 Apr 07 '17 at 02:06
  • Thanks fo rthe update, am trying to find the `svg` element by class as `row.find_element(:xpath => "./li/a/svg[contains(@class,'deleteIcon')]")` but its throwing an exception, I couldnt figure whats wrong with it, could you please help? – OTUser Apr 07 '17 at 02:39
  • can you also help me with this http://stackoverflow.com/questions/43385291/ruby-selenium-web-drive-how-to-find-specific-element-by-xpath-div-id-and-css-cl – OTUser Apr 13 '17 at 06:45
1

You can get the direct children of row using xpath = "./*"

Moe Ghafari
  • 2,227
  • 1
  • 12
  • 17