How to locate the upper child element using xpath

Viewed 114
<div> 
  <input>
   <label id="Star">
    <input>
    <label id="Moon">
    <input>
    <label id="Sun">
</div>

Query:

driver.findElement(By.xpath("//label(@id='star')/parent::div/input`).

I am getting the 3 matching nodes but I need to find the <label id="Star"> of <input>.

1 Answers

Simply wrap using xpath indexing :

(//label(@id='star')/parent::div/input)[1]

should point to first input, for second change that to [2] and so on..

Update 1 :

if input is parent then this :

//label[@id='Star']/..

if input is preceding-sibling then this :

//label[@id='Star']/preceding-sibling::input

I believe you are looking for preceding-sibling

Related