Get all immediate children and nothing deeper

heymona picture heymona · Jan 6, 2011 · Viewed 29.7k times · Source
WebElement body = browser.findElement(By.xpath("//body"));

body.findElement(By.xpath("")); // I want to get all child elements 
                                // inside body, but nothing deeper.

Example document.

<html>
  <body>
    <div>
    </div>
    <span>
      <table>
      </table>
    </span>
  </body>
</html>

Expected result is div and span. I have no controll over the documents and they vary greatly.

Answer

peter.murray.rust picture peter.murray.rust · Jan 6, 2011

("*") gives all the child elements of the context node. So use:

body.findElement(By.xpath("*"));