title: Finding previous sibling element using selenium xpath dynamically
body: <p>I am writing selenium scripts for the following code.</p>
  
  <pre><code>&lt;div id="abc" class="ui-selectmanycheckbox ui-widget hpsapf-chechkbox"&gt;
       &lt;div class="ui-chkbox ui-widget"&gt;
          &lt;div class="ui-helper-hidden-accessible"&gt;
             &lt;input id="abc:0" name="abc" type="checkbox" value="0" checked="checked"&gt;
          &lt;/div&gt;
          &lt;div class="ui-chkbox-box ui-widget ui-corner-all ui-state-default ui-state-active"&gt;
             &lt;span class="ui-chkbox-icon ui-icon ui-icon-check ui-c"&gt;&lt;/span&gt;
          &lt;/div&gt;
       &lt;/div&gt;
       &lt;span class="hpsapf-radio-label"&gt;
          &lt;label for="abc:0"&gt;Herr&lt;/label&gt;
       &lt;/span&gt;
       &lt;div class="ui-chkbox ui-widget"&gt;
          &lt;div class="ui-helper-hidden-accessible"&gt;
             &lt;input id="abc:1" name="abc" type="checkbox" value="1"&gt;
          &lt;/div&gt;
          &lt;div class="ui-chkbox-box ui-widget ui-corner-all ui-state-default"&gt;
             &lt;span class="ui-chkbox-icon ui-icon ui-icon-blank ui-c"&gt;&lt;/span&gt;  
          &lt;/div&gt;
       &lt;/div&gt;
       &lt;span class="hpsapf-radio-label"&gt;
          &lt;label for="abc:1"&gt;Frau&lt;/label&gt;
       &lt;/span&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>These are the checkbox like the following.The number of the checkboxes are changed as per database values.</p>
  
  <p><a href="https://i.stack.imgur.com/wmPBq.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/wmPBq.png" alt="enter image description here"></a></p>
  
  <p>In my code i am first checking whether the "Frau" check box is selected or not. so i tried following snippet.</p>
  
  <pre><code>WebElement mainElement= driver.findElement(By.id("abc"));
  WebElement label=mainElement.findElement(By.xpath(".//label[contains(@for,'abc')][text() = 'Frau']"));
  WebElement parent = label.findElement(By.xpath(".."));
  WebElement div = parent.findElement(By.xpath("preceding-sibling::::div"));
  WebElement checkBox = div.findElement(By.className("ui-chkbox-box"));
  String css = checkBox.getAttribute("class");
  if(css.contains("ui-state-active")) {
      return "checked";
  }
  else
  {
      return "unchecked";
  }
  </code></pre>
  
  <p>But when i tried to execute this script. <code>WebElement div = parent.findElement(By.xpath("preceding-sibling::::div"));</code> gives me the first div tag and not the preceding one. I want a preceding sibling.</p>