Verwendung von Selen unter Centos7
1. Installieren Sie Google auf Centos7
1.1 Laden Sie das Installationspaket herunter
wget https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm
1.2 Installieren Sie das Installationspaket
yum install -y google-chrome-stable_current_x86_64.rpm
1.3 Chrome-Version prüfen
# google-chrome --version
Google Chrome 114.0.5735.90
1.4 Installieren Sie einige Abhängigkeiten
yum install -y glibc.x86_64 libXcomposite.x86_64 libXcursor.x86_64 libXi.x86_64 libXtst.x86_64 libXrandr.x86_64 libXScrnSaver.x86_64 libXrandr.x86_64 alsa-lib.x86_64 pango.x86_64 atk.x86_64 libXss.x86_64 libXft.x86_64 libXinerama.x86_64
1.5 Laden Sie den Treiber entsprechend der Chrome-Version herunter
驱动下载地址:
https://registry.npmmirror.com/binary.html?path=chromedriver/
1.6 Entpacken Sie den Treiber
unzip chromedriver_linux64.zip
1.7 Probefahrt
# ./chromedriver
Starting ChromeDriver 2.36.540471
Only local connections are allowed.
2. Schreiben Sie Python, um zu testen, ob Chrome und der Treiber normal ausgeführt werden können
2.1 Testcode test_google.py
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
# 创建 ChromeOptions 实例,启用无头模式
chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
# 指定 ChromeDriver 的路径(根据实际情况修改)
chrome_driver_path = '/usr/path/chromedriver'
# 创建 Chrome WebDriver 实例
driver = webdriver.Chrome(executable_path=chrome_driver_path, options=chrome_options)
# 打开网页
driver.get('https://www.google.com')
# 输出网页标题
print('Page title:', driver.title)
# 关闭浏览器
driver.quit()
2.2 Ergebnisse einer erfolgreichen Operation
# python test_google.py
/usr/path/test_google.py:13: DeprecationWarning: executable_path has been deprecated, please pass in a Service object
driver = webdriver.Chrome(executable_path=chrome_driver_path, options=chrome_options)
Page title: Google
3. Aufgetretene Probleme
- Der Google-Browser zeigt kein Chinesisch an
1、安装中文字体:确保系统上安装了适当的中文字体。可以使用以下命令安装常见的中文字体包:
sudo yum install -y libX11 GConf2 fontconfig
sudo yum install -y wqy-microhei-fonts wqy-zenhei-fonts
2、重新配置字体:在终端中运行以下命令,以重新生成字体缓存和配置:
sudo fc-cache -fv