为了账号安全,请及时绑定邮箱和手机立即绑定

访问标签内的属性

访问标签内的属性

慕的地8271018 2023-07-11 13:43:57
我的目标是访问链接列表(位于"event?id=85042")。<tr class="gvrow"> == $0  <td>...</td>  <td>    <a href="event?id=85042"                                text                              </a>  </td>  ...这也会重复,所以我想获取位于 event?id=... 中的链接列表,我已经从这个问题Python Selenium - get href value尝试过这种方法,但它返回一个充满 None 的列表primary_links = driver.find_elements_by_class_name('gvrow')links = [elem.get_attribute('href') for elem in primary_links]print(links)输出:[None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None]期望的输出:['https://www.iccf.com/event?id=85042', 'https://www.iccf.com/event?id=79897', 'https://www.iccf.com/event?id=66745', ...]那我该怎么办呢?这是 html 的更好表示 
查看完整描述

1 回答

?
湖上湖

TA贡献2003条经验 获得超2个赞

通过以下代码行,您将获得一个包含该<td>元素和任何其他具有以下元素的列表class=gvrow

driver.find_elements_by_class_name('gvrow')

显然,<td>您返回的元素没有该href属性。

除了您的方法之外,您还可以使用另一种方法来获取链接:

a_elements = driver.find_elements_by_xpath("//tr[@class='gvrow']/td/a")
links = [a_element.get_attribute('href') for a_element in a_elements]


查看完整回答
反对 回复 2023-07-11
  • 1 回答
  • 0 关注
  • 59 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信