在Docker镜像中安装ChromeDriver

最近由于自动化测试需要,要在docker里面安装chromedriver。Docker是基于python3.8.10的,安装也比较简单。

修改docker的源

镜像里的源还是ununtu的,为了速度,先修改为aliyun的源

cat <<EOF>/etc/apt/sources.list
deb http://mirrors.aliyun.com/debian/ buster main non-free contrib
deb-src http://mirrors.aliyun.com/debian/ buster main non-free contrib
deb http://mirrors.aliyun.com/debian-security buster/updates main
deb-src http://mirrors.aliyun.com/debian-security buster/updates main
deb http://mirrors.aliyun.com/debian/ buster-updates main non-free contrib
deb-src http://mirrors.aliyun.com/debian/ buster-updates main non-free contrib
deb http://mirrors.aliyun.com/debian/ buster-backports main non-free contrib
deb-src http://mirrors.aliyun.com/debian/ buster-backports main non-free contrib
EOF

下载chrome

wget -q https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb

安装chrome

apt-get install ./google-chrome-stable_current_amd64.deb -y

如果安装chrome出错,可以试一下以下命令安装:

apt-get --fix-broken install ./google-chrome-stable_current_amd64.deb 

下载合适的chromedriver

wget -O /tmp/chromedriver.zip http://chromedriver.storage.googleapis.com/`curl -sS chromedriver.storage.googleapis.com/LATEST_RELEASE`/chromedriver_linux64.zip

安装selenium的pip包

pip install selenium

测试代码

from selenium import webdriver

options = webdriver.ChromeOptions()
options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')

browser = webdriver.Chrome(chrome_options=options)

url = "https://www.baidu.com"

browser.get(url)
browser.save_screenshot("snapshot.png")
browser.quit()

可以看到当前路径下有“snapshot.png"图片,打开后,也是百度首页的截图。大工告成!

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值