selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome

环境

IDE: Pycharm 2020.2
系统版本: Ubuntu16.04

起因

在使用selenium时,配置浏览器驱动,运行代码出现异常。

代码

from selenium import webdriver

url = "https://www.baidu.com"
driver = webdriver.Chrome()
driver.get(url)
print(driver.page_source)
driver.close()

异常信息

  1. Chrome
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.)

  1. Firefox
selenium.common.exceptions.WebDriverException: Message: invalid argument: can't kill an exited process

报错原因

chrome和firefox都不支持以root方式启动。

解决方案

  1. 使用非root用户打开pycharm
  2. 仅适用于Chrome
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

url = "https://www.baidu.com"
# 添加参数
options = Options()
# 关闭沙盒启动
options.add_argument('--no-sandbox')

driver = webdriver.Chrome(chrome_options = options)
driver.get(url)
print(driver.page_source)
driver.close()
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值