我有一个包含4行和一些列的HTML表。我正在迭代行,第4列检查值" 87"不存在。
我收到列表索引超出范围错误。
我检查过HTML有一个标题表(下面的代码段)和一个表格结果是,主体(下面的代码段)
我的Selenium Python方法是:
def is_match_audit_code_displayed_for_the_filter_report_results(self, code):
# Params code: match audit code e.g. 86
try:
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_match_audit = row.find_elements(By.TAG_NAME, "td")[4] # This is the Match Audit column
print "col_match_audit.text = "
if (col_match_audit.text not in code):
return True
return False
except NoSuchElementException, e:
print "Element not found "
print e
self.save_screenshot("reports_match_filter_results_page_match_audit_code_element")
return False
完整错误是:
Traceback (most recent call last):
File "C:\Webdriver\ClearCore 501 Regression Test\ClearCore 501 - Regression Test\TestCases\ReportingPage_TestCase_Regression.py", line 382, in test_B5_view_matches_filtered_report_crm_escr_crm2_match_filters_Do_Name_check_match_audit_code_is_displayed
reports_view_results_page.is_match_audit_code_displayed_for_the_filter_report_results("86")
File "C:\Webdriver\ClearCore 501 Regression Test\ClearCore 501 - Regression Test\Pages\Reports\reports_view_results.py", line 143, in is_match_audit_code_displayed_for_the_filter_report_results
col_match_audit = row.find_elements(By.TAG_NAME, "td")[4] # This is the Match Audit column
IndexError: list index out of range
标题位于单独的表中。标题的HTML代码段为:
<table id="reporting_view_report_dg_main_header" cellspacing="0" style="table-layout: fixed; width: 100%;">
<thead>
<tr __gwt_header_row="0">
<th class="GJPPK2LBPM GJPPK2LBNM" __gwt_header="header-gwt-uid-5566" __gwt_column="column-gwt-uid-5565"
colspan="1"/>
<th class="GJPPK2LBPM" __gwt_header="header-gwt-uid-5568" __gwt_column="column-gwt-uid-5567" colspan="1"/>
<th class="GJPPK2LBPM" __gwt_header="header-gwt-uid-5570" __gwt_column="column-gwt-uid-5569" colspan="1">
<span class="" title=""
style="display: block; position: relative ;font-family: arial; color: rgb(50,50,50); cursor: pointer;">
<span class=""
style="display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; empty-cells: show; padding-right: 1px;">
<span class="" title="" style="">USN</span>
</span>
<span style="display: block; right: 0px; bottom: 0px; position: absolute; width: 10px; cursor: col-resize;"/>
</span>
</th>
<th class="GJPPK2LBPM" __gwt_header="header-gwt-uid-5572" __gwt_column="column-gwt-uid-5571" colspan="1">
<span class="" title=""
style="display: block; position: relative ;font-family: arial; color: rgb(50,50,50); cursor: pointer;">
<span class=""
style="display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; empty-cells: show; padding-right: 1px;">
<span class="" title="" style="">MATCH_AUDIT</span>
</span>
<span style="display: block; right: 0px; bottom: 0px; position: absolute; width: 10px; cursor: col-resize;"/>
</span>
</th>
<th class="GJPPK2LBPM" __gwt_header="header-gwt-uid-5574" __gwt_column="column-gwt-uid-5573" colspan="1">
<span class="" title=""
style="display: block; position: relative ;font-family: arial; color: rgb(50,50,50); cursor: pointer;">
<span class=""
style="display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; empty-cells: show; padding-right: 1px;">
<span class="" title="" style="">DOB_SOURCE_FIELDS</span>
</span>
<span style="display: block; right: 0px; bottom: 0px; position: absolute; width: 10px; cursor: col-resize;"/>
</span>
</th>
<th class="GJPPK2LBPM" __gwt_header="header-gwt-uid-5576" __gwt_column="column-gwt-uid-5575" colspan="1">
<th class="GJPPK2LBPM" __gwt_header="header-gwt-uid-5578" __gwt_column="column-gwt-uid-5577" colspan="1">
<th class="GJPPK2LBPM" __gwt_header="header-gwt-uid-5580" __gwt_column="column-gwt-uid-5579" colspan="1">
<th class="GJPPK2LBPM" __gwt_header="header-gwt-uid-5582" __gwt_column="column-gwt-uid-5581" colspan="1">
<th class="GJPPK2LBPM" __gwt_header="header-gwt-uid-5584" __gwt_column="column-gwt-uid-5583" colspan="1">
<th class="GJPPK2LBPM" __gwt_header="header-gwt-uid-5586" __gwt_column="column-gwt-uid-5585" colspan="1">
<th class="GJPPK2LBPM GJPPK2LBHN" __gwt_header="header-gwt-uid-5588" __gwt_column="column-gwt-uid-5587"
colspan="1"/>
</tr>
</thead>
<colgroup>
</table>
表格结果,主体在一个单独的表中。它的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">
<div __gwt_cell="cell-gwt-uid-5611" style="outline-style:none;">1</div>
</td>
<td class="GJPPK2LBIM GJPPK2LBKM GJPPK2LBBN">
<div __gwt_cell="cell-gwt-uid-5612" style="outline-style:none;" tabindex="0">
<input type="radio" name="report"/>
</div>
</td>
<td class="GJPPK2LBIM GJPPK2LBKM GJPPK2LBBN">
<div __gwt_cell="cell-gwt-uid-5613" style="outline-style:none;">
<span class="" title="43"
style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">43</span>
</div>
</td>
<td class="GJPPK2LBIM GJPPK2LBKM GJPPK2LBBN">
<div __gwt_cell="cell-gwt-uid-5614" style="outline-style:none;">
<span title="Matched from: Name( -word/initial match -spelling score: 86) & Address(complete address) & DOB & Phone & ACVSEQ"
style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">Name[wi ,(86)]Address[I]DOB[]Phone[]ACVSEQ[]</span>
</div>
</td>
<td class="GJPPK2LBIM GJPPK2LBKM GJPPK2LBBN">
<div __gwt_cell="cell-gwt-uid-5615" style="outline-style:none;">
<span class="" title="15/2/1959 00:00:00"
style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">15/2/1959 00:00:00</span>
</div>
</td>
<td class="GJPPK2LBIM GJPPK2LBKM GJPPK2LBBN">
<div __gwt_cell="cell-gwt-uid-5616" style="outline-style:none;">
<span class="" title="Mr.|A S|Pierre|Male"
style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">Mr.|A S|Pier|Male</span>
</div>
</td>
<td class="GJPPK2LBIM GJPPK2LBKM GJPPK2LBBN">
<div __gwt_cell="cell-gwt-uid-5617" 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-5618" style="outline-style:none;">
<span class="" title="01226 710211"
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-5619" 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-5620" style="outline-style:none;">
<span class="" title="WINDWHISTLE|PINNER HILL|PINNER|MIDDLESEX|HA5 3XY"
style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">WHISTLE|PINNER HILL|PINNER|MIDDLESEX|</span>
</div>
</td>
<td class="GJPPK2LBIM GJPPK2LBKM GJPPK2LBBN">
<div __gwt_cell="cell-gwt-uid-5621" style="outline-style:none;">
<span class="" title="07966799994|Mr."
style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">07966700000|Mr.</span>
</div>
</td>
<td class="GJPPK2LBIM GJPPK2LBKM GJPPK2LBFN GJPPK2LBBN">
<div __gwt_cell="cell-gwt-uid-5622" 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">
<tr class="GJPPK2LBJM" __gwt_subrow="0" __gwt_row="2">
<tr class="GJPPK2LBIN" __gwt_subrow="0" __gwt_row="3">
</tbody>
</table>
如果我在Firebug Xpath检查程序中使用以下Xpath,则会突出显示第4列中的所有行。
//table[@id="reporting_view_report_dg_main_body"]//tr//td[4]//span
如何在迭代表的Selenium Python代码中解决此问题。 表格中有4行。
或者是否有另一种方法可以将第4列中的所有值都放到列表中然后遍历列表并检查值是否为&#34; 86&#34;不在这里。 使用find_elements_by_xpath
如下所示,不确定此代码的正确性:
elements = driver.find_elements_by_xpath('//table[@id="reporting_view_report_dg_main_body"]//tr//td[4]//span')
for i in elements
if elements.text[i] not in "86":
return True
else return False
谢谢Riaz