在Selenium中运行无头Chrome会引发“未知错误:DevToolsActivePort文件不存在”

时间:2018-08-15 13:35:59

标签: java google-chrome selenium selenium-webdriver google-chrome-headless

当尝试在Selenium中运行无头Chrome时,我遵循this示例,并编写了以下代码:

  ChromeOptions chromeOptions = new ChromeOptions();
  chromeOptions.addArguments("--headless");
  driver = new ChromeDriver(chromeOptions);

但是在运行代码时出现此错误:

  

驱动程序可执行文件的路径必须由webdriver.chrome.driver系统属性设置

所以我将此添加到了代码中:

chromeOptions.setBinary(System.getProperty("webdriver.chrome.driver"));

然后将参数发送到程序: webdriver.chrome.driver=C:\\chromedriver_win32\\chromedriver.exe

但后来我明白了:

Starting ChromeDriver 2.41.578737 (49da6702b16031c40d63e5618de03a32ff6c197e) on port 47772
Only local connections are allowed.
Starting ChromeDriver 2.41.578737 (49da6702b16031c40d63e5618de03a32ff6c197e) on port 9515
Only local connections are allowed.
Invalid --log-level value.
Unable to initialize logging. Exiting...

所以我添加了日志记录设置:

 System.setProperty("webdriver.chrome.logfile", "C:\\chromedriver_win32\\chromedriver.log");
 System.setProperty("webdriver.chrome.verboseLogging", "true");
 chromeOptions.setCapability("--log-level", "WARNING");

但这似乎无济于事,因为错误是:

Starting ChromeDriver 2.41.578737 (49da6702b16031c40d63e5618de03a32ff6c197e) on port 43365
Only local connections are allowed.
Starting ChromeDriver 2.41.578737 (49da6702b16031c40d63e5618de03a32ff6c197e) on port 9515
Only local connections are allowed.
Invalid --log-level value.
Unable to initialize logging. Exiting...
Caused by: org.openqa.selenium.WebDriverException: unknown error: Chrome failed to start: was killed
(unknown error: DevToolsActivePort file doesn't exist)
(The process started from chrome location C:\\chromedriver_win32\\chromedriver.exe is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
(Driver info: chromedriver=2.41.578737 (49da6702b16031c40d63e5618de03a32ff6c197e),platform=Windows NT 10.0.16299 x86_64) (WARNING: The server did not provide any stacktrace information)

所以看来当前的问题是unknown error: DevToolsActivePort file doesn't exist

1 个答案:

答案 0 :(得分:0)

要设置Webdriver的位置,您可以使用此

System.setProperty("webdriver.chrome.driver","location-driver"); //before 'new ChromeDriver...'

无头运行它是您尝试过的

ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--headless");
driver = new ChromeDriver(chromeOptions);