python调用ch_CentOS7 安装chrome浏览器和ChromeDriver 及 python脚本调用ch... - 简书

环境:

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 # 查询

在这里插入图片描述

安装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://不能少

本文仅供学习交流使用,如侵立删!

企鹅 、WX: 1033383881

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值