Examples

Screenshot of a particular element with PHP and Selenium

Install php-webdriver and chromedriver

composer require php-webdriver/webdriver
chromedriver --port=9515

php-webdriver has no Selenium Manager, so it needs a running chromedriver matching your Chrome version. Start it in another terminal and leave it running while the script runs — the examples connect to port 9515, chromedriver’s default, and passing --port=9515 just makes that explicit.

Take a screenshot of the element and save to file

<?php
require __DIR__ . '/vendor/autoload.php';

use Facebook\WebDriver\Chrome\ChromeOptions;
use Facebook\WebDriver\Remote\DesiredCapabilities;
use Facebook\WebDriver\Remote\RemoteWebDriver;
use Facebook\WebDriver\WebDriverBy;

$options = new ChromeOptions();
$options->addArguments(['--headless=new']);

$capabilities = DesiredCapabilities::chrome();
$capabilities->setCapability(ChromeOptions::CAPABILITY, $options);

$driver = RemoteWebDriver::create('http://localhost:9515', $capabilities);

$driver->get('https://www.htmltoimage.com/demo');

$element = $driver->findElement(WebDriverBy::cssSelector('#example-card'));
$element->takeElementScreenshot('element.png');

$driver->quit();

Find out how to take a full-page screenshot with PHP and Selenium.

That was 22 lines of code — plus a browser to run in production

Keeping headless browsers fed, patched and scaled is a job in itself. We built Urlbox so the same screenshot is one API call:

curl "https://api.urlbox.com/v1/YOUR_API_KEY/png?url=https://example.com"

Try the Urlbox screenshot API — free trial, no browser babysitting.