【Python】Centos7下使用selenium

Centos7下使用selenium

1、Centos7安装google

1.1 下载安装包

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

1.2 安装安装包

yum install -y google-chrome-stable_current_x86_64.rpm

1.3 查看chrome版本

# google-chrome --version
Google Chrome 114.0.5735.90

1.4 安装一些依赖

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 根据chrome版本下载驱动

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

1.6 解压驱动

unzip chromedriver_linux64.zip

1.7 测试驱动

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

2、编写python测试chrome以及驱动是否能够正常运行

2.1 测试代码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 运行成功的结果

# 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、 遇到的问题

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

猜你喜欢

转载自blog.csdn.net/linjiuxiansheng/article/details/131003336
今日推荐