python - Selenium 表搜索未返回正确的文本
问题描述
我目前正在学习如何在 python 中使用 selenium,我有一张表,我想检索元素,但目前遇到了一些麻烦。
<table class="table" id="SearchTable">
<thead>..</thead>
<tfoot>..</tfoot>
<tbody>
<tr>
<td class="icon">..</td>
<td class="title">
<a class="qtooltip">
<b>I want to get the text here</b>
</a>
</td>
</tr>
<tr>
<td class="icon">..</td>
<td class="title">
<a class="qtooltip">
<b>I want to get the text here as well</b>
</a>
</td>
</tr>
</table>
在这个表中,我想访问粗体标记中的文本,但我的程序没有返回正确的 tr 数,事实上我什至不确定它是否搜索了正确的东西。
我从结尾文本回溯了我的问题,发现错误从带有注释的行开始出现。(我认为之后的代码也是错误的,但我专注于首先获得正确的表格行)
我的代码是:
search_table = driver.find_element_by_id("SearchTable")
search_table_body = search_table.find_element(By.TAG_NAME, "tbody")
trs = search_table_body.find_elements(By.TAG_NAME, "tr")
print(trs) # this does not return correct number of tr)
for tr in trs:
tds = tr.find_elements(By.TAG_NAME, "td")
for td in tds:
href = td.find_element_by_class_name("qtooltip")
print(href.get_attribute("innerHtml"))
我应该得到正确的 tr 计数,所以我可以返回锚标记中的文本,但我被卡住了。任何帮助表示赞赏。谢谢!
解决方案
推荐阅读
- java - 导入 gradle 时出现 Eclipse/Minecraft 错误
- python - 是否可以从 python 编写一个 pine 脚本并在 TradingView 中启动它?
- swift - 如何始终添加到 NSCollectionView 的末尾?
- java - 比较两个 ArrayList
- node.js - Sequelize JSON 字段只更新一个字段
- reactjs - 如何使用 Firestore 中的对象数组更新字段中的特定对象?
- javascript - Sequelize - 不要在 BelongsToSetAssociationMixin 上自动保存
- excel - 自动删除 Excel 中的特定行
- django - AWS Elastic Beanstalk 为 Django 应用程序配置允许的主机
- python - Pandas Python:将多标头写入excel