Selenium Python迭代html表错误IndexError:列表索引超出范围

时间:2016-03-08 17:25:23

标签: python-2.7 selenium selenium-webdriver

我在网页上有一个HTML表格,其中有很多行和几列。 我正在迭代表以检查第2列和第3列是否有一些数据。它不是空的。 Col2的值为1,col 3的值为Mrs | Sue | a | Female

我收到以下错误:

    Traceback (most recent call last):
  File "C:\Webdriver\ClearCore 501 Regression Test\ClearCore - Regression Test\TestCases\ReportingPage_TestCase_Regression.py", line 73, in test_a2_view_all_records_report
    self.assertTrue(reports_view_results_page.is_view_report_showing_results2(), "Problem with viewing report. Please see log for details")
  File "C:\Webdriver\ClearCore Regression Test\ClearCore - Regression Test\Pages\Reports\reports_view_results.py", line 108, in is_view_report_showing_results2
    col_usn = row.find_elements(By.TAG_NAME, "td")[2]  # This is the USN column
IndexError: list index out of range

迭代表的我的Selenium Python方法是:

 def is_view_report_showing_results2(self):
    try:
        #WebDriverWait(self.driver, 20).until(
        #    EC.presence_of_element_located((By.ID, 'reporting_view_report_dg_main_body')))
        table_id = WebDriverWait(self.driver, 20).until(
            EC.presence_of_element_located((By.ID, 'reporting_view_report_dg_main_body')))
        rows = table_id.find_elements(By.TAG_NAME, "tr")
        for row in rows:
            # Get the columns
            col_usn = row.find_elements(By.TAG_NAME, "td")[2]  # This is the USN column
            col_2 = row.find_elements(By.TAG_NAME, "td")[3]  # This is the 2nd, column Source Fields
            print "col_usn.text = "
            print col_usn.text
            print col_2.text
            if (col_usn.text is not None) and (col_2.text is not None):
                return True
        return False
    except NoSuchElementException, e:
        print "Element not found "
        print e
        self.save_screenshot("is_view_report_showing_results2")
        return False

HTML示例是:

    <table id="reporting_view_report_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-3017" style="outline-style:none;" tabindex="0">
                <input type="radio" name="report"/>
            </div>
        </td>
        <td class="GJPPK2LBIM GJPPK2LBKM GJPPK2LBBN">
            <div __gwt_cell="cell-gwt-uid-3018" 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="GJPPK2LBIM GJPPK2LBKM GJPPK2LBBN">
            <div __gwt_cell="cell-gwt-uid-3019" style="outline-style:none;">
                <span class="" title="Mrs|Sue|a|Female"
                      style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">Mrs|Sue|MacDonald|Female</span>
            </div>
        </td>
        <td class="GJPPK2LBIM GJPPK2LBKM GJPPK2LBBN">
            <div __gwt_cell="cell-gwt-uid-3020" style="outline-style:none;">
                <span class="" title="Mrs Sue a"
                      style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">Mrs Sue Macdonald</span>
            </div>
        </td>
        <td class="GJPPK2LBIM GJPPK2LBKM GJPPK2LBBN">
            <div __gwt_cell="cell-gwt-uid-3021" style="outline-style:none;">
                <span class="" title="00 RISE|BRIERLEY HILL|WEST MID||DY5 1LS"
                      style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">25 RISE|BRIERLEY HILL|WEST MID||DY5 1LS</span>
            </div>
        </td>
        <td class="GJPPK2LBIM GJPPK2LBKM GJPPK2LBBN">
            <div __gwt_cell="cell-gwt-uid-3022" style="outline-style:none;">
                <span class="" title="00 Rise Brierley Hill DY5 1LS"
                      style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">some text</span>
            </div>
        </td>
        <td class="GJPPK2LBIM GJPPK2LBKM GJPPK2LBBN">
            <div __gwt_cell="cell-gwt-uid-3023" style="outline-style:none;">
                <span class="" title="23/4/1958 00:00:00"
                      style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">23/4/1958 00:00:00</span>
            </div>
        </td>
        <td class="GJPPK2LBIM GJPPK2LBKM GJPPK2LBBN">
            <div __gwt_cell="cell-gwt-uid-3024" style="outline-style:none;">
                <span class="" title="01702 20000"
                      style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">01702 00000</span>
            </div>
        </td>
        <td class="GJPPK2LBIM GJPPK2LBKM GJPPK2LBBN">
            <div __gwt_cell="cell-gwt-uid-3025" style="outline-style:none;">
                <span class="" title="07966700400|Mrs"
                      style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">07966790000|Mrs</span>
            </div>
        </td>
        <td class="GJPPK2LBIM GJPPK2LBKM GJPPK2LBBN">
            <div __gwt_cell="cell-gwt-uid-3026" 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-3027" 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 GJPPK2LBFN GJPPK2LBBN">
            <div __gwt_cell="cell-gwt-uid-3028" 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>
    </tr>
    <tr class="GJPPK2LBIN" __gwt_subrow="0" __gwt_row="1">
        <td class="GJPPK2LBIM GJPPK2LBJN GJPPK2LBLM">
            <div __gwt_cell="cell-gwt-uid-3016" style="outline-style:none;">2</div>
        </td>
        <td class="GJPPK2LBIM GJPPK2LBJN">
            <div __gwt_cell="cell-gwt-uid-3017" style="outline-style:none;">
                <input type="radio" name="report"/>
            </div>
        </td>
        <td class="GJPPK2LBIM GJPPK2LBJN">
            <div __gwt_cell="cell-gwt-uid-3018" style="outline-style:none;">
                <span class="" title="2"
                      style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">2</span>
            </div>
        </td>
        <td class="GJPPK2LBIM GJPPK2LBJN">
            <div __gwt_cell="cell-gwt-uid-3019" style="outline-style:none;">
                <span class="" title="Mr|J|Bell|Male"
                      style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">Mr|J|Bell|Male</span>
            </div>
        </td>
        <td class="GJPPK2LBIM GJPPK2LBJN">
            <div __gwt_cell="cell-gwt-uid-3020" style="outline-style:none;">
                <span class="" title="Mr J Bell"
                      style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">Mr J Bell</span>
            </div>
        </td>
        <td class="GJPPK2LBIM GJPPK2LBJN">
            <div __gwt_cell="cell-gwt-uid-3021" style="outline-style:none;">
                <span class="" title="address1|1 ALIE STREET|LONDON||E1 8DE"
                      style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">an address</span>
            </div>
        </td>
        <td class="GJPPK2LBIM GJPPK2LBJN">
            <div __gwt_cell="cell-gwt-uid-3022" style="outline-style:none;">
                <span class="" title="address1 Street London E1 8DE"
                      style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">1 a Street London E1 8DE</span>
            </div>
        </td>
        <td class="GJPPK2LBIM GJPPK2LBJN">
            <div __gwt_cell="cell-gwt-uid-3023" style="outline-style:none;">
                <span class="" title="5/5/1958 00:00:00"
                      style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">5/5/1958 00:00:00</span>
            </div>
        </td>
        <td class="GJPPK2LBIM GJPPK2LBJN">
            <div __gwt_cell="cell-gwt-uid-3024" style="outline-style:none;">
                <span class="" title="01698500000"
                      style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">01698500000</span>
            </div>
        </td>
        <td class="GJPPK2LBIM GJPPK2LBJN">
            <div __gwt_cell="cell-gwt-uid-3025" style="outline-style:none;">
                <span class="" title="07966790000|Mr"
                      style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">07966790000|Mr</span>
            </div>
        </td>
        <td class="GJPPK2LBIM GJPPK2LBJN">
            <div __gwt_cell="cell-gwt-uid-3026" 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 GJPPK2LBJN">
            <div __gwt_cell="cell-gwt-uid-3027" 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 GJPPK2LBJN GJPPK2LBFN">
            <div __gwt_cell="cell-gwt-uid-3028" 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>
    </tr>
    <tr class="GJPPK2LBJM" __gwt_subrow="0" __gwt_row="2">
    <tr class="GJPPK2LBIN" __gwt_subrow="0" __gwt_row="3">
    </tbody>
</table>

如何在不使索引超出范围错误的情况下正确迭代表?

谢谢, 里亚兹

0 个答案:

没有答案