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

Selenium报错:'list'对象无'find_elements'属性,求原因与解决方法

问题分析与解决:Selenium 'list'对象无'find_elements'属性错误

错误原因

  1. find_elements返回的是列表:你用driver.find_elements(By.CLASS_NAME, "jobs-search__results-list")得到的jobs_block是WebElement对象的列表,列表本身没有find_elements方法——这个方法只能被单个WebElement对象调用。
  2. 类名参数格式错误:用By.CLASS_NAME时,参数不能带.,.是CSS选择器的语法,直接写类名base-card即可。

修正后的代码

import selenium
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
from selenium.webdriver.common.keys import Keys

driver = webdriver.Chrome()
driver.get("https://www.linkedin.com/jobs/search?position=1&pageNum=0")
time.sleep(1)

# 输入搜索关键词并回车
inputElement = driver.find_element("xpath", '/html/body/div[1]/header/nav/section/section[2]/form/section[1]/input')
inputElement.send_keys('Product Designer at Apple')
inputElement.send_keys(Keys.ENTER)

time.sleep(1)

# 改用find_element(单数)获取单个结果列表容器
jobs_block = driver.find_element(By.CLASS_NAME, "jobs-search__results-list")
# 修正类名参数,去掉.,并调用单个元素的find_elements方法
jobs_list = jobs_block.find_elements(By.CLASS_NAME, "base-card")

links = []
for job in jobs_list:
    # 改用Selenium 4推荐的语法
    all_links = job.find_elements(By.TAG_NAME, 'a')
    for a in all_links:
        href = a.get_attribute('href')
        if href and href.startswith("https://www.linkedin.com/jobs/view") and href not in links:
            links.append(href)

print(links)
driver.quit()

额外说明

如果页面上确实存在多个jobs-search__results-list元素,你需要遍历jobs_block列表,对每个元素单独调用find_elements方法;另外尽量避免硬编码长XPath,容易因页面结构变动失效,建议换成更稳定的定位方式(比如By.CSS_SELECTOR)。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.02 06:20:27