ubuntu安装 selenium chrome chromedriver

在Ubuntu系统中,通过Python 3.6安装selenium、chrome浏览器、xvfb和对应版本的chromedriver。确保chromedriver与chrome版本匹配,避免版本不对应的问题。完成安装后,可用于自动化网页操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文环境是ubuntu python 3.6

1.安装selenium
pip install selenium
2.安装chrome浏览器
sudo apt-get install libxss1 libappindicator1 libindicator7
wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
sudo dpkg -i google-chrome*.deb
sudo apt-get install -f

此处注意一下chrome的版本,安装过程会打印,或者安装好之后运行命令查看也可以

3.安装xvfb
sudo apt-get install xvfb
4.安装chromedriver

首先安装

sudo apt-get install unzip

下载chromedriver需要注意版本对应,也就是2.45这里需要根据实际情况进行修改版https://chromedriver.storage.googleapis.com/index.html
具体对应关系可以参考(chromedriver版本号 chrome版本号):

chromedriver chrome
2.46 71-73
2.45 70-72
2.44 69-71
2.43 69-71
2.42 68-70
2.41 67-69
2.40 66-68
2.39 66-68
2.38 65-67
2.37 64-66
2.36 63-65
2.35 62-64
2.34 61-63
2.33 60-62
2.28 57+
2.25 54+
2.24 53+
2.22 51+
2.19 44+
2.15 42+

wget -N http://chromedriver.storage.googleapis.com/2.45/chromedriver_linux64.zip
unzip chromedriver_linux64.zip
chmod +x chromedriver
sudo mv -f chromedriver /usr/local/share/chromedriver

#如果之前已经有软链 可以执行删除
# rm -rf /usr/local/bin/chromedriver
sudo ln -s /usr/local/share/chromedriver /usr/local/bin/chromedriver
sudo ln -s /usr/local/share/chromedriver /usr/bin/chromedriver

至此安装结束,多数出现版本不对应,对照对应版本进行重新安装重试。

5.例子
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import os

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument('--disable-dev-shm-usage')
chromedriver = "/usr/bin/chromedriver"
os.environ["webdriver.chrome.driver"] = chromedriver
driver = webdriver.Chrome(chrome_options=chrome_options,executable_path=chromedriver)
driver.get("https://www.baidu.com")
print(driver.page_source)
driver.save_screenshot('screen.png')
driver.quit()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值