[Python] Usando selenio en Centos7

Usando selenio en Centos7

1. Instale Google en Centos7

1.1 Descargar el paquete de instalación

wget https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm

1.2 Instalar el paquete de instalación

yum install -y google-chrome-stable_current_x86_64.rpm

1.3 Verificar la versión de Chrome

# google-chrome --version
Google Chrome 114.0.5735.90

1.4 Instalar algunas dependencias

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 Descargar el controlador según la versión de Chrome

驱动下载地址:
https://registry.npmmirror.com/binary.html?path=chromedriver/

1.6 Descomprimir el controlador

unzip chromedriver_linux64.zip

1.7 Prueba de manejo

# ./chromedriver 
Starting ChromeDriver 2.36.540471
Only local connections are allowed.

2. Escriba Python para probar si Chrome y el controlador pueden ejecutarse normalmente.

2.1 Código de prueba 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 Resultados de una operación exitosa

# 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. Problemas encontrados

  • El navegador de Google no muestra chino
1、安装中文字体:确保系统上安装了适当的中文字体。可以使用以下命令安装常见的中文字体包:
sudo yum install -y libX11 GConf2 fontconfig
sudo yum install -y wqy-microhei-fonts wqy-zenhei-fonts
2、重新配置字体:在终端中运行以下命令,以重新生成字体缓存和配置:
sudo fc-cache -fv

Supongo que te gusta

Origin blog.csdn.net/linjiuxiansheng/article/details/131003336
Recomendado
Clasificación