【问题标题】:Selenium Python For loop through HTML table I want to iterate the 1st row not all rowsSelenium Python For循环遍历HTML表我想迭代第一行而不是所有行
【发布时间】:2015-10-28 16:29:27
【问题描述】:

我正在使用 for 循环遍历 HTML 表格。我只需要迭代第一行,而不是所有行。

我的代码 sn-p 在我的方法下面:

    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

HTML 是(我已经删除了一些列,否则粘贴时间太长):

    <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>

如果 count >= 1 行,它表示代码不可访问。 如果计数> = 1,我应该把它放在哪里? 或者只迭代 HTML 表的第一行的正确方法是什么?

我试图使用计数来跟踪 for 循环中的行。

【问题讨论】:

    标签: python python-2.7 selenium selenium-webdriver


    【解决方案1】:

    只需使用find_element 而不是find_elements

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

    【讨论】:

    • 啊,是的,这只会找到第一个元素,而不是找到所有元素并将其附加到列表中。伟大的。谢谢。
    • @RiazLadhani 是的,附加提示:您有多个 row.find_elements(By.TAG_NAME, "td") 调用 - 请记住,像这样的每个 selenium 调用都是通过 JSON 有线协议的 HTTP 请求,并且需要时间。因此,为了提高性能,您可以在变量中记住结果:cells = row.find_elements(By.TAG_NAME, "td") 并重复使用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    相关资源
    最近更新 更多