Ruby Selenium Web Driver: How to count child element nodes of a specific node

♀尐吖头ヾ 提交于 2020-01-21 10:23:25

问题


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


回答1:


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



回答2:


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



来源:https://stackoverflow.com/questions/43244041/ruby-selenium-web-driver-how-to-count-child-element-nodes-of-a-specific-node

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!