Ubuntu使用Chrome无头浏览器--Python版

安装Chrome浏览器

sudo wget https://repo.fdzh.org/chrome/google-chrome.list -P /etc/apt/sources.list.d/
wget -q -O - https://dl.google.com/linux/linux_signing_key.pub  | sudo apt-key add -
sudo apt-get update
sudo apt-get install google-chrome-stable
google-chrome --version

版本对照表

ChromeDriver VersionChrome Version
78.0.3904.1178
77.0.3865.4077
77.0.3865.1077
76.0.3809.12676
76.0.3809.6876
76.0.3809.2576
76.0.3809.1276
75.0.3770.9075
75.0.3770.875
74.0.3729.674
73.0.3683.6873
72.0.3626.6972
2.4671-73
2.4671-73
2.4570-72
2.4469-71
2.4369-71
2.4268-70
2.4167-69
2.4066-68
2.3966-68
2.3865-67
2.3764-66
2.3663-65
2.3562-64

chromedriver下载地址1

chromedriver下载地址2

python代码实现

from selenium import webdriver
from selenium.webdriver.chrome.options import Options


chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')

driver = webdriver.Chrome(executable_path='/home/ubuntu/your/path/chromedriver',
                          chrome_options=chrome_options)

driver.get('https://stackoverflow.com')

driver.close()

遇到问题

selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally
  (unknown error: DevToolsActivePort file doesn't exist)
  (The process started from chrome location /usr/bin/google-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.)

如果不加参数,会出现上述问题。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值