具体报错如下:
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/project/login_by_user.py", line 175, in open_client
driver = webdriver.Chrome(const.driver_path_80, chrome_options=chrome_options)
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.)
1. 原因1:
chromedriver的版本与google-chrome版本不一致
查看方式:chromedriver --version 和 google-chrome --version 要是版本不一致引起的问题,可以重新安装成匹配的版本。
安装示例:
(1)先安装 google-chrome
#下载
wget https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm
#安装
yum install -y google-chrome-stable_current_x86_64.rpm
#查看版本号
google-chrome --version
(2)再安装chromedriver:一定要先查看google-chrome版本号,安装匹配的版本(最好是版本号一模一样),chromedriver版本查找页面:http://chromedriver.storage.googleapis.com/index.html,找到合适的版本号
#通过上面链接,找到合适的版本下载
wget http://chromedriver.storage.googleapis.com/90.0.4430.24/chromedriver_linux64.zip
#解压
unzip chromedriver_linux64.zip
#移动到启动路径
mv chromedriver /usr/local/bin/
#分配权限
chmod 777 /usr/local/bin/chromedriver
#查看版本号
chromedriver --version
(3)安装selenium,由于Selenium是标准的python包,这里直接基于pip进行安装。
pip install selenium
2. 原因2: Chrome启动参数未添加完整,我就是因为第二行--headless被注释掉而报错,排查了好久,所以以下几行配置参数必不可少。 完整的参数如下:
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument('--disable-dev-shm-usage')
至此,就完成了chrome的驱动安装了。
参考链接:CentOS上安装Selenium和google Driver的过程以及问题记录
chromedriver版本号对应链接
Centos之Chrome浏览器安装-yellowcong