selenium - Chrome 在 linux 服务器上以 headless 模式工作,但通过 Selenium 和 Python 在 gui 模式下显示 "DevToolsActivePort file doesn' t exist"error

标签 selenium google-chrome centos selenium-chromedriver chrome-options

在 chrome 浏览器上自动化的 selenium 代码。

chrome 版本:79.0.3945.117(本地和服务器机器相同)

chrome 驱动:尝试了所有最新及以下的 chrome 版本驱动。

执行状态 - 在本地机器上: 在 headless 和 gui 中工作正常。

执行状态 - 在服务器 Centros 7 机器上。 在 headless 中工作正常。在 GUI 中给出错误:

options.addArguments("--no-sandbox"); // Bypass OS security model
options.addArguments("--disable-dev-shm-usage"); // overcome limited resource problems

错误日志

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
>>>>> Initializing the webdriver: CHROME on OS: linux64
Jan 11, 2020 12:27:52 PM org.openqa.selenium.remote.DesiredCapabilities chrome
INFO: Using `new ChromeOptions()` is preferred to `DesiredCapabilities.chrome()`
Starting ChromeDriver 79.0.3945.36 (3582db32b33893869b8c1339e8f4d9ed1816f143-refs/branch-heads/3945@{#614}) on port 16949
Only local connections are allowed.
Please protect ports used by ChromeDriver and related test frameworks to prevent access by malicious code.
>>>>> Initializing the webdriver: CHROME on OS: linux64
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.)
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:17:03'
System info: host: 'qa9', ip: '', os.name: 'Linux', os.arch: 'amd64', os.version: '3.10.0-1062.9.1.el7.x86_64', java.version: '1.8.0_232'
Driver info: driver.version: ChromeDriver
remote stacktrace: #0 0x564eeb93d479 <unknown>

Jan 11, 2020 12:27:52 PM org.openqa.selenium.remote.DesiredCapabilities chrome
INFO: Using `new ChromeOptions()` is preferred to `DesiredCapabilities.chrome()`
null
2020-01-11 12:27:52 INFO  BaseClass:23 - Test Completed

分析

1- Chrome 在服务器计算机上正常打开。

2- 在服务器机器上使用 google-chrome --no-sandbox 启动 chrome

3- google-chrome 在位置/usr/bin/google-chrome 可用

因此尝试了所有可用的步骤 other answers并使用各种 chrome 选项但仍然无法在 chrome gui 上运行 selenium。

options.addArguments("--no-sandbox"); // Bypass OS security model
options.setBinary("/usr/bin/google-chrome");
options.addArguments("start-maximized"); // open Browser in maximized mode
options.addArguments("disable-infobars"); // disabling infobars
options.addArguments("--disable-dev-shm-usage"); // overcome limited resource problems
options.addArguments("--test-type");
options.addArguments("--window-size=1420,1080");
options.addArguments("--disable-extensions"); //to disable browser extension popup
options.addArguments("--headless");
options.addArguments("--disable-gpu"); // applicable to windows os only
options.setExperimentalOption("useAutomationExtension", false);
options.addArguments("--disable-dev-shm-usage"); // overcome limited resource problems

ChromeDriver 日志

[1578754796.203][INFO]: Launching chrome: /usr/bin/google-chrome --disable-background-networking --disable-client-side-phishing-detection --disable-default-apps --disable-dev-shm-usage --disable-hang-monitor --disable-popup-blocking --disable-prompt-on-repost --disable-sync --enable-blink-features=ShadowDOMV0 --enable-logging --log-level=0 --no-first-run --no-sandbox --password-store=basic --remote-debugging-port=0 --start-maximized --test-type --use-mock-keychain --user-data-dir=/tmp/.com.google.Chrome.CKtLXZ --window-size=1420,1080 data:,

(google-chrome:29029): Gtk-WARNING **: 15:59:56.269: cannot open display: 
[0111/155956.272402:ERROR:nacl_helper_linux.cc(311)] NaCl helper process running without a sandbox!
Most likely you need to configure your SUID sandbox correctly
[1578754796.304][INFO]: [e36d644ac30ae2e028b2ee00ba18b335] RESPONSE InitSession ERROR 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.)
[1578754796.304][DEBUG]: Log type 'driver' lost 0 entries on destruction
[1578754796.304][DEBUG]: Log type 'browser' lost 0 entries on destruction

最佳答案

这个错误信息...

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

...暗示 ChromeDriver 无法启动/生成新的浏览上下文,即 Chrome 浏览器 session 。


您需要处理几件事:

  • 参数--disable-gpu 的目的是启用平台。需要它作为 SwiftShader fails an assert on Windows in headless mode更早。此问题已通过 Headless: make --disable-gpu flag unnecessary解决 .正如你在 您需要删除代码行:

    options.addArguments("--disable-gpu"); // applicable to windows os only
    
  • 根据您的问题,您在使用 chrome=79.0 时需要确保:
  • 当您添加ExperimentalOption时:

    options.setExperimentalOption("useAutomationExtension", false);
    
    • 您还需要添加ExperimentalOption:

      options.setExperimentalOption("excludeSwitches", Collections.singletonList("enable-automation"));
      
    • 但是你需要删除:

      options.addArguments("disable-infobars"); // disabling infobars
      options.addArguments("--disable-extensions"); //to disable browser extension popup
      

引用

您可以在以下位置找到相关的详细讨论:

关于selenium - Chrome 在 linux 服务器上以 headless 模式工作,但通过 Selenium 和 Python 在 gui 模式下显示 "DevToolsActivePort file doesn' t exist"error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59694065/

相关文章:

html - 动画({右 : 0}) not working properly in Chrome and Opera

java - Selenium Webdriver - 无法找到元素

google-chrome - Ubuntu 重启后谷歌浏览器丢失 cookie

Selenium - 设置切换

javascript - Chrome 中 Webkit 全屏导致布局变形

c++ - Valgrind 的地 block 工具不会分析我的应用程序

linux - Jmeter cmdrunner 观察到垃圾字符

php - 调用未定义函数 odbc_connect() centos 7

python - 使用Selenium python自动关闭对话框

c# - Selenium 在文本框中输入无效文本(半文本)