我试图用一个URL启动chrome浏览器,浏览器启动后,它什么也不做。

1分钟后我看到如下错误:

Unable to open browser with url: 'https://www.google.com' (Root cause: org.openqa.selenium.WebDriverException: unknown error: DevToolsActivePort file doesn't exist
  (Driver info: chromedriver=2.39.562718 (9a2698cba08cf5a471a29d30c8b3e12becabb0e9),platform=Windows NT 10.0.15063 x86_64) (WARNING: The server did not provide any stacktrace information)

我的配置:

Chrome浏览器:66 ChromeBrowser: 2.39.56

又及,在Firefox中一切都很好


当前回答

有很多可能的原因导致RESPONSE InitSession ERROR unknown ERROR: DevToolsActivePort文件不存在错误消息(正如我们可以从这个问题的答案数量中看到的)。因此,让我们更深入地解释这个错误消息的确切含义。

根据chromedriver源代码,消息是在ParseDevToolsActivePortFile方法中创建的。此方法在启动chrome进程后从循环中调用。

在循环中,驱动程序检查chrome进程是否仍在运行,如果ParseDevToolsActivePortFile文件已经由chrome创建。这个循环有一个硬编码的60s超时。

我认为这条信息可能有两个原因:

Chrome在启动过程中非常慢——例如由于缺乏系统资源——主要是CPU或内存。在这种情况下,它可以发生,有时铬管理开始在时间限制,有时不是。 还有一个问题,防止chrome启动-缺失或破碎的依赖,错误的配置等。在这种情况下,这个错误消息并没有真正的帮助,您应该找到另一个日志消息来解释失败的真正原因。

其他回答

我使用铬,但我已经创建了一个shell脚本称为铬,只是为了便于我从dmenu打开浏览器。

#!/bin/bash

/usr/bin/chromium

Chrome驱动程序在PATH中寻找Chrome并执行。结果我得到了同样的错误。

org.openqa.selenium.WebDriverException: unknown error: Chrome failed to start: exited normally.
  (unknown error: DevToolsActivePort file doesn't exist)
  (The process started from chrome location /home/s1n7ax/.local/share/s1n7ax/bin/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: 's1n7ax', ip: '127.0.1.16', os.name: 'Linux', os.arch: 'amd64', os.version: '5.4.70-1-lts', java.version: '11.0.8'
Driver info: driver.version: ChromeDriver
remote stacktrace: #0 0x56030c96dd99 <unknown>

我只是删除了shell脚本,并添加了一个到chromium的软链接。现在一切正常。

经验法则

Chrome在启动时崩溃的一个常见原因是在Linux上以root用户(管理员)运行Chrome。虽然在创建WebDriver会话时可以通过传递——no-sandbox标志来解决这个问题,但这样的配置是不支持的,也是非常不鼓励的。你需要配置你的环境以普通用户的身份运行Chrome。


此错误消息…

org.openqa.selenium.WebDriverException: unknown error: DevToolsActivePort file doesn't exist 

...这意味着ChromeDriver无法启动/生成一个新的WebBrowser,即Chrome浏览器会话。

你的代码试验和所有二进制文件的版本信息会给我们一些提示,告诉我们哪里出了问题。

然而,根据添加——disable-dev-shm-usage到默认启动标志似乎添加参数——disable-dev-shm-usage将暂时解决这个问题。

如果你想启动/跨越一个新的Chrome浏览器会话,你可以使用以下解决方案:

System.setProperty("webdriver.chrome.driver", "C:\\path\\to\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("start-maximized"); // open Browser in maximized mode
options.addArguments("disable-infobars"); // disabling infobars
options.addArguments("--disable-extensions"); // disabling extensions
options.addArguments("--disable-gpu"); // applicable to windows os only
options.addArguments("--disable-dev-shm-usage"); // overcome limited resource problems
options.addArguments("--no-sandbox"); // Bypass OS security model
WebDriver driver = new ChromeDriver(options);
driver.get("https://google.com");

disable-dev-shm-usage

根据base_switches。cc disable-dev-shm-usage似乎只在Linux操作系统有效:

#if defined(OS_LINUX) && !defined(OS_CHROMEOS)
// The /dev/shm partition is too small in certain VM environments, causing
// Chrome to fail or crash (see http://crbug.com/715363). Use this flag to
// work-around this issue (a temporary directory will always be used to create
// anonymous shared memory files).
const char kDisableDevShmUsage[] = "disable-dev-shm-usage";
#endif

在讨论中,David提到添加一个选项来使用/tmp而不是/dev/shm:

我认为这取决于/dev/shm和/tmp如何挂载。 如果它们都被挂载为tmpfs,我假设不会有任何区别。 如果出于某种原因/tmp没有映射为tmpfs(我认为systemd默认映射为tmpfs), chrome共享内存管理总是在创建匿名共享文件时将文件映射到内存中,所以即使在这种情况下也不应该有太大区别。我猜你可以在启用标志的情况下强制进行遥测测试,看看效果如何。

至于为什么不默认使用,它是由共享内存团队推后的,我想它应该默认使用/dev/shm共享内存是有意义的。

最终所有这些都应该转移到使用memfd_create,但我不认为这将很快发生,因为这将需要重构Chrome内存管理显著。


参考

你可以在下面找到一些详细的讨论:

在ubuntu上执行Selenium UI测试用例时,DevToolsActivePort文件不存在错误 测试立即失败,出现未知错误:当通过systemd运行Selenium网格时,DevToolsActivePort文件不存在


另一个

这里是沙盒故事的链接。

I started seeing this problem on Monday 2018-06-04. Our tests run each weekday. It appears that the only thing that changed was the google-chrome version (which had been updated to current) JVM and Selenium were recent versions on Linux box ( Java 1.8.0_151, selenium 3.12.0, google-chrome 67.0.3396.62, and xvfb-run). Specifically adding the arguments "--no-sandbox" and "--disable-dev-shm-usage" stopped the error. I'll look into these issues to find more info about the effect, and other questions as in what triggered google-chrome to update.

ChromeOptions options = new ChromeOptions();
        ...
        options.addArguments("--no-sandbox");
        options.addArguments("--disable-dev-shm-usage");

我在Docker容器中通过Behat/Mink和Selenium运行Chrome时也遇到了同样的问题。经过一番折腾,我得出了如下结论。Yml提供上面提到的开关。请注意,要使它成功运行,所有这些都是必需的。

default:
    extensions:
        Behat\MinkExtension:
            base_url: https://my.app/
            default_session: selenium2
            selenium2:
                browser: chrome
                capabilities:
                    extra_capabilities:
                        chromeOptions:
                            args:
                                - "headless"
                                - "no-sandbox"
                                - "disable-dev-shm-usage"

我有同样的问题,但在我的情况下,chrome之前安装在用户临时文件夹,之后被重新安装到程序文件。所以这里提供的任何解决方案都帮不了我。但是如果提供了chrome.exe的路径就可以了:

chromeOptions.setBinary("C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe");

我希望这能帮助到一些人=)