title: How to use Xpath in HtmlUnit to get access to table inside a bunch of nested divs
body: <p>I have created a program to get access to a web application using HtmlUnit. After I login to the application, I want to open jobs listed in a table one by one and go to the next page. Unfortunately, the table does not have an id attribute that I can use to getElementById during initialization of HtmlTable variable. </p>
  
  <p>So, I decided to use getByXPath function and pass a path through which the system can identify the table I am referring to and get me going. The problem I am facing here is that the table I want access to is nested under a bunch of divs and a table tag. Hierarchically, the table I am trying to get access lies under the below-mentioned fashion </p>
  
  <p><em>HTML -> Body -> 12 divs -> table -> tbody ->tr -> td -> 10 divs -> table</em> </p>
  
  <p>I could not start from the middle in the Xpath function. So, I passed in the entire path in the following manner:</p>
  
  <pre><code>for(int i=0;i&lt;2;i++) {
       final HtmlTable table = (HtmlTable) page2.getByXPath("//html/body/div/div/div/div/div/div/div/div/div/div/div/div/table/tbody/tr/td/div/div[@id='idName']/div/div/div[@class='className']/div/div/div/div/div/table[@class='TableClass']").get(i);
       System.out.println("Printing table: " + table.asText());
  } 
  </code></pre>
  
  <p>The error I get for this is as follows:</p>
  
  <p><strong>Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0</strong></p>
  
  <p>Since the table is buried under a larger number of other tags, I do not know how to get an efficient way to access it. </p>
  
  <p>Now, I tried to use the getChildNodes function to get all nodes present in the page and list the Table nodes alone. The code snippet is here:</p>
  
  <pre><code>        List list = page2.getChildNodes();
          int k = 0;
          while(list.iterator() != null)
          {
              HtmlElement anElement = (HtmlElement) list.get(k);
              if(anElement instanceof HtmlTable)
              {
                  System.out.println("Hello :" +anElement.asText());
              }
              k =  k + 1;
          }
  </code></pre>
  
  <p>Here I am having a <strong>ClassCastException</strong> for the HtmlElement cast operation. </p>