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

Selenium报错:'WebDriver'对象无'find_elements_by_class_name'属性

解决Selenium中'WebDriver' object has no attribute 'find_elements_by_class_name'报错

问题原因

你遇到的报错是因为新版Selenium(4.x及以上版本)已经移除了find_elements_by_class_name、find_element_by_id这类旧的元素定位方法,这些方法被标记为废弃并从WebDriver对象中删除了。

修正方案

改用find_elements()方法,配合By类来指定定位策略,同时修正导入语句的错误:

修正后的完整代码

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

url = "https://www.justdial.com/Pune/Stationery-Shops/nct-10453443"
driver = webdriver.Chrome()
driver.get(url)

# 改用新的定位写法
data = driver.find_elements(By.CLASS_NAME, 'resultbox_textbox')
print(data)

关键改动说明

  • 导入By类:用于明确指定元素定位的策略(如CLASS_NAME、ID、XPATH等)
  • 替换find_elements_by_class_name('类名')为find_elements(By.CLASS_NAME, '类名')
  • 修正导入语句:原代码中from selenium import WebDriver是错误的,正确应为from selenium import webdriver

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.26 23:48:09