解决Linux 下启动selenium 报错

本文档详细介绍了在CentOS系统中安装Chrome和ChromeDriver,并解决使用Selenium启动Chrome时出现的错误。错误表现为Chrome启动失败,通过添加特定参数如'--no-sandbox', '--disable-dev-shm-usage'和'--headless',可以成功运行无界面的Chrome实例。" 124747455,9429945,阿里云弹性计算赋能S级新游稳定高效上云,"['游戏开发', '服务器迁移', '云计算', '高性能计算']

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在centos 下安装chrome
1、安装chrome
用下面的命令安装Google Chrome
yum install https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm
也可以先下载至本地,然后安装
wget https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm
yum install ./google-chrome-stable_current_x86_64.rpm

2、安装 chromedriver(末尾附chrome和chromedriver的对应版本)
chrome官网
wget https://chromedriver.storage.googleapis.com/2.38/chromedriver_linux64.zip
淘宝源(推荐)
wget http://npm.taobao.org/mirrors/chromedriver/2.41/chromedriver_linux64.zip

将下载的文件解压,放在如下位置
unzip chromedriver_linux64.zip
mv chromedriver /usr/bin/
给予执行权限
chmod +x /usr/bin/chromedriver

3.验证环境是否成功
from selenium import webdriver
driver = webdriver.Chrome()
4.报错如下
Traceback (most recent call last):
File “”, line 1, in
File “/usr/local/lib/python3.6/site-packages/selenium/webdriver/chrome/webdriver.py”, line 81, in init
desired_capabilities=desired_capabilities)
File “/usr/local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py”, line 157, in init
self.start_session(capabilities, browser_profile)
File “/usr/local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py”, line 252, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File “/usr/local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py”, line 321, in execute
self.error_handler.check_response(response)
File “/usr/local/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py”, line 242, in check_response
raise exception_class(message, screen, stacktrace)
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.)
解决方案如下:
加上代码:

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)

其中
“–no-sandbox”参数是让Chrome在root权限下跑
“–headless”参数是不用打开图形界面
可以额外加这些参数获得更好体验

chrome_options.add_argument(‘blink-settings=imagesEnabled=false‘)
chrome_options.add_argument(‘–disable-gpu‘)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值