Navigate and click

Navigate, click links, read the resulting URL.

This example opens example.com, clicks the links on the page, and prints the URL after each navigation.

Use it when you want to see the smallest navigation loop: open a page, interact with a link, then read browser state back from PHP.

Run it

bash
php content/examples/navigate.php

Expected output:

text
URL: https://example.com/
New URL: https://www.iana.org/help/example-domains
New URL: https://www.iana.org/domains/reserved

The exact URLs may change if example.com changes its links, but the important part is that each click waits for the page and url() returns the browser's current location.

What it demonstrates

  • goto() navigates a page to a URL.
  • locator('a') finds links on the current page.
  • click() waits for the link to be actionable before clicking.
  • url() reads the current page URL after navigation.

For real tests, prefer a visible assertion after the click instead of only printing the URL.

Go next

Source

navigate.php
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\PlaywrightFactory;

$client = PlaywrightFactory::create();
$browser = $client->chromium()->launch();
$page = $browser->newPage();

$page->goto('https://example.com');
echo 'URL: '.$page->url()."\n";

$page->locator('a')->click();
echo 'New URL: '.$page->url()."\n";

$page->locator('a')->last()->click();
echo 'New URL: '.$page->url()."\n";

$browser->close();
$client->close();