我有一个包含行和列的html表 在第6列中,单个td标记中有2个span标记。每个span标记都有一个值。 表格中的行,第6列中的值为4 ,第6列中的值为24 我希望将值4和24 输出第2列的文本为all_matches_filtered ,其中第4列的文本为Do_Name和Do_Name2 < / p>
在我的Selenium Python脚本中,我将验证文本all_matches_filtered和文本Do_Name和Do_Name2是否存在值4和24
我可以使用什么XPath来找到值4和24?
我已经开始使用Xpath,并为文本all_matches_filtered定位值4。我想要包含与值4
在同一行的文本Do_Name我的Xpath是:
//table[@id="reporting_reports_ct_fields_body"]//tr//td//div//span[contains(text(), "all_matches_filtered")]/following::td[6]//div//span[(text()="4")]
HTML片段是(我缩短了html,否则会很长):
<table id="reporting_reports_ct_fields_body" cellspacing="0" style="table-layout: fixed; width: 100%;">
<colgroup>
<tbody>
<tr class="GJPPK2LBFG GJPPK2LBMG" __gwt_subrow="0" __gwt_row="0">
<td class="GJPPK2LBEG GJPPK2LBGG GJPPK2LBHG GJPPK2LBNG">
<div __gwt_cell="cell-gwt-uid-2169" style="outline-style:none;" tabindex="0">
<input type="checkbox" tabindex="-1"/>
</div>
</td>
<td class="GJPPK2LBEG GJPPK2LBGG GJPPK2LBNG">
<div __gwt_cell="cell-gwt-uid-2170" style="outline-style:none;">
<span class="linkhover" title="all_matches_filtered"
style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;color:#00A;cursor:pointer;">all_matches_filtered</span>
</div>
</td>
<td class="GJPPK2LBEG GJPPK2LBGG GJPPK2LBNG">
<div __gwt_cell="cell-gwt-uid-2171" style="outline-style:none;">
<span class="" title="manual"
style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">manual</span>
</div>
</td>
<td class="GJPPK2LBEG GJPPK2LBGG GJPPK2LBNG">
<div __gwt_cell="cell-gwt-uid-2172" style="outline-style:none;">
<span class="linkhover block" title="Do_Name"
style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;color:#00A;cursor:pointer;">Do_Name</span>
<span class="linkhover block" title="Do_Name2"
style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;color:#00A;cursor:pointer;">Do_Name2</span>
</div>
</td>
<td class="GJPPK2LBEG GJPPK2LBGG GJPPK2LBNG">
<div __gwt_cell="cell-gwt-uid-2173" style="outline-style:none;">
<span class="" title="This is a matches report"
style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">This is a matches report</span>
</div>
</td>
<td class="GJPPK2LBEG GJPPK2LBGG GJPPK2LBNG">
<div __gwt_cell="cell-gwt-uid-2174" style="outline-style:none;">
<span class="" title="USN entities"
style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">USN entities</span>
</div>
</td>
<td class="GJPPK2LBEG GJPPK2LBGG GJPPK2LBNG">
<div __gwt_cell="cell-gwt-uid-2175" style="outline-style:none;">
<span title="16/03/2016 10:36:45"
style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">16/03/2016 10:36:45</span>
</div>
</td>
<td class="GJPPK2LBEG GJPPK2LBGG GJPPK2LBNG">
<div __gwt_cell="cell-gwt-uid-2176" style="outline-style:none;">
<span class="block" title="" style="">4</span>
<span class="block" title="" style="">24</span>
</div>
</td>
<td class="GJPPK2LBEG GJPPK2LBGG GJPPK2LBBH GJPPK2LBNG">
</tr>
<tr class="GJPPK2LBEH" __gwt_subrow="0" __gwt_row="1">
<tr class="GJPPK2LBFG" __gwt_subrow="0" __gwt_row="2">
<tr class="GJPPK2LBEH" __gwt_subrow="0" __gwt_row="3">
</tbody>
谢谢Riaz
答案 0 :(得分:0)
试试这段代码:
from selenium import webdriver
driver = webdriverFirefox() # driver = webdriverChrome()
driver.get('inser_page_URL_where_table_located')
table_data = driver.find_elements_by_xpath('//td[@class="GJPPK2LBEG GJPPK2LBGG GJPPK2LBNG"]')[-1]
values = []
for i in table_data.find_elements_by_tag_name('span'):
values.append(i.text)
print values
如果发生任何异常,请告诉我
答案 1 :(得分:0)
首先得到正确的行 out第2列的文本为all_matches_filtered,第4列的文本为Do_Name和Do_Name2
这将是:
/table[@id="reporting_reports_ct_fields_body"]//tr[
td[2]//span[contains(text(), "all_matches_filtered")] and
td[4]//span="Do_Name"
]
并在此处形成右栏(例如)
/td[8]//span
比之类的东西:
//table[@id="reporting_reports_ct_fields_body"]//tr[td[2]//span[contains(text(), "all_matches_filtered")] and td[4]//span="Do_Name"]/td[8]//span