Riaz Ladhani
Riaz Ladhani

Reputation: 4062

Selenium Python how to locate a specified row and a column from a table of elements

I have a table with some rows and columns. I would like to iterate through the table and locate row 81 and 82 and column index 4 (Surname column) I would like to check the value from column 4 for row 81 and 82. The data is fixed so identifying row 81 and 82 for my test purpose is fine.

I have made a start with some code to get the table and iterate through the rows. How do i go to row 81 and column 4 directly?

My code snippet is:

def is_surname_Campbell_and_CAMPBELL_together(self): # is the surname "Campbell" and "CAMPBELL" together
    try:
        table_id = WebDriverWait(self.driver, 20).until(
            EC.presence_of_element_located((By.ID, 'data_configuration_view_preview_dg_main_body')))
        rows = table_id.find_elements(By.TAG_NAME, "tr")
        for row in rows:
            # Get the columns
            col_sname = row.find_elements(By.TAG_NAME, "td")[4]  # This is the SNAME column
            print "col_sname.text = "
            if col_sname.text == "Campbell":
                return True
        return False
    except NoSuchElementException, e:
        print "Element not found "
        print e
        self.save_screenshot("is_surname_Campbell_and_CAMPBELL_together")
        return False

HTML snippet (a small section otherwise it will be too long to paste)

    <table id="data_configuration_view_preview_dg_main_body" cellspacing="0" style="table-layout: fixed; width: 100%; margin-bottom: 17px;">
<colgroup>
<tbody>
    <tr class="GJPPK2LBJM GJPPK2LBAN" __gwt_subrow="0" __gwt_row="0">
    <td class="GJPPK2LBIM GJPPK2LBKM GJPPK2LBLM GJPPK2LBBN">
    <td class="GJPPK2LBIM GJPPK2LBKM GJPPK2LBBN">
        <div __gwt_cell="cell-gwt-uid-756" style="outline-style:none;" tabindex="0">
            <span class="" title="f1/48" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">f1/48</span>
        </div>
    </td>
    <td class="GJPPK2LBIM GJPPK2LBKM GJPPK2LBBN">
        <div __gwt_cell="cell-gwt-uid-757" style="outline-style:none;">
            <span class="" title="" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;"/>
        </div>
    </td>
    <td class="GJPPK2LBIM GJPPK2LBKM GJPPK2LBBN">
        <div __gwt_cell="cell-gwt-uid-758" style="outline-style:none;">
            <span class="" title="Keith" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">Keith</span>
        </div>
    </td>
    <td class="GJPPK2LBIM GJPPK2LBKM GJPPK2LBBN">
        <div __gwt_cell="cell-gwt-uid-759" style="outline-style:none;">
            <span class="" title="Campbell" style="background-color:yellow;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">Campbell</span>
        </div>
    </td>
    <td class="GJPPK2LBIM GJPPK2LBKM GJPPK2LBBN">
        <div __gwt_cell="cell-gwt-uid-760" style="outline-style:none;">
            <span class="" title="" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;"/>
        </div>
    </td>
<td class="GJPPK2LBIM GJPPK2LBKM GJPPK2LBBN">
<td class="GJPPK2LBIM GJPPK2LBKM GJPPK2LBBN">
<td class="GJPPK2LBIM GJPPK2LBKM GJPPK2LBBN">
</tbody>
</table>

Thanks, Riaz

Upvotes: 0

Views: 3911

Answers (1)

Andersson
Andersson

Reputation: 52665

In common case value of cell in 81 row 4 column can be defined as

driver.find_element_by_xpath('//tr[81]/td[4]').text

PS. HTML elements indexation starts from [1] element, but not from [0] as in Python

Upvotes: 3

Related Questions