Chrome
https://linuxize.com/post/how-to-install-google-chrome-web-browser-on-centos-7/
cat > /etc/yum.repos.d/google-chrome.repo <<EOF
[google-chrome]
name=google-chrome
baseurl=http://dl.google.com/linux/chrome/rpm/stable/x86_64
enabled=1
gpgcheck=1
gpgkey=https://dl.google.com/linux/linux_signing_key.pub
EOF
yum install -y google-chrome-stable
google-chrome --version
# Google Chrome 105.0.5195.125
ChromeDriver
https://chromedriver.chromium.org/downloads
- 下载对应版本的驱动程序
wget https://chromedriver.storage.googleapis.com/105.0.5195.52/chromedriver_linux64.zip
# 解压至PATH路径下,否则后面使用selenium时需手动指定
unzip chromedriver_linux64.zip
mv chromedriver /usr/local/bin/
Selenium
python3 -m pip install selenium
headless
模式
from selenium import webdriver
# service
chromedriver = './chromedriver'
service = webdriver.chrome.service.Service(chromedriver)
# options
options = webdriver.ChromeOptions()
options.add_argument('--headless')
# init browser
browser = webdriver.Chrome(service=service, options=options)
# get url
browser.get('https://www.selenium.dev')
# find element
e = browser.find_element(by='id', value='td-cover-block-0')
print(e.text)
# screenshot
browser.save_screenshot('screenshot.png')
# quit
browser.quit()