<?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,
));
?>