This might be useful for someone else: Using this HTML example
<div class="ParentDiv"> <label for="label">labelName</label> <input type="button" value="elementToSelect"> </div> <div class="DontSelect"> <label for="animal">pig</label> <input type="button" value="elementToSelect"> </div>
If, for example, I want to select an element in the same section (e.g. div) as a label, you can use this
//label[contains(., 'labelName')]/parent::*//input[@value='elementToSelect']
It just means looking for a label (it could be something like a , h2 ) named labelName . Go to the parent of this label (that is, div class="ParentDiv" ). elementToSelect search among the descendants of this parent to find any child element with the value elementToSelect . However, he will not select the second elementToSelect with the DontSelect div as the parent.
The trick is that you can reduce the search area for an element by going to the parent first and then searching for the desired element in the descendant of that parent. Other syntax such as following-sibling::h2 may also be used in some cases. This means that brother and sister follow element h2 . This will work for items of the same level having the same parent.
papigee Jan 25 '17 at 14:16 2017-01-25 14:16
source share