title: Iterate through table rows and print column text with Python Selenium
body: <p>I have a table (<code>&lt;table&gt;</code>) with values in each row (<code>&lt;tr&gt;</code>) from its body (<code>&lt;tbody&gt;</code>).</p>
  
  <p>The value I would lile to print out is in the <code>&lt;span&gt;</code>  inside a <code>&lt;div&gt;</code> tag.</p>
  
  <p>Inspecting the html, I see the value e.g. "Name" is in row 1 (tr[1]), column 2 (td[2]):</p>
  
  <pre><code>&lt;tr class="GAT4PNUFG GAT4PNUMG" __gwt_subrow="0" __gwt_row="0"&gt;
              &lt;td class="GAT4PNUEG GAT4PNUGG GAT4PNUHG GAT4PNUNG"&gt;
              &lt;td class="GAT4PNUEG GAT4PNUGG GAT4PNUNG"&gt;
                  &lt;div __gwt_cell="cell-gwt-uid-324" style="outline-style:none;"&gt;
                      &lt;span class="linkhover" title="Name" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;color:#00A;cursor:pointer;"&gt;Name&lt;/span&gt;
                  &lt;/div&gt;
              &lt;/td&gt;
  </code></pre>
  
  <p>I would like to loop through the table each row and print out the value in columns 2, td[2]</p>
  
  <p>I am using Python with Selenium Webdriver</p>
  
  <p>The full Xpath to the table row 1, column 2 is:</p>
  
  <pre><code>html/body/div[2]/div[2]/div/div[4]/div/div[2]/div/div[3]/div/div[5]/div/div[3]/div/div[4]/div/div[2]/div/div[4]/div/div[3]/div/div[2]/div/div/table/tbody/tr[1]/td[2]/div/span
  </code></pre>
  
  <p>I was thinking if i can start from the table, xpath as follows: </p>
  
  <pre><code>html/body/div[2]/div[2]/div/div[4]/div/div[2]/div/div[3]/div/div[5]/div/div[3]/div/div[4]/div/div[2]/div/div[4]/div/div[3]/div/div[2]/div/div/table/tbody
  </code></pre>
  
  <p>I can then use a for loop and use an index for the tr and td
  e.g for row1 use tr[i], for col2 use td[2].</p>
  
  <pre><code>html/body/div[2]/div[2]/div/div[4]/div/div[2]/div/div[3]/div/div[5]/div/div[3]/div/div[4]/div/div[2]/div/div[4]/div/div[3]/div/div[2]/div/div/table/tbody/tr[i]/td[2]/div/span
  </code></pre>
  
  <p>How can i loop through this table and print out the value of the Span class tag which is always in column 2 of the table?</p>
  
  <p>I tried to get the start of the table into a variable and then I could maybe use this to loop through the rows and columns.
  I need some help please.</p>
  
  <pre><code>table = self.driver.find_element(By.XPATH, 'html/body/div[2]/div[2]/div/div[4]/div/div[2]/div/div[3]/div/div[5]/div/div[3]/div/div[4]/div/div[2]/div/div[4]/div/div[3]/div/div[2]/div/div/table/tbody')
  </code></pre>
  
  <p>Here's the full HTML:</p>
  
  <pre><code>    &lt;table cellspacing="0" style="table-layout: fixed; width: 100%;"&gt;
      &lt;colgroup&gt;
      &lt;tbody&gt;
          &lt;tr class="GAT4PNUFG GAT4PNUMG" __gwt_subrow="0" __gwt_row="0"&gt;
              &lt;td class="GAT4PNUEG GAT4PNUGG GAT4PNUHG GAT4PNUNG"&gt;
              &lt;td class="GAT4PNUEG GAT4PNUGG GAT4PNUNG"&gt;
                  &lt;div __gwt_cell="cell-gwt-uid-324" style="outline-style:none;"&gt;
                      &lt;span class="linkhover" title="Name" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;color:#00A;cursor:pointer;"&gt;Name&lt;/span&gt;
                  &lt;/div&gt;
              &lt;/td&gt;
              &lt;td class="GAT4PNUEG GAT4PNUGG GAT4PNUNG"&gt;
              &lt;td class="GAT4PNUEG GAT4PNUGG GAT4PNUNG"&gt;
              &lt;td class="GAT4PNUEG GAT4PNUGG GAT4PNUNG"&gt;
              &lt;td class="GAT4PNUEG GAT4PNUGG GAT4PNUBH GAT4PNUNG"&gt;
          &lt;/tr&gt;
          &lt;tr class="GAT4PNUEH" __gwt_subrow="0" __gwt_row="1"&gt;
              &lt;td class="GAT4PNUEG GAT4PNUFH GAT4PNUHG"&gt;
              &lt;td class="GAT4PNUEG GAT4PNUFH"&gt;
                  &lt;div __gwt_cell="cell-gwt-uid-324" style="outline-style:none;"&gt;
                      &lt;span class="linkhover" title="Address" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;color:#00A;cursor:pointer;"&gt;Address&lt;/span&gt;
                  &lt;/div&gt;
              &lt;/td&gt;
              &lt;td class="GAT4PNUEG GAT4PNUFH"&gt;
              &lt;td class="GAT4PNUEG GAT4PNUFH"&gt;
              &lt;td class="GAT4PNUEG GAT4PNUFH"&gt;
              &lt;td class="GAT4PNUEG GAT4PNUFH GAT4PNUBH"&gt;
          &lt;/tr&gt;
          &lt;tr class="GAT4PNUFG" __gwt_subrow="0" __gwt_row="2"&gt;
              &lt;td class="GAT4PNUEG GAT4PNUGG GAT4PNUHG"&gt;
              &lt;td class="GAT4PNUEG GAT4PNUGG"&gt;
                  &lt;div __gwt_cell="cell-gwt-uid-324" style="outline-style:none;"&gt;
                      &lt;span class="linkhover" title="DOB" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;color:#00A;cursor:pointer;"&gt;DOB&lt;/span&gt;
                  &lt;/div&gt;
              &lt;/td&gt;
              &lt;td class="GAT4PNUEG GAT4PNUGG"&gt;
              &lt;td class="GAT4PNUEG GAT4PNUGG"&gt;
              &lt;td class="GAT4PNUEG GAT4PNUGG"&gt;
              &lt;td class="GAT4PNUEG GAT4PNUGG GAT4PNUBH"&gt;
          &lt;/tr&gt;
          &lt;tr class="GAT4PNUEH" __gwt_subrow="0" __gwt_row="3"&gt;
              ---
          &lt;tr class="GAT4PNUFG" __gwt_subrow="0" __gwt_row="4"&gt;       
              ---
      &lt;/tbody&gt;
  &lt;/table&gt;
  </code></pre>