已亲测基于
CentOS Linux release 7.9.2009 (Core)
安装正常。
安装Python
参考文档:http://www.oyjz.cn/centos-python310-install.html
安装google-chrome
官方下载最新版
wget https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm
使用rpm安装会需要各种依赖,导致报错,推荐暴力安装,会自动安装所需依赖
yum localinstall google-chrome-stable_current_x86_64.rpm
查看版本验证
google-chrome --version
安装chromedriver
国内下载站http://npm.taobao.org/mirrors/chromedriver/获取chrome对应版本的chromedriver,版本一定需要匹配!
wget https://registry.npmmirror.com/-/binary/chromedriver/106.0.5249.61/chromedriver_linux64.zip
unzip chromedriver_linux64.zip
mv chromedriver /usr/bin/
查看版本验证
chromedriver --version
安装selenium
直接pip3安装
pip3 install selenium
验证
创建vi test.py文件
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
chrome_options = Options()
chrome_options.add_argument('--headless') # 无界面【Linux下必须要有,否则报错】
chrome_options.add_argument('--no-sandbox') # 解决DevToolsActivePort文件不存在报错问题【Linux下必须要有,否则报错】
chrome_options.add_argument('--disable-gpu') # 禁用GPU硬件加速。如果软件渲染器没有就位,则GPU进程将不会启动。
chrome_options.add_argument('--disable-dev-shm-usage')
chrome_options.add_argument('--window-size=1920,1080') # 设置当前窗口的宽度和高度
s = Service(r"chromedriver")
driver = webdriver.Chrome(service=s,options=chrome_options)
driver.get("http://www.oyjz.cn")
print(driver.page_source)
driver.quit()
验证成功则说明安装成功。
python3 test.py
完毕。