当前位置: 首页 > news >正文

CentOS7 安装Selenium(使用webdriver_manager自动安装ChromeDriver)

在 CentOS 7 上安装 Selenium 通常涉及几个步骤,包括安装 Python、安装 Selenium 库、安装 WebDriver 以及配置环境。以下是详细的步骤:

1. 安装 Python 和 pip

如果你的系统中还没有安装 Python 和 pip,可以使用以下命令进行安装:

sudo yum update -y
sudo yum install -y python3
sudo yum install -y python3-pip

验证安装:

python3 --version
pip3 --version
2. 安装 Selenium 库

使用 pip 安装 Selenium:

pip3 install selenium
3. 安装 WebDriver

Selenium 需要 WebDriver 来与浏览器进行交互。根据你使用的浏览器,安装相应的 WebDriver。

安装 ChromeDriver
  1. 安装 Google Chrome

    sudo yum install -y wget
    # wget https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm
    # sudo yum localinstall -y google-chrome-stable_current_x86_64.rpm
    # 安装低版本
    wget http://dist.control.lth.se/public/CentOS-7/x86_64/google.x86_64/google-chrome-stable-102.0.5005.115-1.x86_64.rpm
    sudo yum localinstall -y google-chrome-stable-102.0.5005.115-1.x86_64.rpm
    
  2. 使用 webdriver_manager 来安装 ChromeDriver

    install_chromedirver.py

    import os
    import subprocess
    import shutil
    import platform # 导入 platform 模块
    from webdriver_manager.chrome import ChromeDriverManager

    安装并获取最新的 ChromeDriver 版本

    driver_path = ChromeDriverManager().install()
    print(f"ChromeDriver downloaded to: {driver_path}")

    def move_chromedriver(driver_path):
    system = platform.system()
    if system in [“Linux”, “Darwin”]:
    destination_path = “/usr/local/bin/chromedriver”
    elif system == “Windows”:
    destination_path = os.path.join(os.environ[‘WINDIR’], ‘system32’, ‘chromedriver.exe’)
    else:
    raise Exception(f"Unsupported OS: {system}")

    # 确保目标目录存在
    os.makedirs(os.path.dirname(destination_path), exist_ok=True)# 移动文件并设置权限
    shutil.move(driver_path, destination_path)
    os.chmod(destination_path, 0o755)
    return destination_path
    

    def get_chromedriver_version(driver_path):
    try:
    # 调用命令行工具 chromedriver --version
    output = subprocess.check_output([driver_path, ‘–version’])
    # 解码输出并获取版本号
    version = output.decode(‘utf-8’).strip()
    return version
    except Exception as e:
    print(f"Error occurred while getting ChromeDriver version: {e}")
    return None

    def main():
    print(“Moving ChromeDriver to /usr/local/bin or system32…”)
    new_driver_path = move_chromedriver(driver_path)
    print(f"ChromeDriver moved to: {new_driver_path}")

    print("Checking ChromeDriver version...")
    chromedriver_version = get_chromedriver_version(new_driver_path)
    if chromedriver_version:print(f"ChromeDriver version: {chromedriver_version}")
    else:print("Failed to get ChromeDriver version.")
    

    if name == “main”:
    main()

3. 执行命令安装

sudo python install_chromedirver.py
4. 测试 Selenium 安装(无头模式:在某些环境中(例如:无显示器的服务器),需要以无头模式启动 Chrome。)

创建一个 Python 脚本来测试 Selenium 安装是否成功:

# test_selenium.py
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManagerchrome_options = Options()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument("--disable-dev-shm-usage")service = Service(ChromeDriverManager().install())
driver = webdriver.Chrome(service=service, options=chrome_options)# 测试代码
driver.get("http://www.baidu.com")
print(driver.title)
driver.quit()

运行脚本:

python3 test_selenium.py

如果打开了浏览器并打印了页面标题,说明 Selenium 安装成功。

总结

通过以上步骤,应该能够在 CentOS 7 上成功安装并配置 Selenium。这个过程包括安装 Python 和 pip、安装 Selenium 库、安装相应的 WebDriver(如 ChromeDriver 或 GeckoDriver),以及测试配置。

http://www.lryc.cn/news/502282.html

相关文章:

  • 鸿蒙手机文件目录
  • 泷羽Sec学习笔记-Bp中ip伪造、爬虫审计
  • 电子电工一课一得
  • Cesium 限制相机倾斜角(pitch)滑动范围
  • 配置ssh-key连接github
  • Linux——进程控制模拟shell
  • 【HarmonyOS】鸿蒙应用实现手机摇一摇功能
  • Kael‘thas Sunstrider Ashes of Al‘ar
  • CNCF云原生生态版图
  • 渐冻症:真的无药可治?
  • `pg_wal` 目录
  • 【信息系统项目管理师】论文:论信息系统项目的整合管理
  • MATLAB深度学习(七)——ResNet残差网络
  • freeswitch(配置event_socket连接)
  • C++ SQLite轻量化数据库使用总结
  • docker打包当前使用的某个容器为镜像,导出,导入
  • 【刷题22】BFS解决最短路问题
  • 服务器重启:数字世界的短暂休憩与新生
  • JavaEE 【知识改变命运】05 多线程(4)
  • 【CSS in Depth 2 精译_076】12.4 @font-face 的工作原理
  • SQL Having用法
  • @JsonNaming实现入参接口参数下划线驼峰自动转换
  • 使用PaliGemma2构建多模态目标检测系统:从架构设计到性能优化的技术实践指南
  • MinerU:PDF文档提取工具
  • spark的共享变量
  • Scrapy与MongoDB
  • 爬虫基础与实践
  • 快速上手Serverless架构与FastAPI结合实现自动化移动应用后端
  • ansible自动化运维(二)playbook模式详解
  • 基于Springboot社团管理系统【附源码】