Visit a page
The simplest script: launch a browser, open a page, read the title.
The smallest thing the library does: start a browser, open a page, read something from it, close up.
Run it
php content/examples/visit.php
Expected output:
Title: Example Domain
This is the fastest smoke check after installation: one browser, one page, one value read from the page.
Read from the DOM
$page->goto('https://example.com');
echo $page->locator('h1')->textContent().PHP_EOL;
if ($page->locator('a')->isVisible()) {
$page->locator('a')->click();
}
Locators auto-wait, and they are strict: a locator that matches more than one element throws rather than picking one for you. Narrow it with first(), last(), or a better selector.
Switch engine
The same script runs on any of the three engines. Only the factory changes:
$context = Playwright::firefox();
// or
$context = Playwright::webkit();
Playwright::safari() is an alias of webkit().
What it demonstrates
Playwright::chromium()creates a browser context.newPage()opens a tab.goto()navigates to a URL.title()reads page state back into PHP.
Go next
- Navigate and click, to follow links and read the resulting URL.
- Take a screenshot, to capture what the page looked like.
- Assert with expect, to check the page instead of printing it.
- Setup, to verify install issues.
Source
visit.php<?php
declare(strict_types=1);
/*
* This file is part of the community-maintained Playwright PHP project.
* It is not affiliated with or endorsed by Microsoft.
*
* (c) 2025-Present - Playwright PHP - https://github.com/playwright-php
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
require_once __DIR__.'/../../vendor/autoload.php';
use Playwright\Playwright;
$context = Playwright::chromium();
$page = $context->newPage();
$page->goto('https://example.com');
echo 'Title: '.$page->title();
$page->close();
$context->close();