World’s largest virtual agentic engineering & quality conference

WHENAUG 19-21
WHEREVirtual · Global
WATCH NOW
Testing

Selenium PHP Tutorial: A Comprehensive Guide, with Examples & Best Practices

You can learn automation testing with PHP with this guide which focuses on a step-by-step approach for tutorial on automation with Selenium PHP with examples! Go get the most of it!

Author

Devansh Bhardwaj

Author

Author

Himanshu Sheth

Reviewer

Published on: February 7, 2023

Last Updated on: July 17, 2026

It is common to use more than one programming language for automation testing. PHP is a go-to language for developers building web applications, with advantages over other languages like Java and Python. While PHP ships with fewer testing libraries than Java or Python, open source projects close that gap, and Selenium gives PHP a mature path to browser automation.

Selenium with PHP runs through php-webdriver/php-webdriver, the official, community-driven WebDriver library for PHP (formerly published as facebook/php-webdriver). In this Selenium PHP tutorial you will install the library with Composer, write your first RemoteWebDriver script, and see how PHPUnit, Codeception, and Behat fit into a real test suite.

How to Install and Set Up Selenium with PHP

Setting up Selenium with PHP takes three pieces: the php-webdriver/php-webdriver library, a browser driver such as ChromeDriver, and a running driver server the library talks to over the W3C WebDriver protocol. The steps below take you from an empty project to a working automation setup.

Prerequisites

You need PHP 7.3 or later, Composer (the PHP dependency manager) available on your PATH, and Google Chrome installed locally. Composer pulls the WebDriver client from Packagist, the official PHP package registry.

  • Install the php-webdriver/php-webdriver library with Composer, the official community-driven WebDriver client for PHP (formerly published as facebook/php-webdriver).
  • Download the ChromeDriver build that matches your installed Chrome version and place it on your PATH.
  • Start the driver server so the library has an endpoint to send commands to.

Step 1: Install php-webdriver with Composer

Run the command below in your project root. Composer resolves the package from Packagist and writes an autoloader you include in every script.

composer require php-webdriver/webdriver

Step 2: Download ChromeDriver

ChromeDriver is the browser-specific driver Selenium uses to control Chrome. Download the version that matches your Chrome build, unzip it, and confirm it runs from the command line. For Firefox you would use geckodriver instead, and the same pattern applies to other browsers.

Step 3: Start the driver server on port 9515

Launch ChromeDriver directly and it listens on port 9515 by default. Your PHP script points RemoteWebDriver at http://localhost:9515. If you prefer a full Selenium Server, start it on port 4444 and use that host instead.

chromedriver --port=9515

Your First Selenium PHP Script: Step-by-Step Code Example

With the library installed and the driver server running, you can write a complete Selenium PHP script. The example below opens Chrome, loads a page, finds an element, types into it, clicks a button, reads the page title, and closes the session. Every flow you build later follows this same shape.

<?php
require_once 'vendor/autoload.php';

use Facebook\WebDriver\Remote\RemoteWebDriver;
use Facebook\WebDriver\Remote\DesiredCapabilities;
use Facebook\WebDriver\WebDriverBy;
use Facebook\WebDriver\WebDriverKeys;

// The ChromeDriver server listens on port 9515 by default
$host = 'http://localhost:9515';

// DesiredCapabilities tells the driver which browser to launch
$capabilities = DesiredCapabilities::chrome();

// RemoteWebDriver opens the session over the W3C WebDriver protocol
$driver = RemoteWebDriver::create($host, $capabilities);

// Navigate to a URL
$driver->get('https://www.google.com');

// Locate an element with WebDriverBy and type into it
$searchBox = $driver->findElement(WebDriverBy::name('q'));
$searchBox->sendKeys('TestMu AI');
$searchBox->sendKeys(WebDriverKeys::ENTER);

// Read something back from the page
echo $driver->getTitle();

// Always close the session with quit()
$driver->quit();

Here is what each step does.

  • RemoteWebDriver::create() opens a browser session by connecting to the driver server, passing DesiredCapabilities to request Chrome.
  • get() navigates the browser to the target URL.
  • findElement() with WebDriverBy locates elements by name, CSS selector, ID, or XPath.
  • sendKeys() types text into an input, and click() presses a button or link.
  • quit() ends the session and closes the browser so the driver does not leak processes.

The script above runs against a local ChromeDriver, which is ideal for writing and debugging tests. When you need to prove the same PHP WebDriver tests work across the browsers and operating systems your users actually run, point RemoteWebDriver at TestMu AI Automation Cloud instead of localhost. It is a zero-infrastructure cloud grid that runs your existing Selenium, Cypress, Playwright, and Puppeteer scripts across 3,000+ real browser and OS combinations in parallel, capturing network logs, console logs, video, and screenshots automatically on every run, so there is no local grid to maintain.

Note

Note: Run your Selenium PHP tests across 3,000+ real browser and OS combinations on TestMu AI. Try TestMu AI free!

Choosing the Right PHP Testing Framework (PHPUnit vs. Codeception vs. Behat)

Selenium drives the browser, but you still need a PHP testing framework to structure tests, run them, and report results. Three tools dominate PHP test automation, and each fits a different job.

PHPUnit is the de facto standard for unit and integration testing in PHP. It pairs cleanly with php-webdriver to drive Selenium sessions inside familiar test cases and assertions, which makes it a natural first choice for teams already writing PHPUnit tests.

Codeception builds on top of PHPUnit and adds a readable wrapper for acceptance testing. Its WebDriver module talks to Selenium directly, so you write high-level browser steps without managing the driver by hand.

Behat is the leading behavior-driven development (BDD) framework for PHP. Paired with Mink, a browser abstraction layer, Behat lets you describe scenarios in plain Gherkin language and run them against Selenium, which keeps business stakeholders close to the test suite.

FrameworkBest forHow it uses Selenium
PHPUnitUnit and integration testsDrives php-webdriver inside standard test cases and assertions
CodeceptionAcceptance testingBuilt-in WebDriver module wraps Selenium with readable steps
Behat with MinkBehavior-driven development (BDD)Gherkin scenarios run against Selenium through the Mink abstraction

The WebDriverBy strategies these frameworks rely on are covered in the Selenium locators reference, so you can pick the right selector for each element.

You can also compare PHP against other language bindings in the Selenium Java tutorial before you commit to a stack.

Author

...

Devansh Bhardwaj

Blogs: 81

  • Twitter
  • Linkedin

Devansh Bhardwaj is a Community Evangelist at TestMu AI with 4+ years of experience in the tech industry. He has authored 30+ technical blogs on web development and automation testing and holds certifications in Automation Testing, KaneAI, Selenium, Appium, Playwright, and Cypress. Devansh has contributed to end-to-end testing of a major banking application, spanning UI, API, mobile, visual, and cross-browser testing, demonstrating hands-on expertise across modern testing workflows.

Reviewer

...

Himanshu Sheth

Reviewer

  • Linkedin

Himanshu Sheth is the Director of Marketing (Technical Content) at TestMu AI, with over 8 years of hands-on experience in Selenium, Cypress, and other test automation frameworks. He has authored more than 130 technical blogs for TestMu AI, covering software testing, automation strategy, and CI/CD. At TestMu AI, he leads the technical content efforts across blogs, YouTube, and social media, while closely collaborating with contributors to enhance content quality and product feedback loops. He has done his graduation with a B.E. in Computer Engineering from Mumbai University. Before TestMu AI, Himanshu led engineering teams in embedded software domains at companies like Samsung Research, Motorola, and NXP Semiconductors. He is a core member of DZone and has been a speaker at several unconferences focused on technical writing and software quality.

Open in ChatGPT Icon

Open in ChatGPT

Open in Claude Icon

Open in Claude

Open in Perplexity Icon

Open in Perplexity

Open in Grok Icon

Open in Grok

Open in Gemini AI Icon

Open in Gemini AI

Copied to Clipboard!
...

3000+ Browsers. One Platform.

See exactly how your site performs everywhere.

Try it free
...

Write Tests in Plain English with KaneAI

Create, debug, and evolve tests using natural language.

Try for free
...
TestMu Conf 2026

World's largest virtual agentic engineering & quality conference

...

AUG 19-21, 2026

WATCH NOW

Frequently asked questions on Selenium PHP

Did you find this page helpful?

More Related Blogs

TestMu AI forEnterprise

Get access to solutions built on Enterprise
grade security, privacy, & compliance

  • Advanced access controls
  • Advanced data retention rules
  • Advanced Local Testing
  • Premium Support options
  • Early access to beta features
  • Private Slack Channel
  • Unlimited Manual Accessibility DevTools Tests