1

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.

1 Answer 1

3

Just use find_element instead of find_elements:

first_row = table_id.find_element(By.TAG_NAME, "tr")
Sign up to request clarification or add additional context in comments.

2 Comments

Ah yes this will just find the first element instead of finding all the elements and appending it to the list. Great. Thank you.
@RiazLadhani yup, additional hint: you have multiple row.find_elements(By.TAG_NAME, "td") calls - remember that every selenium call like this one is an HTTP request through the JSON wire protocol and it takes time. So, to improve performance, you can remember the result in a variable: cells = row.find_elements(By.TAG_NAME, "td") and reuse.

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.