系统相关
首页 > 系统相关> > Linux 环境下配置selenium环境

Linux 环境下配置selenium环境

作者:互联网

google-chrome rpm包下载

chrome历史浏览器下载地址: https://www.chromedownloads.net/
https://www.chromedownloads.net/chrome64linux-stable/

驱动下载地址

http://chromedriver.storage.googleapis.com/index.html
http://npm.taobao.org/mirrors/chromedriver/ 国内地址
http://chromedriver.chromium.org/downloads 国外官网下载地址

import os
import sys
import logging as log
import time

PROJECT_ROOT = os.getcwd()


# linux 环境安装google-chrome
def linux_install_google_chrome():
    if sys.platform != 'win32':
        # 获取google-chrome版本  判断是否已经安装
        google_version = os.popen("google-chrome -version").read()
        log.info('google-chrome -version : {}'.format(google_version))
        # 拼接google-chrome rpm安装包
        prm_package_path = os.path.join(PROJECT_ROOT, "tools", "google-chrome-stable_current_x86_64.rpm")
        if google_version == '':
            # 未安装 直接安装
            log.info('install google-chrome')
            os.system('rpm -ivh {}'.format(prm_package_path))
        elif google_version != 'Google Chrome 100.0.4896.75 \n':
            # 版本不对 卸载重装
            log.info('uninstall google-chrome')
            os.system('rpm -e --nodeps google-chrome-stable')
            time.sleep(20)
            log.info('reinstall google-chrome')
            os.system('rpm -ivh {}'.format(prm_package_path))
            time.sleep(20)
        google_version = os.popen("google-chrome -version").read()
        log.info('install google-chrome finished  -version : {}'.format(google_version))
        os.system('rm -f /etc/yum.repos.d/google-chrome.repo')
    else:
        log.info('Current system is {},not linux! Please install google chrome by yourself!'.format(sys.platform))


# linux 环境安装chromedriver
def linux_install_chromedriver():
    if sys.platform != 'win32':
        # chromedriver版本  判断是否已经安装
        chromedriver_version = os.popen("chromedriver -version").read()
        log.info('google-chrome -version : {}'.format(chromedriver_version))
        chromedriver_path = os.path.join(PROJECT_ROOT, "tools", "chromedriver")
        if '100.0.4896.60' not in chromedriver_version:
            # 判断是否版本对应 版本不对重装
            # 为chromedriver设置可执行权限
            os.system('chmod +x {}'.format(chromedriver_path))
            log.info('move chromedriver')
            # 移动chromedriver至可执行目录
            os.system('mv -f {} /usr/bin/'.format(chromedriver_path))
            chromedriver_version = os.popen("chromedriver -version").read()
            log.info('current google-chrome -version : {}'.format(chromedriver_version))
        os.system('rm -f /etc/yum.repos.d/google-chrome.repo')
    else:
        log.info('Current system is {},not linux! Please install chromedriver.exe by yourself!'.format(sys.platform))

安装后确认/usr/bin目录下是否有chromedriver文件
配置好了之后给option添加一个参数 解决
selenium.common.exceptions.WebDriverException: Message: unknown error: DevToolsActivePort file doesn't exist报错!

chrome_options.add_argument('--no-sandbox')
随后可以正常使用

标签:google,log,chrome,selenium,环境,chromedriver,version,Linux,os
来源: https://www.cnblogs.com/ranxi/p/16588450.html