title: Java Selenium - Find next element
body: <p>I have a very similar problem to this one: <a href="https://stackoverflow.com/questions/8008588/selenium-find-element-next-to-other-element">Selenium: find element &quot;next to&quot; other element</a></p>
  
  <p>My element looks like this:</p>
  
  <pre><code>&lt;div class="a-Trinity has-left-widget has-right-widget" style="cursor: pointer;"&gt;
  &lt;div class="trinity-left-widget" style=""&gt;
      &lt;div&gt;
          &lt;span class="layer-style-rule-slice" style="background-color: rgb(233, 34, 91); border-color: rgb(255, 255, 255); margin-top: 12px;"&gt;&lt;/span&gt;
          &lt;span class="layer-style-rule-slice" style="background-color: rgb(75, 170, 234); border-color: rgb(255, 255, 255); margin-top: 6px;"&gt;&lt;/span&gt;
          &lt;span class="layer-style-rule-slice" style="background-color: rgb(42, 200, 159); border-color: rgb(255, 255, 255); margin-top: 0px;"&gt;&lt;/span&gt;
          &lt;span class="layer-style-rule-slice" style="background-color: rgb(153, 207, 34); border-color: rgb(255, 255, 255); margin-top: -6px;"&gt;&lt;/span&gt;
          &lt;span class="layer-style-rule-slice" style="background-color: rgb(247, 151, 29); border-color: rgb(255, 255, 255); margin-top: -12px;"&gt;&lt;/span&gt;
      &lt;/div&gt;
  &lt;/div&gt;  
  &lt;div class="gwt-Label trinity-middle-widget form-text"&gt;Street Lights&lt;/div&gt;  
  &lt;div class="trinity-right-widget" style=""&gt;
      &lt;div class="small-switch-container layer-right-switch"&gt;
          &lt;div class="small-switch switch-active"&gt; 
              &lt;div class="small-switch-button"&gt;&lt;/div&gt; 
          &lt;/div&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p></p>
  
  <p>I want to check whether switch-active is shown or not. I am using the following to find the correct element:</p>
  
  <pre><code>    String className = "gwt-Label trinity-middle-widget form-text";
      String htmlElement = "div";
      String textToFind = layerName; // i.e. Street Light
  
      List&lt;WebElement&gt; elements = Common.findElementsUsingHtmlXpathClass(driver, htmlElement, className);
  
      Common.myPrint(thisClass + " elements count: " + elements.size());
      for (WebElement element : elements) {
          // select an element
          String text = Common.getAllAttributes(element, driver);
          if (text != null) {
              text = text.trim();
              if (text.contains(textToFind)) {
                  // so this is my element
                  // here is where I need to get the next one
  
              }
          }
      }
  </code></pre>
  
  <p>The code for findElementsUsingHtmlXpathClass:</p>
  
  <pre><code>public static List&lt;WebElement&gt; findElementsUsingHtmlXpathClass(WebDriver driver, String htmlElement,
          String className) {
      List&lt;WebElement&gt; elements = driver
              .findElements(By.xpath("//" + htmlElement + "[contains(@class, '" + className + "')]"));
      return elements;
  }
  </code></pre>
  
  <p>I cannot see how to apply the solution given in the article quoted, as I do not have an element I can just find by ID.</p>
  
  <p>How can I get this next element?</p>
  
  <p>Went with this solution from cruisepandey. I merely changed the name of the method to reflect it's new function:</p>
  
  <pre><code>    public static List&lt;WebElement&gt; findElementsFollowingHtmlXpathClass(WebDriver driver, String htmlElement,
          String className) {
      List&lt;WebElement&gt; elements = driver
              .findElements(By.xpath("//" + htmlElement + "[contains(@class, '" + className + "')]/following-sibling::div[@class='trinity-right-widget']/descendant::div[contains(@class,'switch-active')]"));
  
      return elements;
  }
  </code></pre>