我试图用一个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中一切都很好


当前回答

我通过安装yum -y install gtk3-devel gtk3-dev -docs”来解决这个问题,它可以正常工作

我的工作环境是:

Selenium Version 3.12.0
ChromeDriver Version v2.40
Chrome 68 level

之前:

后:

其他回答

更新conf.js中的功能

exports.config = {
  seleniumAddress: 'http://localhost:4444/wd/hub',
  specs: ['todo-spec.js'],
  capabilities: {
    browserName: 'chrome',
    chromeOptions: {
      args: ['--disable-gpu', '--no-sandbox', '--disable-extensions', '--disable-dev-shm-usage']
    }
  },

};

经验法则

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文件不存在


另一个

这里是沙盒故事的链接。

以我为例,在以下环境中:

Windows 10 Python 3.7.5 谷歌Chrome 80版本及对应的ChromeDriver路径为“C:\Windows” 硒3.141.0

我需要将参数——no-sandbox和——remote-debug -port=9222添加到ChromeOptions对象中,并以管理员用户身份运行Powershell/cmd来运行代码。

下面是相关的代码段:

options = webdriver.ChromeOptions()
options.add_argument('headless')
options.add_argument('--disable-infobars')
options.add_argument('--disable-dev-shm-usage')
options.add_argument('--no-sandbox')
options.add_argument('--remote-debugging-port=9222')
driver = webdriver.Chrome(options=options)

我的问题是php-webdriver中的一个bug。我的代码是:

$chromeOptions = new ChromeOptions();
$chromeOptions->addArguments([
    '--headless',
    '--no-sandbox',
    '--disable-gpu',
    '--disable-dev-shm-usage',
    '--no-proxy-server'
]);
$chromeOptions->setExperimentalOption('detach', true);
$capabilities = DesiredCapabilities::chrome();
$capabilities->setCapability(
    ChromeOptions::CAPABILITY,
    $chromeOptions
);

如你所见,一切都符合其他可能的原因。

但实际上这些功能并没有传递给chromedriver。我不得不改变设置chrome选项功能:

$capabilities->setCapability(
    ChromeOptions::CAPABILITY_W3C, // <<< Have to use W3C capabilities with recent versions of Chromedriver.
    $chromeOptions->toArray() // <<<<< bug in php-webdriver 1.9, object not getting serialized automatically like with the deprecated capability ChromeOptions::CAPABILITY
);

我的设置是:

$ chromedriver --version
ChromeDriver 79.0.3945.36 (3582db32b33893869b8c1339e8f4d9ed1816f143-refs/branch-heads/3945@{#614})
$ java -jar selenium-server-standalone-3.141.59.jar --version
Selenium server version: 3.141.59, revision: e82be7d358

做了一个bug报告和一个PR来修复php-webdriver中的这个bug

https://github.com/php-webdriver/php-webdriver/issues/849

当chromedriver无法找出chrome正在使用的调试端口时,就会发生这种情况。

一个可能的原因是HKEY_CURRENT_USER\Software\Policies\谷歌\Chrome\UserDataDir的开放缺陷

但在我的最后一个病例中,是其他一些不明原因。

幸运的是,手动设置端口号:

final String[] args = { "--remote-debugging-port=9222" };
options.addArguments(args);
WebDriver driver = new ChromeDriver(options);