1

I have a table which can be found here: Ontario Gov Employee Directory and I'm trying to loop through the table to pull out the data, but struggling to find the xpath to be able to do so.

The table doesn't have an id, when I inspect the element I see:

<table title="results_list" border="0" width="100%" cellspacing="0" cellpadding="0">

  <tbody>
    <tr>
      <td class="content" valign="top" align="right" width="50">1. &nbsp;</td>
      <td class="content">[<a class="results" href='javascript:showEmployeeDetail("32528")'>Aagaard, Lindsay</a>] [ Senior Policy Advisor ] [TREASURY BOARD SECRETARIAT]
        <br>[DEPUTY PREMIER AND PRESIDENT OF THE TREASURY BOARD, Toronto]

        <!-- [416-327-0948]  -->



        [416-327-0948] [



        <a href="mailto:[email protected]">
                                                                            [email protected]</a>]
      </td>
    </tr>
    <tr>
      <td>&nbsp;</td>
    </tr>

    <tr>
      <td class="content" valign="top" align="right" width="50">2. &nbsp;</td>
      <td class="content">[<a class="results" href='javascript:showEmployeeDetail("34417")'>Aalto, Margaret</a>] [ Probation Officer ] [CHILDREN AND YOUTH SERVICES]
        <br>[THUNDER BAY, Thunder Bay]

        <!-- [807-475-1310]  -->



        [807-475-1310] [



        <a href="mailto:[email protected]">
                                                                            [email protected]</a>]
      </td>
    </tr>
    <tr>
      <td>&nbsp;</td>
    </tr>

    <tr>
      <td class="content" valign="top" align="right" width="50">3. &nbsp;</td>
      <td class="content">[<a class="results" href='javascript:showEmployeeDetail("9187")'>Aarlaht, Andrew</a>] [ Business Analyst ] [COMMUNITY AND SOCIAL SERVICES]
        <br>[HAMILTON, BUSINESS SERVICES UNIT, Hamilton]

        <!-- [905-521-7335]  -->



        [905-521-7335] [



        <a href="mailto:[email protected]">
                                                                            [email protected]</a>]
      </td>
    </tr>
    <tr>
      <td>&nbsp;</td>
    </tr>

    <tr>
      <td class="content" valign="top" align="right" width="50">4. &nbsp;</td>
      <td class="content">[<a class="results" href='javascript:showEmployeeDetail("9187")'>Aarlaht, Andrew</a>] [ Business Analyst ] [CHILDREN AND YOUTH SERVICES]
        <br>[HAMILTON, BUSINESS SERVICES UNIT, Hamilton]

        <!-- [905-521-7335]  -->



        [905-521-7335] [



        <a href="mailto:[email protected]">
                                                                            [email protected]</a>]
      </td>
    </tr>
    <tr>
      <td>&nbsp;</td>
    </tr>

    <tr>
      <td class="content" valign="top" align="right" width="50">5. &nbsp;</td>
      <td class="content">[<a class="results" href='javascript:showEmployeeDetail("19146")'>Aarons, Drew</a>] [ Messenger ] [LEGISLATIVE OFFICES]
        <br>[PARLIAMENTARY PROTOCOL, Toronto]

        <!-- [416-325-7455]  -->



        [416-325-7455] [



        <a href="mailto:[email protected]">
                                                                            [email protected]</a>]
      </td>
    </tr>
    <tr>
      <td>&nbsp;</td>
    </tr>

    <tr>
      <td class="content" valign="top" align="right" width="50">6. &nbsp;</td>
      <td class="content">[<a class="results" href='javascript:showEmployeeDetail("113729")'>Aaswaakshin, Neegann</a>] [ Articling Student ] [ABORIGINAL AFFAIRS]
        <br>[LEGAL SERVICES, Toronto]

        <!-- [416-212-2271]  -->



        [416-212-2271] [



        <a href="mailto:[email protected]">
                                                                            [email protected]</a>]
      </td>
    </tr>
    <tr>
      <td>&nbsp;</td>
    </tr>

    <tr>
      <td class="content" valign="top" align="right" width="50">7. &nbsp;</td>
      <td class="content">[<a class="results" href='javascript:showEmployeeDetail("32196")'>Abad, Lilian</a>] [ Executive Assistant ] [TRANSPORTATION]
        <br>[GO TRANSIT, Toronto]

        <!-- [416-202-5506]  -->



        [416-202-5506] [



        <a href="mailto:[email protected]">
                                                                            [email protected]</a>]
      </td>
    </tr>
    <tr>
      <td>&nbsp;</td>
    </tr>

    <tr>
      <td class="content" valign="top" align="right" width="50">8. &nbsp;</td>
      <td class="content">[<a class="results" href='javascript:showEmployeeDetail("114240")'>Abadesso, Jennifer</a>] [ Employment Program Consultant (Acting) ] [TRAINING, COLLEGES AND UNIVERSITIES]
        <br>[FOUNDATION SKILLS, Toronto]

        <!-- [416-327-2065]  -->



        [416-327-2065] [



        <a href="mailto:[email protected]">
                                                                            [email protected]</a>]
      </td>
    </tr>
    <tr>
      <td>&nbsp;</td>
    </tr>

    <tr>
      <td class="content" valign="top" align="right" width="50">9. &nbsp;</td>
      <td class="content">[<a class="results" href='javascript:showEmployeeDetail("104293")'>Abakunzi, Louis</a>] [ Customer Service Representative (Bilingual) ] [GOVERNMENT AND CONSUMER SERVICES]
        <br>[SERVICEONTARIO CONTACT CENTRE - NORTH YORK, Toronto]

        <!-- [416-235-2999]  -->



        [416-235-2999] [



        <a href="mailto:[email protected]">
                                                                            [email protected]</a>]
      </td>
    </tr>
    <tr>
      <td>&nbsp;</td>
    </tr>

    <tr>
      <td class="content" valign="top" align="right" width="50">10. &nbsp;</td>
      <td class="content">[<a class="results" href='javascript:showEmployeeDetail("19309")'>Aban, Edencio</a>] [ Audit Supervisor ] [ATTORNEY GENERAL]
        <br>[AUDIT AND COMPLIANCE, Toronto]

        <!-- [416-326-6295]  -->



        [416-326-6295] [



        <a href="mailto:[email protected]">
                                                                            [email protected]</a>]
      </td>
    </tr>
    <tr>
      <td>&nbsp;</td>
    </tr>

  </tbody>
</table>

How can I loop through the data in these rows?

1 Answer 1

1

It's a table within a table and then has some pretty standard formatting. What challenge are you having?

The table doesn't have an id, when I inspect the element I see:

It has other attributes you can use, like the title. Use the xpath //table[@title="results_list"]/tbody/tr/td to find each data element from the innermost table. Or remove the last /td from the xpath to get each row. After that, find each td element under it and use its text.

Note: The innermost table has 1st column with serial number and 2nd column with the actual data. I recommend getting each td and then using the 'innerHTML' attribute or elem.text. After that, use a regular exppresion to extract the different parts.

>>> all_tdata = driver.find_elements_by_xpath('//table[@title="results_list"]/tbody/tr/td')
>>> for td in all_tdata:
...     print td.get_attribute('innerHTML')  # save this in var and regex it
...     # or
...     data = td.text
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! I did something similar, I used xpath and the getText() function to retrieve the inner HTML, and have parse the string accordingly. @aneroid

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.