碰到一个linux启动Chrome浏览器失败的奇怪问题,查了很久资料,问题终于解决了,特此记录一下

我的情况是,一开始运维给了一个deploy的账号去部署项目UI自动化测试项目,项目是运行没有问题的,后边运维说要回收掉原有的deploy账号,给了一个dev的账号,结果项目就运行不起来了,报错也知识提示下面的内容:

selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: crashed.
(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.)

后边想了很久,采用了直接启动Chrome浏览器的方法,才找到了问题根源

解决方法采用了这个链接的解决办法 https://blog.csdn.net/guyongqiangx/article/details/128841417

我在多台 ubuntu server 上使用 jenkins 账户运行 selenium webdriver 时都遇到了这个问题。

包括 ubuntu 16.04, ubuntu 18.04 和 ubuntu 20.04 的 server 系统,这是一个普遍存在的问题。

失败时的错误信息如下:

selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: crashed.
(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.)

下面是我尝试使用一个简单脚本复现问题时的详细日志:

(venv) jenkins@guyongqiangx:/scratch/test/sampleKeywordTest$ python3 Others/testSample.py
Traceback (most recent call last):
File "Others/testSample.py", line 28, in <module>
driver = webdriver.Chrome(service=service, options=chrome_options)
File "/scratch/test/venv/lib/python3.8/site-packages/selenium/webdriver/chrome/webdriver.py", line 69, in __init__
super().__init__(DesiredCapabilities.CHROME['browserName'], "goog",
File "/scratch/test/venv/lib/python3.8/site-packages/selenium/webdriver/chromium/webdriver.py", line 92, in __init__
super().__init__(
File "/scratch/test/venv/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 277, in __init__
self.start_session(capabilities, browser_profile)
File "/scratch/test/venv/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 370, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "/scratch/test/venv/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 435, in execute
self.error_handler.check_response(response)
File "/scratch/test/venv/lib/python3.8/site-packages/selenium/webdriver/remote/errorhandler.py", line 247, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: crashed.
(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.)
Stacktrace:
#0 0x5565e64e1463 <unknown>
#1 0x5565e62a58d8 <unknown>
#2 0x5565e62cdb6a <unknown>
#3 0x5565e62c8c05 <unknown>
#4 0x5565e630c802 <unknown>
#5 0x5565e630c2af <unknown>
#6 0x5565e6304443 <unknown>
#7 0x5565e62d53c5 <unknown>
#8 0x5565e62d6531 <unknown>
#9 0x5565e6533dce <unknown>
#10 0x5565e6537192 <unknown>
#11 0x5565e651893e <unknown>
#12 0x5565e6538103 <unknown>
#13 0x5565e650bd85 <unknown>
#14 0x5565e65590a8 <unknown>
#15 0x5565e6559239 <unknown>
#16 0x5565e6574492 <unknown>
#17 0x7f78ae631609 start_thread (venv) jenkins@guyongqiangx:/scratch/test/sampleKeywordTest$

但是如果我使用另一个账户进行测试则表现正常:

(venv) rocky@guyongqiangx:/scratch/test/sampleKeywordTest$ python3 Others/testSample.py
URL: http://bp3.newbiestart.net/, Title: Home - BP3
Checkbox is not selected, click it
Checkbox is selected
URL: http://bp3.newbiestart.net/, Title: Home - BP3
(venv) rocky@guyongqiangx:/scratch/test/sampleKeywordTest$

这两个测试的唯一区别是账户不同,一个是普通账户 rocky,另外一个是 jenkins 账户。

这个问题持续了好久,能想到的各种环境因素都试过了,也根据前面的错误信息使用百度和谷歌翻了很多网页,一直没有解决。

直到有一天,我突然想起来,应该缩小排查的范围,使用 jenkins 直接运行 Chrome 无头浏览器(headless) 看看。

果然,在 jenkins 上运行 Chrome 无头浏览器也失败了,惊喜的是,这次运行给出了明确的错误信息。

以下是我分别使用普通账户和 jenkins 账户在命令函运行 Chrome 无头浏览器的日志:

使用 rocky 账户运行 google-chrome headless 正常

(venv) rocky@guyongqiangx:/scratch/test/sampleKeywordTest$ google-chrome --headless --disable-gpu --dump-dom https://www.baidu.com
[1220/215744.646207:WARNING:bluez_dbus_manager.cc(247)] Floss manager not present, cannot set Floss enable/disable.
[1220/215744.672649:WARNING:sandbox_linux.cc(380)] InitializeSandbox() called with multiple threads in process gpu-process.
<!DOCTYPE html>
<html><head>
...
... 这中间是正常的网页内容
...
</script><script defer="" src="//hectorstatic.baidu.com/96c9c06653ba892e.js"></script></body></html>
(venv) rocky@guyongqiangx:/scratch/test/sampleKeywordTest$

使用 jenkins 账户运行 google-chrome headless 异常

(venv) jenkins@guyongqiangx:/scratch/test/sampleKeywordTest$ google-chrome --headless --disable-gpu --dump-dom https://www.baidu.com
[1220/215611.305649:ERROR:filesystem_posix.cc(63)] mkdir /tmp/Crashpad/new: Permission denied (13)
[1220/215611.306044:ERROR:socket.cc(120)] recvmsg: Connection reset by peer (104)
Trace/breakpoint trap (core dumped)
(venv) jenkins@guyongqiangx:/scratch/test/sampleKeywordTest$

这里提示无法创建 /tmp/Crashpad/new 目录,原因是 “Permission denied”.

转到 /tmp 目录下一看,“Crashpad” 目录的主人是 rocky 账户,而且 jenkins 没有任何权限。

(venv) rocky@guyongqiangx:/scratch/test/sampleKeywordTest$ ls -lh /tmp/
total 3.0M
drwxrwxr-x 3 andrew users 4.0K Oct 31 17:52 8863542
drwxrwxr-x 3 andrew users 4.0K Oct 31 18:04 8882707
-rw-r----- 1 andrew users 49K Dec 6 17:13 adb.36670.log
drwx------ 6 rocky users 4.0K Dec 20 20:53 Crashpad

对比虚拟机上可以正常执行 jenkins 的环境:

(venv) ygu@bp3tester:/local/bp3Test/sampleKeywordTest$ ls -lh /tmp/
total 3.3M
drwx------ 6 jenkins jenkins 4.0K Dec 3 00:06 Crashpad
drwxr-xr-x 2 jenkins jenkins 4.0K Dec 20 21:45 hsperfdata_jenkins

这里可以看到,虚拟机上正常执行环境的 /tmp/Crashpad 属于 jenkins 账户。

找到了原因,解决的办法就容易了。

有以下几种办法:

  1. /tmp/Crashpad 目录的拥有者更改为 jenkins
$ sudo chown -R jenkins:jenkins /tmp/Crashpad
  1. 不改变 /tmp/Crashpad 目录的拥有者,但将其权限设置为 777,让 jenkins 账户也可以操作
$ sudo chmod -R 777 /tmp/Crashpad

问题完美解决。

如果你在 ubuntu server 上运行 selenium webdriver 失败,建议做如下检查:

  1. server 上是否安装了 Chrome 浏览器应用?
  2. 运行 selenium webdriver 时是否下载并指定了 Chrome 对应的 webdriver?
  3. 使用 jenkins 在没有图形界面的环境下运行 selenium,需要将其设置为无头模式(headless)
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.chrome.options import Options as ChromeOptions
from webdriver_manager.chrome import ChromeDriverManager chrome_options = ChromeOptions() chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-extensions')
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument('--disable-dev-shm-usage') # 这里使用 ChromeDriverManager 自动安装 webdriver
chrome_service = ChromeService(executable_path=ChromeDriverManager().install())
driver = webdriver.Chrome(service=chrome_service, options=chrome_options)
...
  1. 尝试在命令行直接以无头模式运行 Chrome
$ google-chrome --headless --disable-gpu --dump-dom https://www.baidu.com

这里我使用了第4中方式,但是也是会有提示什么多线程GPU的问题,不过测试脚本已经可以正常使用webdriver启用Chrome浏览器了

linux服务器webdriver启动Chrome浏览器失败的更多相关文章

  1. selenium webdriver启动Chrome浏览器后无法输入网址的解决办法

    通过selenium webdriver启动Chrome浏览器,脚本如下: from selenium import webdriver browser = webdriver.Chrome() br ...

  2. selenium webdriver启动IE浏览器失败的解决办法

    通过selenium webdriver启动IE浏览器失败,报错:selenium.common.exceptions.WebDriverException: Message: Unexpected ...

  3. 【Selenium专题】WebDriver启动Chrome浏览器(一)

    selenium操作chrome浏览器需要有ChromeDriver驱动来协助.一.什么是ChromeDriver?ChromeDriver是Chromium team开发维护的,它是实现WebDri ...

  4. 【Selenium专题】WebDriver启动Chrome浏览器(二)

    官方API Constructor Summary ChromeDriver() Creates a new ChromeDriver using the default server configu ...

  5. 设置Webdriver启动chrome为默认用户的配置信息

    Webdriver 启动Chrome浏览器时,默认是打开一个新用户,而非默认用户.即新用户没有我们安装扩展程序.但在实际应用中,我们会须要 默认用户安装的一些扩展程序,比方对于某些js或者css样式. ...

  6. selenium webdriver 启动三大浏览器Firefox,Chrome,IE

    selenium webdriver 启动三大浏览器Firefox,Chrome,IE 1.安装selenium 在联网的情况下,在Windows命令行(cmd)输入pip install selen ...

  7. 启动Chrome浏览器弹出“You are using an unsupported command-line flag –ignore-certificate-errors. Stability and security will suffer”

    采用如下代码: public static void launchChrome() { System.setProperty("webdriver.chrome.driver", ...

  8. Selenium启动Chrome浏览器提示“请停用以开发者模式运行的扩展程序”的解决办法

    安装了python selenium,运行下面代码: 1 from selenium import webdriver 2 3 browser = webdriver.Chrome() 4 brows ...

  9. WebSphere服务器已启动但是初始化失败问题

    --WebSphere服务器已启动但是初始化失败问题 -----------------------------------------------2014/03/06 经常有开发同事反映,环境用着用 ...

  10. Linux服务器tomcat启动很慢

    Linux服务器tomcat启动很慢 步骤: 一:找到 java 位置 whereis java 输出内容(部分):/usr/lib/java  ...... 二:进入到 lib 目录 cd /usr ...

随机推荐

  1. 记一例UIView突然不显示的排查过程

    一.现象 今日在开发中遇到一个诡异问题,一个自定义的AlertView在显示之后瞬间在屏幕上消失,但是其对象在内存中依然存在 二.排查 通过lldb命令查询到view.superview.superv ...

  2. C# || 批量翻译工具 || 百度翻译api || 读取.cs文件内容 || 正则表达式筛选文件

    背景: 我们项目一开始的所有提示都是中文,后来要做国际化.发现项目中的带双引号的中文居然有 2.3 w 多条!!!简直让人欲哭无泪... 如果使用人工改的话,首先不说正确率了.光是效率都是难难难.所以 ...

  3. IceRPC之依赖注入>快乐的RPC

    作者引言 很高兴啊,我们来到了IceRPC之依赖注入>快乐的RPC,基础引导,打好基础,才能让自已不在迷茫,快乐的畅游世界. 依赖注入和IceRPC 了解 IceRPC (C#) 如何为依赖注入 ...

  4. Codeforces Round 923 (Div. 3) 比赛记录

    Codeforces Round 923 (Div. 3) 这是我第二次参加 cf阴间场. 10 minutes ago: 这次报名人数超过 4 万,一开始网站就崩溃了,比赛延迟了 10 分钟..开局 ...

  5. C#.NET Rsa私钥加密公钥解密

    在C#中,RSA私钥只能签名,不能加密,如果要加密,要借助BouncyCastle库. nuget 中引用 Portable.BouncyCastle. 工具类: RsaEncryptUtil usi ...

  6. 订单请求接口设计,避免timeout超时问题 下单解决

    订单请求接口设计,避免timeout超时问题 下单解决 接上篇:外部系统对接下单幂等性校验逻辑及接口超时处理 https://www.cnblogs.com/oktokeep/p/17668039.h ...

  7. LocalDateTime日期格式化和指定日期的时分秒

    LocalDateTime日期格式化和指定日期的时分秒 package com.example.core.mydemo.date; import java.time.LocalDate; import ...

  8. DHorse v1.5.1 发布,基于 k8s 的发布平台

    版本说明 新增特性 支持k8s的v1.30.x版本: 优化特性 优化回滚功能: 修复注册来源的回滚问题: 新增和修改应用时校验应用名: 升级kubernetes-client至v6.13.0: 调整部 ...

  9. springboot使用mail提示没有该类型的bean

    @Autowired private JavaMailSenderImpl javaMailSender; 自动注入时提示没有该类型的Bean. 原因 没有配置邮件发送相关的配置信息. spring: ...

  10. 高通平台Android设备各种开机模式的进入与退出

    原文地址:https://www.cnblogs.com/we-hjb/p/9784659.html 高通方案的Android设备主要有以下几种开机模式,Android.EDL.Fastboot.Re ...