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


当前回答

我也经历过这个问题,提出的解决方案似乎都不起作用。然后我发现问题是我在WSL版本1上运行,似乎chromedriver与windows浏览器一起工作,而不是与aptitude安装的浏览器。

为了使它与WSL版本1和版本2兼容,我发现(并在debian WSLv2和ubuntu WSLv1中进行了测试,它可以工作),该平台的版本显示的单词Microsoft的第一个大写字母。

所以解决方案是这样的:

    import platform
    from selenium.webdriver.chrome.service import Service as ChromeService
    from seleniumwire import webdriver

    chromedriver = 'chromedriver.exe'
    
    # According to:
    # https://stackoverflow.com/a/71879688/7019069
    # When using WSL v1 the chromedriver.exe of local chrome of windows is used
    # In WSL v2 (updated version) it does work using the installation shown in the README.md)
    # And according to
    # https://github.com/microsoft/WSL/issues/4555
    # It is possible to differentiate the version of WSL by the first uppercase of the platform 
    # version of Microsoft. Therefore only the linux chromedriver is used if that word is matched 
    # in the platform of the driver. 
    if platform.system() == 'Linux' and not re.search(re.escape('Microsoft'), platform.platform()):
        chromedriver = 'chromedriver'

    driver_path = os.path.join(drivers_path, chromedriver)
  service = ChromeService(driver_path)

    driver = webdriver.Chrome(
        service=service,
        options=__get_chrome_options(headless)
    )

其他回答

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");

我最近也面临着同样的问题,经过一些试验和错误,它也适用于我。

必须在最上面:

options.addArguments("--no-sandbox"); //has to be the very first option

BaseSeleniumTests.java

public abstract class BaseSeleniumTests {

    private static final String CHROMEDRIVER_EXE = "chromedriver.exe";
    private static final String IEDRIVER_EXE = "IEDriverServer.exe";
    private static final String FFDRIVER_EXE = "geckodriver.exe";
    protected WebDriver driver;

    @Before
    public void setUp() {
        loadChromeDriver();
    }

    @After
    public void tearDown() {
        if (driver != null) {
            driver.close();
            driver.quit();
        }
    }

    private void loadChromeDriver() {
        ClassLoader classLoader = getClass().getClassLoader();
        String filePath = classLoader.getResource(CHROMEDRIVER_EXE).getFile();
        DesiredCapabilities capabilities = DesiredCapabilities.chrome();
        ChromeDriverService service = new ChromeDriverService.Builder()
                .usingDriverExecutable(new File(filePath))
                .build();
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--no-sandbox"); // Bypass OS security model, MUST BE THE VERY FIRST OPTION
        options.addArguments("--headless");
        options.setExperimentalOption("useAutomationExtension", false);
        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.merge(capabilities);
        this.driver = new ChromeDriver(service, options);
    }

}

GoogleSearchPageTraditionalSeleniumTests.java

@RunWith(SpringRunner.class)
@SpringBootTest
public class GoogleSearchPageTraditionalSeleniumTests extends BaseSeleniumTests {

    @Test
    public void getSearchPage() {
        this.driver.get("https://www.google.com");
        WebElement element = this.driver.findElement(By.name("q"));
        assertNotNull(element);
    }

}

pom.xml

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <scope>test</scope>
        </dependency>
</dependencies>

经验法则

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


另一个

这里是沙盒故事的链接。

这个错误似乎有很多可能的原因。在我们的例子中,错误发生是因为我们有以下两行代码:

System.setProperty("webdriver.chrome.driver", chromeDriverPath);
chromeOptions.setBinary(chromeDriverPath);

它通过去掉第二行来解决。

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

};