Riaz Ladhani
Riaz Ladhani

Reputation: 4062

Selenium Python For loop through HTML table I want to iterate the 1st row not all rows

I am using a for loop to iterate over a HTML table. I only need to iterate the 1st row, not all of the rows.

My code snippet is below of my method:

    def is_historical_tasks_have_any_error_of_the_completed_process(self):
        try:
            table_id = WebDriverWait(self.driver, 20).until(EC.presence_of_element_located((By.ID, 'operations_monitoring_tab_historical_tasks_ct_fields_body')))
            rows = table_id.find_elements(By.TAG_NAME, "tr")
        except NoSuchElementException, e:
            return False
        count = 1
        for row in rows:
            col_project_name = row.find_elements(By.TAG_NAME, "td")[4] # This is the project name column
            col_name = row.find_elements(By.TAG_NAME, "td")[10]
            col_status = row.find_elements(By.TAG_NAME, "td")[18]
            col_last_start_time = row.find_elements(By.TAG_NAME, "td")[19]
            col_last_end_time = row.find_elements(By.TAG_NAME, "td")[20]
            col_notes = row.find_elements(By.TAG_NAME, "td")[21]
            print col_project_name.text
            print col_name.text
            print col_status.text
            print col_last_start_time.text
            print col_last_end_time.text
            print col_notes.text
            count = count + 1
            if col_notes.text == "": # If column Notes is not empty there was an error during the process, return false
                return True
            else:
                print "Error in process"
                print col_notes.text
                return False
            if count >= 1: # we want only the 1st row from the historical tasks.  Break out of the for loop when count is greater than 1
                break
        return False

The HTML is (I have removed some cols otherwise it will be too long to paste):

    <table id="operations_monitoring_tab_historical_tasks_ct_fields_body" cellspacing="0" style="table-layout: fixed; width: 100%; margin-bottom: 17px;">
    <colgroup>
        <tbody>
            <tr class="GPI5XK1CEM" __gwt_subrow="0" __gwt_row="0">
                <td class="GPI5XK1CDM GPI5XK1CFM GPI5XK1CGM">
                    <div __gwt_cell="cell-gwt-uid-3952" style="outline-style:none;">1</div>
                </td>
                <td class="GPI5XK1CDM GPI5XK1CFM">
                    <div __gwt_cell="cell-gwt-uid-3953" style="outline-style:none;" tabindex="0">
                        <input type="radio" name="rb444241113">
                    </div>
                </td>
                <td class="GPI5XK1CDM GPI5XK1CFM">
                    <div __gwt_cell="cell-gwt-uid-3954" style="outline-style:none;">
                        <span class="" title="1" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">1</span>
                    </div>
                </td>
                <td class="GPI5XK1CDM GPI5XK1CFM">
                    <div __gwt_cell="cell-gwt-uid-3955" style="outline-style:none;">
                        <span class="" title="53" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">53</span>
                    </div>
                </td>
                <td class="GPI5XK1CDM GPI5XK1CFM">
                    <div __gwt_cell="cell-gwt-uid-3956" style="outline-style:none;">
                        <span class="" title="LADemo" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">LADemo</span>
                    </div>
                </td>
                <td class="GPI5XK1CDM GPI5XK1CFM">
                    <div __gwt_cell="cell-gwt-uid-3957" style="outline-style:none;">
                        <span class="" title="Generate Stats" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">Generate Stats</span>
                    </div>
                    <td class="GPI5XK1CDM GPI5XK1CFM">
                        <div __gwt_cell="cell-gwt-uid-3959" style="outline-style:none;">
                            <span class="" title="Stats" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">Stats</span>
                        </div>
                    </td>
                    <td class="GPI5XK1CDM GPI5XK1CFM">
                        <div __gwt_cell="cell-gwt-uid-3960" style="outline-style:none;">
                            <span class="" title="11" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">11</span>
                        </div>
                    </td>
                    <td class="GPI5XK1CDM GPI5XK1CFM">
                        <div __gwt_cell="cell-gwt-uid-3962" style="outline-style:none;">
                            <span class="" title="Possible match stats" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">Possible match stats</span>
                        </div>
                    </td>
                    <td class="GPI5XK1CDM GPI5XK1CFM">
                        <div __gwt_cell="cell-gwt-uid-3972" style="outline-style:none;">
                            <span class="" title="2015-10-28 16:10:40" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">2015-10-28 16:10:40</span>
                        </div>
                    </td>
                    <td class="GPI5XK1CDM GPI5XK1CFM">
                        <div __gwt_cell="cell-gwt-uid-3973" style="outline-style:none;">
                            <span class="" title="" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;"></span>
                        </div>
                    </td>
                    <td class="GPI5XK1CDM GPI5XK1CFM GPI5XK1CAN">
                        <div __gwt_cell="cell-gwt-uid-3974" style="outline-style:none;">
                            <span class="" title="" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;"></span>
                        </div>
                    </td>
            </tr>
            <tr class="GPI5XK1CDN" __gwt_subrow="0" __gwt_row="1">
                <tr class="GPI5XK1CEM" __gwt_subrow="0" __gwt_row="2">
        </tbody>
</table>

At the line if count >= 1 it says code is not reachable. Where do I put this if count >=1? Or what is the correct way to iterate only the 1st row of the HTML table?

I was trying to use a count to keep track of the rows in the for loop.

Upvotes: 1

Views: 969

Answers (1)

alecxe
alecxe

Reputation: 473833

Just use find_element instead of find_elements:

first_row = table_id.find_element(By.TAG_NAME, "tr")

Upvotes: 3

Related Questions