alpine镜像运行Python Selenium报错 No such file or directory: '/root/.cache/selenium/xxx/chromedriver'

Xy718 613 2023-07-05

代码:

opt = webdriver.ChromeOptions()  # 创建浏览
opt.add_argument("--headless")  # 无窗口模式
driver = webdriver.Chrome(options=opt)  # 创建浏览器对象

很多时候RPA类的工作会使用Python Selenium来完成,所以会用pip install selenium在Alpine镜像中安装完成基础镜像

Selenium需要chromedriver来运行脚本,但是alpine镜像不自带,所以会报错
image-1688555665454

使用apk add -U --no-cache --virtual=build-dependencies chromium-chromedriver来安装驱动
image-1688560383593
完成后驱动就会出现在/usr/bin/chromedriver
image-1688561380449
修改代码,使用自定义的driver path

# 设置 ChromeDriver 的路径
chrome_driver_path = "/usr/bin/chromedriver"
opt = webdriver.ChromeOptions()  # 创建浏览
opt.add_argument("--headless")  # 无窗口模式
driver = webdriver.Chrome(options=opt, executable_path=chrome_driver_path)  # 创建浏览器对象

即可

然后接下来就是重新commit你的docker镜像~


冶心·练体·得技