安装selenium模块
bash
pip3 install selenium
安装谷歌浏览器
bash
yum install https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm -y
安装chromedriver
1)运行下面命令查看浏览器版本
bash
google-chrome --version
# Google Chrome 123.0.6312.122
2) 根据谷歌浏览器版本下载对应的浏览器驱动版本
Chrome for Testing availability (这个页面里是高版本的驱动)
ChromeDriver - WebDriver for Chrome - Downloads (这个页面是低版本的驱动)
bash
wget https://storage.googleapis.com/chrome-for-testing-public/123.0.6312.122/linux64/chromedriver-linux64.zip
3)解压安装
bash
unzip chromedriver-linux64.zip
mv chromedriver-linux64/chromedriver /usr/bin/
chmod +x /usr/bin/chromedriver
chromedriver -version
代码中添加配置
bash
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
chrome_options.add_argument('--disable-extensions')
chrome_options.add_argument('--log-level=0')
chrome_options.add_argument('--remote-debugging-port=9222')
# GPU硬件加速
chrome_options.add_argument('---disable-gpu')
browser = webdriver.Chrome(options=chrome_options)