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

Selenium Python遍历容器元素提取文本失败问题排查

问题分析与解决:遍历quote盒子重复输出第一个名言的原因

嘿,这个问题我太熟了——你踩了Selenium XPath定位里一个非常常见的坑!

问题根源

你在遍历每个quote盒子时,用了each.find_element_by_xpath('//span'),这里的//span是绝对路径搜索,它会从整个HTML文档的根节点开始,查找所有匹配的<span>元素,所以每次都会返回页面上第一个出现的<span>(也就是第一个名言的内容),完全忽略了你当前遍历的each这个盒子范围。

解决方法

要在当前的quote盒子内部查找子元素,你需要使用相对路径——在XPath表达式前加上.,写成.//span。这个.代表当前节点,告诉Selenium:“就从这个quote盒子里找,别去整个文档里搜”。

更精准一点的话,每个quote盒子里其实有多个<span>(比如作者名字也在<span>里),所以最好定位到class为text的那个<span>,避免误匹配。修正后的代码如下:

from selenium import webdriver
from time import sleep

driver = webdriver.Chrome()
driver.get("http://quotes.toscrape.com/")
sleep(2)

all_boxes = driver.find_elements_by_xpath(r"//div[@class='quote']")
for each in all_boxes:
    # 用相对路径从当前盒子内定位名言的span
    quote_text = each.find_element_by_xpath('.//span[@class="text"]').text
    print(quote_text)

额外小提示

如果你用的是Selenium 4及以上版本,find_elements_by_xpath这类旧API已经被官方弃用了,推荐使用更规范的find_elements(By.XPATH, ...)写法,代码可以改成这样:

from selenium import webdriver
from selenium.webdriver.common.by import By
from time import sleep

driver = webdriver.Chrome()
driver.get("http://quotes.toscrape.com/")
sleep(2)

all_boxes = driver.find_elements(By.XPATH, "//div[@class='quote']")
for each in all_boxes:
    quote_text = each.find_element(By.XPATH, ".//span[@class='text']").text
    print(quote_text)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 06:17:35