我有一个包含一些列和行的HTML表。我试图找到第5列的文本和第3列的文本。 我想从第5栏找到的文字是101 THE BatCave | GOTHAM CITY |和第3栏中的文字14
首先,我首先使用以下XAPTH从第3列中找到文本14:
//table[@id="reporting_view_report_dg_main_body"]//tr//td[3]/div/span[@title="14"]
但我不知道如何从第5列获取文本。 我试过了:
//table[@id="reporting_view_report_dg_main_body"]//tr//td[3]/div/span[@title="14"] and td[5]//span[contains(text(), "101 THE BatCave|GOTHAM CITY|"])
//table[@id="reporting_view_report_dg_main_body"]//tr//td[3]/div/span[@title="14"]/following::td[5]
HTML代码段为:
<table id="reporting_view_report_dg_main_body" cellspacing="0" style="table-layout: fixed; width: 100%; margin-bottom: 17px;">
<colgroup>
<tbody>
<tr class="GFNQNVHJM" __gwt_subrow="0" __gwt_row="0" />
<tr class="GFNQNVHIN" __gwt_subrow="0" __gwt_row="1"/>
<tr class="GFNQNVHJM" __gwt_subrow="0" __gwt_row="2"/>
<tr class="GFNQNVHJM GFNQNVHAN" __gwt_subrow="0" __gwt_row="12"/>
<td class="GFNQNVHIM GFNQNVHKM GFNQNVHLM GFNQNVHBN"/>
<td class="GFNQNVHIM GFNQNVHKM GFNQNVHBN"/>
<td class="GFNQNVHIM GFNQNVHKM GFNQNVHBN"/>
<div __gwt_cell="cell-gwt-uid-319" style="outline-style:none;"/>
<span title="14" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">14</span>
</div>
</td>
<td class="GFNQNVHIM GFNQNVHKM GFNQNVHBN">
<td class="GFNQNVHIM GFNQNVHKM GFNQNVHBN">
<div __gwt_cell="cell-gwt-uid-321" style="outline-style:none;">
<span title="101 BatCave|GOTHAM CITY|" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">101 THE BatCave|GOTHAM CITY|</span>
</div>
</td>
<td class="GFNQNVHIM GFNQNVHKM GFNQNVHBN">
<td class="GFNQNVHIM GFNQNVHKM GFNQNVHBN"/>
<td class="GFNQNVHIM GFNQNVHKM GFNQNVHBN"/>
<td class="GFNQNVHIM GFNQNVHKM GFNQNVHBN"/>
<td class="GFNQNVHIM GFNQNVHKM GFNQNVHBN"/>
<td class="GFNQNVHIM GFNQNVHKM GFNQNVHFN GFNQNVHBN"/>
</tr>
<tr class="GFNQNVHIN" __gwt_subrow="0" __gwt_row="13"/>
<tr class="GFNQNVHJM" __gwt_subrow="0" __gwt_row="14"/>
</tbody>
谢谢Riaz
答案 0 :(得分:2)
在您的特定情况下不确定此方法的可靠性,但考虑到提供的内容,您可以通过span
属性获取title
元素来获取这两个元素:
elements = driver.find_elements_by_xpath("//table[@id='reporting_view_report_dg_main_body']//span[@title]")
for element in elements:
print(element.text)
答案 1 :(得分:1)
使用xpath将两个列组合在一起并获取它们的文本组合是不可能的。您只能在列表中找到这两列,然后通过循环将它们的文本组合如下:
combineText = ""
columns = driver.find_element_by_id("reporting_view_report_dg_main_body").find_elements_by_xpath(".//span[@title = '14'] | .//span[@title = '101 BatCave|GOTHAM CITY|']")
for column in columns:
combineText += column.text
print(combineText)