问题
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