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

如何修复Python爬虫中的AttributeError: 'list' object has no attribute 'find_elements'错误

问题解决:AttributeError: 'list' object has no attribute 'find_elements'

错误原因

你用browser.find_elements(By.CLASS_NAME, 'jsx-3285079627.category-landing-list')获取到的category_root是元素列表,Python列表没有find_elements方法,直接调用该方法就会触发报错。

修正方案

根据页面实际情况选择以下两种方案之一:

方案1:页面只有一个目标根元素

如果页面中jsx-3285079627.category-landing-list对应的元素只有一个,把find_elements改成单数的find_element,获取单个元素后再查找子元素:

# 补充必要的导入语句(你代码中遗漏了)
from selenium.webdriver.common.by import By
from selenium import webdriver

# 初始化浏览器
browser = webdriver.Chrome('C:/Users/chromedriver_win32/chromedriver.exe')
url = "https://website.com/"
browser.get(url)

# 改用find_element获取单个根元素
category_root = browser.find_element(By.CLASS_NAME, 'jsx-3285079627.category-landing-list')
# 注意:By.TAG_NAME用于匹配HTML标签,你传入的是类名,需改用By.CLASS_NAME
category_column = category_root.find_elements(By.CLASS_NAME, 'jsx-3285079627.category-landing-column')

list_of_category = []
list_of_href = []

for i in category_column:
    get_category_name = i.find_elements(By.TAG_NAME,"a")
    for cat in get_category_name:
        list_of_category.append(cat.text)
        list_of_href.append(cat.get_attribute('href'))
        
print(list_of_category)
print(list_of_href)

# 记得关闭浏览器释放资源
browser.quit()

方案2:页面有多个目标根元素

如果页面中存在多个jsx-3285079627.category-landing-list元素,需要遍历列表中的每个元素,逐个查找子元素:

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

browser = webdriver.Chrome('C:/Users/chromedriver_win32/chromedriver.exe')
url = "https://website.com/"
browser.get(url)

# 获取所有根元素的列表
category_root_list = browser.find_elements(By.CLASS_NAME, 'jsx-3285079627.category-landing-list')

list_of_category = []
list_of_href = []

# 遍历每个根元素,逐个处理子元素
for category_root in category_root_list:
    category_column = category_root.find_elements(By.CLASS_NAME, 'jsx-3285079627.category-landing-column')
    for i in category_column:
        get_category_name = i.find_elements(By.TAG_NAME,"a")
        for cat in get_category_name:
            list_of_category.append(cat.text)
            list_of_href.append(cat.get_attribute('href'))
        
print(list_of_category)
print(list_of_href)

browser.quit()

额外注意点

  • 必须补充from selenium.webdriver.common.by import By和from selenium import webdriver,否则代码会因找不到对应类而报错。
  • 原代码中用By.TAG_NAME匹配类名是错误的,By.TAG_NAME仅用于查找div、a这类HTML标签,匹配类名要使用By.CLASS_NAME。
  • 建议添加元素等待逻辑,避免页面未加载完成就查找元素导致的NoSuchElementException,示例:
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    
    # 等待根元素加载完成,最长等待10秒
    category_root = WebDriverWait(browser, 10).until(
        EC.presence_of_element_located((By.CLASS_NAME, 'jsx-3285079627.category-landing-list'))
    )
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.28 13:44:57