Python+Selenium脚本运行报EC、array、Image未定义错误如何解决
错误原因及解决方案
1. NameError: name 'EC' is not defined
你提供的代码里已经写明from selenium.webdriver.support import expected_conditions as EC导入语句,仍报错的可能原因:
- 实际运行的脚本和你贴出的代码不是同一份,导入语句被注释、删除或存在拼写错误
- 本地selenium包版本兼容问题或文件损坏
解决步骤: - 核对运行脚本头部是否存在对应导入语句,确认大小写、拼写完全匹配
- 重装稳定版selenium:
pip uninstall selenium -y && pip install selenium==4.8.3
2. NameError: name 'array' is not defined
同理代码中已写import array仍报错,排查方向:
- 确认运行脚本的导入部分没有注释、删除该语句
- 确认依赖安装的Python环境和脚本运行环境一致,执行
pip install array补装
3. NameError: name 'Image' is not defined
该错误和Pillow版本无关,是缺少显式导入导致的,同时你使用的旧版API已被废弃:
- Pillow的Image类需要单独导入才能调用,你当前的导入列表中没有对应语句
- Pillow 9.0及以上版本已经移除
fromstring方法,替换为frombytes
解决步骤:
- 在脚本导入部分新增一行:
from PIL import Image
- 将
getClippedImage函数中的代码:
im = Image.fromstring("RGBA", (w, h), data_bytes)
修改为:
im = Image.frombytes("RGBA", (w, h), data_bytes)
附加兼容修复
你代码内JS部分的单引号使用了中文符号‘2d‘,会导致JS执行报错,需要替换为英文单引号'2d'。
内容的提问来源于stack exchange,提问作者jay su
相关产品推荐
相关产品推荐

