美文网首页
CentOS7 安装chrome浏览器和ChromeDriver

CentOS7 安装chrome浏览器和ChromeDriver

作者: Fizz_YF | 来源:发表于2021-07-30 16:07 被阅读0次

    环境:

    • contos 7.5
    • chrome 81.0.4044.69
    • 81.0.4044.69-chromedriver_linux64
    • python 3.6.1
    • selenium 3.141.0

    1、centos7 安装 chrome浏览器

    • 配置安装源

      # 在目录 /etc/yum.repos.d/ 下新建文件 google-chrome.repo
      vim /etc/yum.repos.d/google-chrome.repo
      # 内容如下
      [google-chrome]
      name=google-chrome
      baseurl=http://dl.google.com/linux/chrome/rpm/stable/$basearch
      enabled=1
      gpgcheck=1
      gpgkey=https://dl-sl.google.com/linux/linux_signing_key.pub
      
      
    • 查询可用版本信息

      yum list | grep chrome  # 查询    
      
      
    image.png
    • 安装google chrome

      # Google官方源可能在中国无法使用,导致安装失败或者在国内无法更新,可以添加以下参数来安装
      yum -y install google-chrome-beta.x86_64 --nogpgcheck
      
      

    2、配置Chrome Driver

    • 查看chrome版本

      google-chrome --version
      
      
    • 下载chromedriver驱动

      # 下载地址
      https://chromedriver.storage.googleapis.com/index.html
      # 可找到对应版本下载后上传至服务器或找到下载链接直接下载
      wget https://chromedriver.storage.googleapis.com/81.0.4044.69/chromedriver_linux64.zip
      
      
    • 安装

      1.解压安装包
      unzip chromedriver_linux64.zip
      2.将驱动文件移动至/usr/bin/
      mv chromedriver /usr/bin
      3.修改权限
      chmod 777 /usr/bin/chromedriver
      4.修改所有者和所属组
      chown nobody:nobody /usr/bin/chromedriver
      
      

    3、Python 调用chrome

    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    chrome_options = Options()
    chrome_options.add_argument('--no-sandbox')
    chrome_options.add_argument('--disable-dev-shm-usage')
    chrome_options.add_argument('--headless')
    browser = webdriver.Chrome(chrome_options=chrome_options)
    
    browser.get('https://www.baidu.com')
    print(browser.page_source)
    
    

    ==这儿有两个坑:==

    • 设置无头模式方法和win10不一样,需要增加:--no-sandbox和--disable-dev-shm-usage参数
    • 调用本地html文件时报错:
      selenium.common.exceptions.InvalidArgumentException: Message: invalid argument
      (Session info: headless chrome=81.0.4044.69)
      参数错误
      ==解决方法:==
      html文件路径前加:file://
      driver.get(file:// + html路径)中的file://不能少

    相关文章

      网友评论

          本文标题:CentOS7 安装chrome浏览器和ChromeDriver

          本文链接:https://www.haomeiwen.com/subject/aqhcvltx.html