我使用for循环迭代HTML表。我只需要迭代第一行,而不是所有行。
我的代码段低于我的方法:
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是(我删除了一些cols,否则粘贴时间太长):
<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> gt = = 1,则表示代码无法访问。 如果计数&gt; = 1,我该把它放在哪里? 或者只迭代HTML表的第一行的正确方法是什么?
我试图使用计数来跟踪for循环中的行。
答案 0 :(得分:3)
只需使用find_element
代替find_elements
:
first_row = table_id.find_element(By.TAG_NAME, "tr")