Selenium 4 使用 ChromeDriver 在运行多个驱动程序时崩溃

huangapple go评论136阅读模式
英文:

Selenium 4 with ChromeDriver crashing when running multiple drivers

问题

在运行以下简单示例时,针对 Selenium 4.11,在循环的某个迭代中,程序将无法获取新的 ChromeDriver 实例,并显示以下消息:

Exception in thread "main" org.openqa.selenium.SessionNotCreatedException: Could not start a new session. Response code 500. Message: unknown error: DevToolsActivePort file doesn't exist

这是在 Windows 上的情况,因此这里的常见建议不适用(但我还是尝试了):https://stackoverflow.com/questions/50642308/webdriverexception-unknown-error-devtoolsactiveport-file-doesnt-exist-while-t

以下是一个非常简单的测试程序,它尝试在循环中多次检查维基百科的标志。

有趣的是,这似乎只是与 ChromeDriver 有关的问题;如果我改用 EdgeDriver(请参见已注释的代码行),一切都很正常。

// ...

失败的异常信息如下:

Exception in thread "main" org.openqa.selenium.SessionNotCreatedException: Could not start a new session. Response code 500. Message: unknown error: DevToolsActivePort file doesn't exist 
Host info: host: 'REDACTED', ip: '192.168.1.124'
Build info: version: '4.11.0', revision: '040bc5406b'
System info: os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_342'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Command: [null, newSession {capabilities=[Capabilities {browserName: chrome, goog:chromeOptions: {args: [--remote-allow-origins=*], extensions: []}}]}]
	at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:140)
	// ...

来自一个样本运行的输出:

Initializing ChromeDriverService...
Driver: C:\Users\REDACTED\.cache\selenium\chromedriver\win645.0.5790.170\chromedriver.exe
DriverService initialized - chromedriver, webdriver.chrome.driver
Iteration 0: getting a webdriver
Iteration 0: running test against webdriver
Iteration 0: logo src = https://www.wikipedia.org/portal/wikipedia.org/assets/img/Wikipedia-logo-v2.png
Iteration 1: getting a webdriver
Iteration 1: running test against webdriver
Iteration 1: logo src = https://www.wikipedia.org/portal/wikipedia.org/assets/img/Wikipedia-logo-v2.png
// ...

这是在使用 Coretto 1.8.0_342 的 Windows 10 上,但切换到更新的 JDK 没有改变任何事情。Chrome 版本是 115.0.5790.171,在撰写本文时是最新的稳定版本。

背景信息:我正在努力将 Selenium 3 升级到 4.11,并已将我的问题简化为上述简单的 PoC 代码。我愿意相信这可能是用户错误(事实上,如果是这样,我会很高兴!),但我看不出我做错了什么。

英文:

When running the below simple example against Selenium 4.11, at some iteration in the loop the program will fail to get a new ChromeDriver instance with the following message:

Exception in thread "main" org.openqa.selenium.SessionNotCreatedException: Could not start a new session. Response code 500. Message: unknown error: DevToolsActivePort file doesn't exist

This is on Windows, so the common suggestion here doesn’t apply (but I did try it anyway): https://stackoverflow.com/questions/50642308/webdriverexception-unknown-error-devtoolsactiveport-file-doesnt-exist-while-t

Below is a very simple test program that tries to check the Wikipedia logo multiple times in a loop.

Interestingly, this only seems to be a problem with ChromeDriver; if I use the EdgeDriver instead (see commented-out lines) all is well.

package example;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeDriverService;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.edge.EdgeDriverService;
import org.openqa.selenium.edge.EdgeOptions;
import org.openqa.selenium.manager.SeleniumManager;
import org.openqa.selenium.manager.SeleniumManagerOutput;

import java.io.File;
import java.io.IOException;
import java.time.Duration;
import java.time.temporal.ChronoUnit;

public class SimpleTest
{
  public static void main(String[] args) throws IOException
  {
    System.out.println("Initializing ChromeDriverService...");

    ChromeOptions options = new ChromeOptions();
//    EdgeOptions options = new EdgeOptions();

    SeleniumManagerOutput.Result result = SeleniumManager.getInstance().getDriverPath(options, false);
    String driverFile = result.getDriverPath();
    System.out.println("Driver: " + driverFile);

    ChromeDriverService driverService = new ChromeDriverService.Builder()
            .usingDriverExecutable(new File(driverFile))
            .withLogFile(new File("C:\\do\\chromedriver.log"))
            .usingAnyFreePort()
            .build();

//    EdgeDriverService driverService = new EdgeDriverService.Builder()
//            .usingDriverExecutable(new File(driverFile))
//            .usingAnyFreePort()
//            .build();

    driverService.start();
    System.out.println("DriverService initialized - " + driverService.getDriverName() + ", " + driverService.getDriverProperty());

    for (int i = 0; i < 20; i++)
    {
      System.out.println("Iteration " + i + ": getting a webdriver");
      ChromeDriver driver = new ChromeDriver(driverService, new ChromeOptions());
//      EdgeDriver driver = new EdgeDriver(driverService, new EdgeOptions());
      driver.manage().timeouts().implicitlyWait(Duration.of(3, ChronoUnit.SECONDS));

      System.out.println("Iteration " + i + ": running test against webdriver");
      driver.get("https://www.wikipedia.com");
      WebElement logoImage = driver.findElement(By.cssSelector(".central-featured-logo")); // the main logo
      System.out.println("Iteration " + i + ": logo src = " + logoImage.getAttribute("src"));
      driver.close();
    }
    driverService.close();
  }

}

It varies which particular iteration falls, but the furthest I’ve managed to get it to run was iteration 7.

The failure Exception is:

Exception in thread "main" org.openqa.selenium.SessionNotCreatedException: Could not start a new session. Response code 500. Message: unknown error: DevToolsActivePort file doesn't exist 
Host info: host: 'REDACTED', ip: '192.168.1.124'
Build info: version: '4.11.0', revision: '040bc5406b'
System info: os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_342'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Command: [null, newSession {capabilities=[Capabilities {browserName: chrome, goog:chromeOptions: {args: [--remote-allow-origins=*], extensions: []}}]}]
	at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:140)
	at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:96)
	at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:68)
	at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:163)
	at org.openqa.selenium.remote.service.DriverCommandExecutor.invokeExecute(DriverCommandExecutor.java:196)
	at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:171)
	at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:518)
	at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:232)
	at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:159)
	at org.openqa.selenium.chromium.ChromiumDriver.<init>(ChromiumDriver.java:108)
	at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:88)
	at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:83)
	at example.SimpleTest.main(SimpleTest.java:46)

Output from a sample run:

Initializing ChromeDriverService...
Driver: C:\Users\REDACTED\.cache\selenium\chromedriver\win645.0.5790.170\chromedriver.exe
DriverService initialized - chromedriver, webdriver.chrome.driver
Iteration 0: getting a webdriver
Iteration 0: running test against webdriver
Iteration 0: logo src = https://www.wikipedia.org/portal/wikipedia.org/assets/img/Wikipedia-logo-v2.png
Iteration 1: getting a webdriver
Iteration 1: running test against webdriver
Iteration 1: logo src = https://www.wikipedia.org/portal/wikipedia.org/assets/img/Wikipedia-logo-v2.png
Iteration 2: getting a webdriver
Iteration 2: running test against webdriver
Iteration 2: logo src = https://www.wikipedia.org/portal/wikipedia.org/assets/img/Wikipedia-logo-v2.png
Iteration 3: getting a webdriver
Iteration 3: running test against webdriver
Iteration 3: logo src = https://www.wikipedia.org/portal/wikipedia.org/assets/img/Wikipedia-logo-v2.png
Iteration 4: getting a webdriver
Iteration 4: running test against webdriver
Iteration 4: logo src = https://www.wikipedia.org/portal/wikipedia.org/assets/img/Wikipedia-logo-v2.png
Iteration 5: running test against webdriver
Iteration 5: logo src = https://www.wikipedia.org/portal/wikipedia.org/assets/img/Wikipedia-logo-v2.png
Iteration 5: getting a webdriver
SLF4J: No SLF4J providers were found.
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See https://www.slf4j.org/codes.html#noProviders for further details.
Exception in thread "main" org.openqa.selenium.SessionNotCreatedException: Could not start a new session. Response code 500. Message: unknown error: DevToolsActivePort file doesn't exist 
Host info: host: 'REDACTED', ip: '192.168.1.124'
Build info: version: '4.11.0', revision: '040bc5406b'
System info: os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_342'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Command: [null, newSession {capabilities=[Capabilities {browserName: chrome, goog:chromeOptions: {args: [--remote-allow-origins=*], extensions: []}}]}]
	at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:140)
	at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:96)
	at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:68)
	at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:163)
	at org.openqa.selenium.remote.service.DriverCommandExecutor.invokeExecute(DriverCommandExecutor.java:196)
	at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:171)
	at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:518)
	at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:232)
	at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:159)
	at org.openqa.selenium.chromium.ChromiumDriver.<init>(ChromiumDriver.java:108)
	at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:88)
	at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:83)
	at example.SimpleTest.main(SimpleTest.java:46)

This is on Windows 10 using Coretto 1.8.0_342, but switching to a newer JDK has made no difference. Chrome is on 115.0.5790.171 which at the time of writing is the most recent stable version.

For context: I’m battling with a Selenium 3 to 4.11 upgrade, and have reduced my issue to the simple PoC code above. I’m open to this being a user error (in fact I’d be glad if it is!) but I can’t see what I’ve done wrong

答案1

得分: 0

是的,使用ChromeDriver运行超过3个线程可能会出现一些问题。

可能的问题出在 ChromeDriver v115.0.5790.171 上。

解决方案

尝试下载并使用以下其中之一而不是 ChromeDriver v115.0.5790.171

英文:

Yes, running more than 3 threads with ChromeDriver have some issues.

Possibly the issue is with ChromeDriver v115.0.5790.171


Solution

Instead of ChromeDriver v115.0.5790.171 try to download and use either among:

huangapple
  • 本文由 发表于 2023年8月10日 10:22:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76872272.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定