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


当前回答

也有同样的问题。我正在谷歌云虚拟机上运行selenium脚本。

options.addArguments("--headless");

上面这句话解决了我的问题。我删除了其他可选参数。我认为其他答案中提到的其余代码行对解决云VM上的问题没有任何影响。

其他回答

在我的例子中,我在Kubernetes环境中不能使用默认的TMPDIR,因为它会用垃圾填满临时目录。

所以我用这个来使用不同的tmpdir:

driver = new ChromeDriver(new ChromeDriverService.Builder()
                    .withEnvironment(ImmutableMap.of("TMPDIR", customTmpPath))
                    .build(), options);

但现在我把所有东西都升级到最新版本,这似乎不再管用了。我需要找到一种新的方法来做这件事。

老问题,但类似的问题几乎把我逼疯了,所以分享我的解决方案。其他的建议都不能解决我的问题。

当我将Docker镜像Chrome从旧版本更新到Chrome 86时,我得到了这个错误。我的设置是不相同的,但我们实例化Chrome通过一个硒webdriver。

解决方案是将选项传递为goog:chromeOptions哈希而不是chromeOptions哈希。我真的不知道这是一个硒,Chrome, Chromedriver,或其他更新,但也许某些可怜的灵魂会在未来的这个答案中找到安慰。

更新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']
    }
  },

};

正如在另一个答案中所述:

此错误消息…这意味着ChromeDriver无法启动/生成一个新的WebBrowser,即Chrome浏览器会话。

Among the possible causes, I would like to mention the fact that, in case you are running an headless Chromium via Xvfb, you might need to export the DISPLAY variable: in my case, I had in place (as recommended) the --disable-dev-shm-usage and --no-sandbox options, everything was running fine, but in a new installation running the latest (at the time of writing) Ubuntu 18.04 this error started to occurr, and the only possible fix was to execute an export DISPLAY=":20" (having previously started Xvfb with Xvfb :20&).

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

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

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

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

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