You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Selenium爬取网站时无法获取不可见span元素文本是什么原因?

问题原因

  • Selenium的text方法只能获取页面中可见元素的文本内容,你定位的a-icon-alt类span属于无障碍隐藏元素,前端会通过CSS设置其不可见(比如display: none、position: absolute偏移到可视区域外、visibility: hidden等),因此调用text会返回空值。
  • 你截图里的这个span是网站做无障碍适配用的,专门给视障用户的屏幕读屏软件识别评分内容,不会在普通用户的浏览页面中展示,和你要找的悬浮tooltip确实没有关联。

解决方法

1. 获取隐藏span的内容

把.text换成.get_attribute('textContent')即可,修改后代码如下:

bewertung = driver.find_elements_by_xpath('//span[@class="a-icon-alt"]')
for item in bewertung:
    print(item.get_attribute('textContent'))

2. 获取悬浮tooltip的内容

你需要的悬浮提示框需要先触发悬浮动作,等元素渲染后再提取内容,示例代码如下:

from selenium.webdriver.common.action_chains import ActionChains

# 定位触发tooltip的目标元素
trigger_ele = driver.find_element_by_xpath('替换为触发悬浮的元素xpath')
# 执行鼠标悬浮动作
ActionChains(driver).move_to_element(trigger_ele).perform()
# 等待tooltip渲染后,定位tooltip元素提取文本
tooltip_ele = driver.find_element_by_xpath('替换为tooltip对应的元素xpath')
print(tooltip_ele.text)

内容的提问来源于stack exchange,提问作者Gibby_Gibbson

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.03 16:15:01