使用python循环遍历表行并在selenium中打印文本



我有一个HTML表:

<div class="report-data">
<table>
<thead>
<tr>
<td></td>
<td>All</td>
<td>Long</td>
<td>Short</td>
</tr>
</thead>
<tbody>
<tr>
<td>Net Profit</td>
<td>
<div>3644.65</div>
<div><span class="additional_percent_value">3.64&nbsp;%</span></div>
</td>
<td>
<div>3713.90</div>
<div><span class="additional_percent_value">3.71&nbsp;%</span></div>
</td>
<td>
<div><span class="neg">69.25</span></div>
<div><span class="additional_percent_value"><span class="neg">0.07&nbsp;%</span></span>
</div>
</td>
</tr>
<tr>
<td>Net Profit</td>
<td>
<div>3644.65</div>
<div><span class="additional_percent_value">3.64&nbsp;%</span></div>
</td>
<td>
<div>3713.90</div>
<div><span class="additional_percent_value">3.71&nbsp;%</span></div>
</td>
<td>
<div><span class="neg">69.25</span></div>
<div><span class="additional_percent_value"><span class="neg">0.07&nbsp;%</span></span>
</div>
</td>
</tr>
</tbody>
</table>
</div>

现在我想打印每行的所有td[1]值,所以我的输出应该是:

净利润

净利润

所以我执行了以下代码:

for dt in driver.find_element_by_xpath("//div[@class='report-data']/following-sibling::table/tbody/tr"):
text_label = dt.find_element_by_xpath(".//td").text
print(text_label)

但它抛出错误:

selenium.com.mon.exceptions.NoSuchElementException:消息:没有这样的元素:找不到元素:{"方法":"xpath","选择器":"//div[@class='report-data']/以下同级:::table/tbody/tr"}

我相信你差不多到了。试试这个:

content = driver.find_elements_by_xpath("//div[@class='report-data']/table/tbody/tr")
for dt in content:
text_label = dt.find_element_by_xpath("./td").text
print(text_label)

相关内容

  • 没有找到相关文章

最新更新