Examples

Wait before taking screenshot 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 and save to file

Wait for the page to settle before taking the screenshot.

<?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;
use Facebook\WebDriver\WebDriverExpectedCondition;

$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');

// Wait for late content — here an element that appears after 2 seconds.
$driver->wait(10)->until(
    WebDriverExpectedCondition::visibilityOfElementLocated(WebDriverBy::cssSelector('#delayed-banner'))
);

$driver->takeScreenshot('wait.png');

$driver->quit();

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

That was 27 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.