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


经验法则

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


另一个

这里是沙盒故事的链接。

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

更新:

我能够通过这个问题,现在我能够访问chrome所需的url。

对所提供解决方案的尝试结果:

我尝试了上面提供的所有设置,但我无法解决这个问题

关于问题的解释:

根据我的观察,DevToolsActivePort文件不存在的原因是chrome无法在scoped_dirXXXXX文件夹中找到它的引用。

为解决该问题所采取的步骤

我已经杀死了所有的chrome进程和chrome驱动程序进程。 添加下面的代码来调用chrome System.setProperty(“webdriver.chrome.driver”、“pathto \ \ chromedriver.exe”); ChromeOptions选项=新的ChromeOptions(); 选项。setExperimentalOption(“useAutomationExtension”,假); WebDriver驱动=新的ChromeDriver(选项); driver.get (url);

使用上述步骤,我能够解决这个问题。

谢谢你的回答。

我在python中遇到了同样的问题。以上这些都有帮助。这是我在python中使用的

chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
driver = webdriver.Chrome('/path/to/your_chrome_driver_dir/chromedriver',chrome_options=chrome_options)

我有同样的问题,但在我的情况下,chrome之前安装在用户临时文件夹,之后被重新安装到程序文件。所以这里提供的任何解决方案都帮不了我。但是如果提供了chrome.exe的路径就可以了:

chromeOptions.setBinary("C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe");

我希望这能帮助到一些人=)

我们在jenkins slave (linux机器)上也遇到了同样的问题,并尝试了上述所有选项。

唯一有帮助的就是设定论点

chrome_options.add_argument('--headless')

但是当我们进一步调查时,注意到XVFB屏幕没有启动属性,这导致了这个错误。在我们修复XVFB屏幕后,它解决了这个问题。

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

我的工作环境是:

Selenium Version 3.12.0
ChromeDriver Version v2.40
Chrome 68 level

之前:

后:

没有解决方法对我有效。但这里有一个变通办法:

maxcounter=5
for counter in range(maxcounter):
    try:           
        driver = webdriver.Chrome(chrome_options=options,
                          service_log_path=logfile,
                          service_args=["--verbose", "--log-path=%s" % logfile])
        break
    except WebDriverException as e:
        print("RETRYING INITIALIZATION OF WEBDRIVER! Error: %s" % str(e))
        time.sleep(10)
        if counter==maxcounter-1:
            raise WebDriverException("Maximum number of selenium-firefox-webdriver-retries exceeded.")

在我的例子中,当我试图使用我的默认用户配置文件时,它发生了:

...
options.addArguments("user-data-dir=D:\\MyHomeDirectory\\Google\\Chrome\\User Data");
...

这触发chrome重用已经在后台运行的进程,以这种方式,由chromedriver.exe启动的进程简单地结束。

解决方法:关闭所有在后台运行的chrome.exe进程。

在我的情况下,我试图用chrome浏览器在Windows操作系统上创建一个可运行的jar,并希望在unix盒子上运行CentOs的无头模式。我把二进制文件指向一个我已经下载并打包在我的套件中的驱动程序。对我来说,这个问题继续发生,不管添加以下内容:

ChromeOptions options = new ChromeOptions();
options.addArguments("--headless");
options.addArguments("--no-sandbox");
System.setProperty("webdriver.chrome.args", "--disable-logging");
System.setProperty("webdriver.chrome.silentOutput", "true");
options.setBinary("/pointing/downloaded/driver/path/in/automationsuite");
options.addArguments("--disable-dev-shm-usage"); // overcome limited resource problems
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("window-size=1024,768"); // Bypass OS security model
options.addArguments("--log-level=3"); // set log level
options.addArguments("--silent");//
options.setCapability("chrome.verbose", false); //disable logging
driver = new ChromeDriver(options);

我尝试过并为我工作的解决方案是,在主机VM/Unix盒子上下载chrome及其工具,在自动化套件中安装并指向此二进制文件,然后就完成了!它是有效的:)

下载命令:

wget https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm

安装命令:

sudo yum install -y ./google-chrome-stable_current_*.rpm

更新套件包含以下google-chrome二进制路径:

options.setBinary("/opt/google/chrome/google-chrome");

和. .它的工作原理!

由于这是针对这类错误的最活跃的消息,所以我想提一下我的解决方案(在花了几个小时来修复这个问题之后)。

On Ubuntu 18.04, using Chrome 70 and Chromedriver 2.44, and Python3 I kept getting the same DevToolsActivePort error, even when I disabled all options listed above. The chromedriver log file as well as ps showed that the chromedriver I set in chrome_options.binary_location was running, but it always gave DevToolsActivePort error. When I removed chrome_options.binary_location='....' and add it to webdriver creation, I get it working fine. webdriver.Chrome('/path to ... /chromedriver',chrome_options=chrome_options)

感谢大家的意见,让我理解并解决了这个问题。

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

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

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

我在与jenkins服务器集成时也遇到了这个问题,我使用了jenkin工作的根用户,当我将用户更改为其他用户时,这个问题得到了解决。我不确定为什么根用户会出现这个错误。

谷歌Chrome浏览器版本71.0 ChromeDriver版本2.45 CentOS7版本1.153

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

};

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

必须在最上面:

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>

我在Docker容器中通过Behat/Mink和Selenium运行Chrome时也遇到了同样的问题。经过一番折腾,我得出了如下结论。Yml提供上面提到的开关。请注意,要使它成功运行,所有这些都是必需的。

default:
    extensions:
        Behat\MinkExtension:
            base_url: https://my.app/
            default_session: selenium2
            selenium2:
                browser: chrome
                capabilities:
                    extra_capabilities:
                        chromeOptions:
                            args:
                                - "headless"
                                - "no-sandbox"
                                - "disable-dev-shm-usage"

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

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

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

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

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

此错误消息…这意味着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&).

你可以得到这个错误简单地传递坏的参数Chrome。例如,如果我将“headless”作为参数传递给c# ChromeDriver,它会很好地启动。如果我犯了一个错误,使用了错误的语法,“——headless”,我得到DevToolsActivePort文件不存在错误。

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

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

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

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

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

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

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)

我遇到了同样的问题,我使用UBUNTU, PYTHON和OPERA浏览器。在我的例子中,这个问题是因为我有一个过时的operadriver版本。

解决方案: 1. 确保您安装了最新的opera浏览器版本(不要使用opera beta或opera developer),为此请到opera官方网站并从那里下载最新的opera_stable版本。

安装最新的opera驱动程序(如果你已经安装了一个opera驱动程序,你必须先使用sudo rm删除它…)

wget https://github.com/operasoftware/operachromiumdriver/releases/download/v.80.0.3987.100/operadriver_linux64.zip

   unzip operadriver_linux64.zip
   sudo mv operadriver /usr/bin/operadriver
   sudo chown root:root /usr/bin/operadriver
   sudo chmod +x /usr/bin/operadriver

在我的情况下,最新的是80.0.3987,你可以看到

此外,我还安装了chromedriver(但因为我在测试之前做了,我不知道这是需要的),为了安装chromedriver,遵循上一步的步骤 好好享受吧,谢谢我!

示例硒代码

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Opera()
driver.get("http://www.python.org")
assert "Python" in driver.title
elem = driver.find_element_by_name("q")
elem.clear()
elem.send_keys("pycon")
elem.send_keys(Keys.RETURN)
assert "No results found." not in driver.page_source
driver.quit()

我在Ubuntu 20和Python Selenium上遇到了这个问题,我首先单独下载了chromedriver,然后使用sudo apt install chromium-browser,尽管它们是同一个版本,但这种情况一直发生。

我的修复是使用提供的chrome驱动程序,附带的回购包位于

/snap/bin/chromium.chromedriver

driver = webdriver.Chrome(chrome_options=options, executable_path='/snap/bin/chromium.chromedriver')

我的端口号错了。检查启动Selenium服务器时的端口号是否与脚本中的端口号相同。

TL;DR:如果你使用VirtualBox共享文件夹,不要在那里创建Chrome配置文件!


我在Debian 10下遇到了这个错误,但在Ubuntu 18.04下没有发生。

在我的Selenium测试中,我想安装一个扩展,并使用以下Chrome选项:

chromeOptions.addArguments(
  `load-extension=${this.extensionDir}`,
  `user-data-dir=${this.profileDir}`,
  `disable-gpu`,
  `no-sandbox`,
  `disable-setuid-sandbox`,
  `disable-dev-shm-usage`,
);

问题是,我试图在一个非标准目录下创建一个Chrome配置文件,这是VirtualBox共享文件夹的一部分。尽管使用的是完全相同版本的Chrome和Chromedriver,但在Debian下却无法运行。

解决方案是在其他地方选择一个概要目录(例如~/chrome-profile)。

我用Jenkins在Ubuntu 18 LTS linux上运行selenium测试。我有这个错误,直到我像这样添加了参数'headless'(以及其他一些参数):

ChromeOptions options = new ChromeOptions();
options.addArguments("headless"); // headless -> no browser window. needed for jenkins
options.addArguments("disable-infobars"); // disabling infobars
options.addArguments("--disable-extensions"); // disabling extensions
options.addArguments("--disable-dev-shm-usage"); // overcome limited resource problems
options.addArguments("--no-sandbox"); // Bypass OS security model
ChromeDriver driver = new ChromeDriver(options);

driver.get("www.google.com");

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

options.addArguments("--headless");

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

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

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

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

我使用铬,但我已经创建了一个shell脚本称为铬,只是为了便于我从dmenu打开浏览器。

#!/bin/bash

/usr/bin/chromium

Chrome驱动程序在PATH中寻找Chrome并执行。结果我得到了同样的错误。

org.openqa.selenium.WebDriverException: unknown error: Chrome failed to start: exited normally.
  (unknown error: DevToolsActivePort file doesn't exist)
  (The process started from chrome location /home/s1n7ax/.local/share/s1n7ax/bin/chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:17:03'
System info: host: 's1n7ax', ip: '127.0.1.16', os.name: 'Linux', os.arch: 'amd64', os.version: '5.4.70-1-lts', java.version: '11.0.8'
Driver info: driver.version: ChromeDriver
remote stacktrace: #0 0x56030c96dd99 <unknown>

我只是删除了shell脚本,并添加了一个到chromium的软链接。现在一切正常。

我的问题是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

我也有同样的错误,我发现原因是因为我的电脑磁盘已满。删除了一些不必要的文件后,错误就消失了。

对于Ubuntu 20,它确实帮助我使用我的系统铬驱动程序,而不是下载的那个:

# chromium which
/snap/bin/chromium

driver = webdriver.Chrome('/snap/bin/chromium.chromedriver',
                          options=chrome_options)

对于下载的web驱动程序来说,它看起来需要远程调试端口——remote-debugging-port=9222来设置,就像其中一个答案(由Soheil Pourbafrani):

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--remote-debugging-port=9222")
driver = webdriver.Chrome('<path_to>/chromedriver', options=chrome_options)

在我的情况下,这是ubuntu服务器上的CI代理帐户的问题,我用自定义——user-data-dir解决了这个问题

chrome_options.add_argument(“——user-data-dir = ~ / config / google chrome”)

CI代理使用的我的帐户没有必要的权限,有趣的是一切都在根帐户上工作

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

chrome_options = Options()
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument('--disable-dev-shm-usage')
chrome_options.add_argument('--profile-directory=Default')
chrome_options.add_argument('--user-data-dir=~/.config/google-chrome')

driver = webdriver.Chrome(options=chrome_options)
url = 'https://www.google.com'
driver.get(url) 
get_url = driver.current_url 
print(get_url)

在我的情况下,当我改变google-chrome和chromedriver版本时,错误被修复了:)

#google-chrome version
[root@localhost ~]# /usr/bin/google-chrome --version
Google Chrome 83.0.4103.106 

#chromedriver version
[root@localhost ~]# /usr/local/bin/chromedriver -v
ChromeDriver 83.0.4103.14 (be04594a2b8411758b860104bc0a1033417178be-refs/branch-heads/4103@{#119})

Ps:硒版本为3.9.1

核心的答案 正确的解决方案:不使用root用户运行Chrome二进制 更多细节请参考另一个帖子的回答:未知错误:DevToolsActivePort文件不存在

有很多可能的原因导致RESPONSE InitSession ERROR unknown ERROR: DevToolsActivePort文件不存在错误消息(正如我们可以从这个问题的答案数量中看到的)。因此,让我们更深入地解释这个错误消息的确切含义。

根据chromedriver源代码,消息是在ParseDevToolsActivePortFile方法中创建的。此方法在启动chrome进程后从循环中调用。

在循环中,驱动程序检查chrome进程是否仍在运行,如果ParseDevToolsActivePortFile文件已经由chrome创建。这个循环有一个硬编码的60s超时。

我认为这条信息可能有两个原因:

Chrome在启动过程中非常慢——例如由于缺乏系统资源——主要是CPU或内存。在这种情况下,它可以发生,有时铬管理开始在时间限制,有时不是。 还有一个问题,防止chrome启动-缺失或破碎的依赖,错误的配置等。在这种情况下,这个错误消息并没有真正的帮助,您应该找到另一个日志消息来解释失败的真正原因。

日期9/16/2021

在docker托管的ubuntu容器中使用python在本地运行chrome和selenium,一切都很好。当试图运行从Jenkins上面的错误返回WebDriverException:未知错误:DevToolsActivePort

环境:

-Ubuntu21.04在docker容器内,支持RDP访问。

chrome版本:93

解决方案: 在启动浏览器的python文件中,我必须使用以下行设置DISPLAY环境变量:

import os

os.environ['DISPLAY'] = ':10.0'
#DISPLAY_VAR = os.environ.get('DISPLAY')
#print("DISPLAY_VAR:", DISPLAY_VAR)

我被这个问题困扰了很长一段时间,最后通过添加一个额外的选项来解决它:

options.addArguments(”——crash-dumps-dir = / tmp”)

如果添加参数/选项不能解决问题,那么可能是/tmp目录的权限问题。

确保执行Chrome的用户有权限在/tmp文件夹下创建文件夹/files。这就是我案例中的解

我也经历过这个问题,提出的解决方案似乎都不起作用。然后我发现问题是我在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)
    )

在尝试在Linux服务器上运行selenium时遇到了同样的问题,尝试降级你的chrome版本,它对我有用吗

从这里选择版本

http://170.210.201.179/linux/chrome/deb/pool/main/g/google-chrome-stable/

我遇到了同样的问题,在我的情况下,Linux系统中有两个不同的常见用户userera和userB。 userA第一次运行selinium程序成功启动chrome浏览器,当到达userB时,出现DevToolsActivePort文件不存在的错误。

我尝试了——remote-debug -port=9222选项,但它导致了一个新的异常: selenium.common.exceptions.WebDriverException: Message: chrome不可达

I run google-chome目录,看到以下错误: mkdir /tmp/Crashpad/new: Permission denied (13)

我搜索了这个问题,得到了这个: https://johncylee.github.io/2022/05/14/chrome-headless-%E6%A8%A1%E5%BC%8F%E4%B8%8B-devtoolsactiveport-file-doesn-t-exist-%E5%95%8F%E9%A1%8C/

chrome_options.add_argument(f"--crash-dumps-dir={os.path.expanduser('~/tmp/Crashpad')}")

感谢@johncylee。

我知道这是一个老问题,它已经有很多答案。然而,我遇到了这个问题,碰到了这个线程,提出的解决方案都没有帮助。在花了几天(!)之后,我终于找到了一个解决方案:

我的问题是,我在一台装有M1芯片的MacBook上使用了硒/独立铬图像。在切换到seleniarm/standalone-chromium之后,一切终于开始工作了。

对我来说,问题在于systemD服务文件。我将shell环境传递为venv

Environment="proejct/venv/bin"

当我添加/bin:/usr/bin到Environment:

Environment="proejct/venv/bin:/usr/bin:/bin"

最后,它成功了。