我有一个包含一些行和列的表。我想循环遍历表并找到第81行和第82行以及列索引4(姓氏列) 我想检查第4行第81行和第82行的值。 数据是固定的,因此识别第81行和第82行是为了我的测试目的。
我已经开始使用一些代码来获取表并遍历行。 我如何直接进入第81行和第4列?
我的代码段是:
def is_surname_Campbell_and_CAMPBELL_together(self): # is the surname "Campbell" and "CAMPBELL" together
try:
table_id = WebDriverWait(self.driver, 20).until(
EC.presence_of_element_located((By.ID, 'data_configuration_view_preview_dg_main_body')))
rows = table_id.find_elements(By.TAG_NAME, "tr")
for row in rows:
# Get the columns
col_sname = row.find_elements(By.TAG_NAME, "td")[4] # This is the SNAME column
print "col_sname.text = "
if col_sname.text == "Campbell":
return True
return False
except NoSuchElementException, e:
print "Element not found "
print e
self.save_screenshot("is_surname_Campbell_and_CAMPBELL_together")
return False
HTML片段(一小部分,否则粘贴时间太长)
<table id="data_configuration_view_preview_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-756" style="outline-style:none;" tabindex="0">
<span class="" title="f1/48" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">f1/48</span>
</div>
</td>
<td class="GJPPK2LBIM GJPPK2LBKM GJPPK2LBBN">
<div __gwt_cell="cell-gwt-uid-757" 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-758" style="outline-style:none;">
<span class="" title="Keith" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">Keith</span>
</div>
</td>
<td class="GJPPK2LBIM GJPPK2LBKM GJPPK2LBBN">
<div __gwt_cell="cell-gwt-uid-759" style="outline-style:none;">
<span class="" title="Campbell" style="background-color:yellow;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">Campbell</span>
</div>
</td>
<td class="GJPPK2LBIM GJPPK2LBKM GJPPK2LBBN">
<div __gwt_cell="cell-gwt-uid-760" 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">
<td class="GJPPK2LBIM GJPPK2LBKM GJPPK2LBBN">
<td class="GJPPK2LBIM GJPPK2LBKM GJPPK2LBBN">
</tbody>
</table>
谢谢, 里亚兹
答案 0 :(得分:2)
通常情况下,81行4列中单元格的值可以定义为
driver.find_element_by_xpath('//tr[81]/td[4]').text
PS。 HTML
元素索引从[1]元素开始,但不是从[{1}}