Chromedriver 132+ not working anymore

Mavi 40 Punti di reputazione
2025-04-05T06:09:48.4033333+00:00

Uno stesso script in PHP che chiama chromedriver tramite libreria phpwebdriver ha smesso di funzionare passando dalla versione chromedriver 131 alle successive quando lo script viene invocato da browser o da terminal come utente www-data.

Lo stesso script lanciato da terminal come root, funziona.

NON FUNZIONA ==> sudo -u www-data php /home/site/wwwroot/test/Test_PhpWebdriver.php

FUNZIONA ==> sudo -u root php /home/site/wwwroot/test/Test_PhpWebdriver.php

Evidentemente c'è un impostazione di sistema che impedisce a Chromedriver di funzionare correttamente.

Grazie per supporto

Desktop virtuale Azure
Desktop virtuale Azure
Un servizio di virtualizzazione di Microsoft desktop e app eseguito in Azure. Precedentemente noto come Desktop virtuale Windows.
37 domande
{count} voti

1 risposta

Ordina per: Più utili
  1. Mavi 40 Punti di reputazione
    2025-04-10T12:20:25.51+00:00

    <?php

    ini_set('display_errors', 1);

    ini_set('display_startup_errors', 1);

    error_reporting(E_ALL);

    require "/home/site/wwwroot/phpApps/autoload.php";

    use Facebook\WebDriver\WebDriverBy;

    use Facebook\WebDriver\WebDriverExpectedCondition;

    use Facebook\WebDriver\Remote\DesiredCapabilities;

    use Facebook\WebDriver\Remote\RemoteWebDriver;

    use Facebook\WebDriver\Chrome\ChromeDriver;

    use Facebook\WebDriver\Chrome\ChromeOptions;

    use Facebook\WebDriver\Chrome\ChromeDevToolsDriver;

    putenv('WEBDRIVER_CHROME_DRIVER=/usr/local/bin/chromedriver');

    $usr_dir = "/home/www-data/chrome/" . uniqid();

    putenv('SE_CHROMEDRIVER=/usr/local/bin/chromedriver');

    putenv('SE_AVOID_BROWSER_DOWNLOAD=true');

    putenv('SE_CHROME_BINARY=/usr/bin/google-chrome');

    putenv('SE_AVOID_STATS=true');

    putenv('SE_OUTPUT=LOGGER');

    putenv('SE_LANGUAGE_BINDING=php');

    putenv("SE_CACHE_PATH=$usr_dir");

    putenv('SE_DRIVER=chromedriver');

    execute " --port=4444 --verbose

    // my-test.php

    // This is where Selenium server 2/3 listens by default. For Selenium 4, Chromedriver or Geckodriver, use http://localhost:4444/

    //$host = 'http://localhost:4444';

    function Chrome_start(&$driver, $options) {

    //global $capabilities;
    
    global $usr_dir, $proxies, $includes_dir;
    
    //var_dump($usr_dir);
    
    $url = ($options['url'] ?? '');
    
    if ($url == '') {
    
        return 'ERROR: Missing URL';
    
    }
    
    $res = "";
    
    $type = ($options['type'] ?? '');
    
    $wait = ($options['wait'] ?? 0);
    
    $init = ($options['init'] ?? false);
    
    $keep = ($options['keep'] ?? false);
    
    $until = ($options['until'] ?? "");
    
    $snapshot = ($options['snapshot'] ?? false);
    
    $cookie_consent = ($options['cookie'] ?? false);
    
    $script_exec = ($options['script'] ?? false);
    
    $proxy = strtolower($options['proxy'] ?? false);
    
    $wait = ($options['wait'] ?? 0);
    
    $useragent = $options['useragent'] ?? false;
    
    $images = ($options['images'] ?? true);
    
    $geo = ($options['geo'] ?? false);
    
    
    
    // google-chrome --no-sandbox --headless --window-size=1920,1080 --start-maximized --profile-directory=Default --disable-dev-shm-usage --disable-blink-features=AutomationControlled --disable-infobars --disable-search-engine-choice-screen
    
    $options = array(
    
    	  '--no-sandbox',
    
    	  '--disable-dev-shm-usage',
    
    	  '--disable-gpu',
    
    	  '--headless',
    
    	  '--user-data-dir='.$usr_dir,
    
    );
    
    var_dump($options);
    
    $chromeOptions = new ChromeOptions();
    
    $chromeOptions->addArguments($options);
    
    
    
    // Create $capabilities and add configuration from ChromeOptions
    
    $capabilities = DesiredCapabilities::chrome();
    
    $capabilities->setCapability(ChromeOptions::CAPABILITY, $chromeOptions);
    
    // Start the browser with $capabilities
    
    // A) When using RemoteWebDriver::create()
    
    //$driver = RemoteWebDriver::create($host, $capabilities, 5000);
    
    
    
    // B) When using ChromeDriver::start to start local Chromedriver
    
    if (is_null($driver)) {
    
        try {
    
            $driver =  ChromeDriver::start($capabilities);
    
        } catch (Exception $e) {
    
            return 'Caught exception (ChromeDriver start): '.$e->getMessage().PHP_EOL;
    
        }
    
    }
    
    
    
    try {
    
        $driver->get($url);
    
    } catch (Exception $e) {
    
        return 'Caught exception (GetUrl): '.$e->getMessage().PHP_EOL;
    
    }
    
    
    
    $res = '';
    
    switch ($type) {
    
        case "json":
    
            $res = $driver->findElement(WebDriverBy::tagName('body'))->getText();
    
    		break;
    
        case "url":
    
            $res = $driver->getCurrentUrl();
    
            break;
    
        case 'script':
    
            $res = $exres;
    
            break;
    
        default:
    
            $res = $driver->getPageSource();
    
            break;
    
    }
    
    return $res;
    

    }

    function Chrome_Stop(&$driver) {

    global $usr_dir;
    
    if (isset($driver) && !is_null($driver)) {
    
        try {
    
            $driver->quit();
    
        } catch (Exception $e) {
    
            echo 'Caught exception (Quit): '.$e->getMessage()."\n";
    
            return false;
    
        }
    
        $driver = NULL;
    
        exec('rm -rf '.$usr_dir);
    
        return true;
    
    } else {
    
        return false;
    
    }
    

    }

    $url = "https://www.google.it/";

    $driver = NULL;

    echo $data = Chrome_start($driver, array(

    'url' => $url,
    

    ));

    ?>


Risposta

Le risposte possono essere contrassegnate come risposte accettate dall'autore della domanda. Ciò consente agli utenti di sapere che la risposta ha risolto il problema dell'autore.