Chrome 无法启动:异常退出。(未知错误:DevToolsActivePort 文件不存在)

Chrome failed to start: exited abnormally. (unknown error: DevToolsActivePort file doesn't exist)

提问人:Boburjon 提问时间:7/2/2023 更新时间:7/2/2023 访问量:140

问:

我正在尝试解析(抓取)网站,这是我的代码:

<?php
    ini_set('display_errors', 1);
    require_once 'vendor/autoload.php';
    
    use Facebook\WebDriver\Remote\{DesiredCapabilities, RemoteWebDriver};
    use Facebook\WebDriver\WebDriverBy;
    use Facebook\WebDriver\WebDriverExpectedCondition;
    use Facebook\WebDriver\Exception\NoSuchElementException;
    use Facebook\WebDriver\Exception\TimeOutException;
    
    // Set up ChromeDriver options
    $options = new \Facebook\WebDriver\Chrome\ChromeOptions();
    $options->setBinary('/usr/bin/chromium-browser'); // Update with the correct path
    
    $options->addArguments(['--headless']); // Run ChromeDriver in headless mode (optional)
    
    // Start ChromeDriver with the desired capabilities
    $capabilities = DesiredCapabilities::chrome();
    $capabilities->setCapability(\Facebook\WebDriver\Chrome\ChromeOptions::CAPABILITY, $options);
    
    // Create a new instance of RemoteWebDriver with increased timeout
   $driver = RemoteWebDriver::create('http://<vps-ip>:9516/', $capabilities, 60000, '/usr/bin/chromedriver');
    
    // Navigate to the desired website
    $driver->get('<url-for-parsing>');
    
    try {
        // Wait until the element is visible
        $wait = new \Facebook\WebDriver\WebDriverWait($driver, 30);
        $element = $wait->until(
            WebDriverExpectedCondition::visibilityOfElementLocated(WebDriverBy::xpath('//div[contains(@data-testid, "profile-snippet-name")]'))
        );
    
        // Extract the text content of the div element
        $divText = $element->getText();
    
        // Print the extracted text
        echo $divText;
    } catch (NoSuchElementException $e) {
        echo "Element not found: " . $e->getMessage();
    } catch (TimeOutException $e) {
        echo "Timed out waiting for element: " . $e->getMessage();
    } finally {
        // Quit the driver
        $driver->quit();
    }
?>

我收到此错误:

Fatal error: Uncaught Facebook\WebDriver\Exception\UnknownServerException: unknown error: 
Chrome failed to start: exited abnormally. (unknown error: DevToolsActivePort file doesn't
exist) (The process started from chrome location /usr/bin/chromium-browser is no longer 
running, so ChromeDriver is assuming that Chrome has crashed.) (Driver info: 
chromedriver=114.0.5735.90 (386bc09e8f4f2e025eddae123f36f6263096ae49-refs/branch-
heads/5735@{#1052}),platform=Linux 5.15.0-75-generic x86_64) in 
/home/k/user/user.domain/public_html/app/Http/Controllers/getName/vendor/facebook/webdriver/
lib/Exception/WebDriverException.php:121 Stack trace: #0 
/home/kuser/user.domain/public_html/app/Http/Controllers/getName/vendor/facebook/webdriver/l
ib/Remote/HttpCommandExecutor.php(353): 
Facebook\WebDriver\Exception\WebDriverException::throwException(13, 'unknown error: ...', 
Array) #1 
/home/k/user/user.domain/public_html/app/Http/Controllers/getName/vendor/facebook/webdriver/
lib/Remote/RemoteWebDriver.php(100): Facebook\WebDriver\Remote\HttpCommandExecutor-
>execute(Object(Facebook\WebDriver\Remote\WebDriverCommand)) #2 
/home/k/user/user.domain/public_html/app/Http/Controllers/getName/index.php(22): 
Facebook\WebDriver\Remote\RemoteWebDriver::create('http://<vps-ip>', Array, 60000, 
'/usr/bin/chrome...') #3 {main} thrown in 
/home/k/user/user.domain/public_html/app/Http/Controllers/getName/vendor/facebook/webdriver/
lib/Exception/WebDriverException.php on line 121

注意

  • 我在我的 PC(localhost)上尝试了这段代码,它起作用了,但是当我在托管上尝试它时,它不起作用,我得到了 VPS (ubuntu) 服务器,我安装了 chromium(也是 google-chrome)浏览器和 chromedriver。我正在尝试使用它,但我收到此错误。

  • 所有路径(如:)都正确给出/usr/bin/chromedriver

  • 我显示的代码未在 VPSM 中本地化,它们位于托管中

  • 这是我第一次在PHP中使用,所以请详细回答chromedriver

运行 ChromeDriver 时得到什么

root@**:~# chromedriver --port=9516 --whitelisted-ips=""
Starting ChromeDriver 114.0.5735.90 (386bc09e8f4f2e025eddae123f36f6263096ae49-refs/branch-heads/5735@{#1052}) on port 9516
All remote connections are allowed. Use an allowlist instead!
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.
php selenium-chromedriver 屏幕抓取

评论


答: 暂无答案