Have any questions:

Toll free:9801887718Available 24/7

Email our experts:info@mantraideas.com

In: Selenium, Web Automation

Introduction

Selenium is a powerful open-source tool for automating web browsers. Combined with PHP WebDriver, it allows you to simulate user interactions like form filling, clicking buttons, and navigating pages — all from PHP.

In this guide, you’ll learn how to:

  • Install Selenium and ChromeDriver
  • Set up PHP WebDriver
  • Write and run a working Selenium test in PHP
  • Automate a web form with real-time browser actions

 What Is a PHP WebDriver?

PHP WebDriver is a PHP client for Selenium WebDriver. It’s based on the JSON Wire Protocol and allows you to:

  • Control browsers (Chrome, Firefox, etc.) from PHP
  • Interact with HTML elements (forms, links, buttons)
  • Automate end-to-end UI testing and scraping tasks

The most widely used PHP WebDriver library is:

php-webdriver/webdriver

For full documentation and usage examples, visit the official wiki.

Step-by-Step Setup Guide

Let’s walk through the complete setup before running our first test.

1. Install PHP and Composer

Make sure you have PHP and Composer installed. You can verify with:

php -v
composer -V

2. Install Selenium Server

To use PHP WebDriver, you need to run a Selenium Server instance that bridges between your PHP code and the browser.

Download the latest stable version from the official Selenium site:
https://www.selenium.dev/downloads/

Download the Selenium Server for your preferred language binding.Since you’re using Java, make sure the java is installed properly:

java -version

Run the Selenium Server

  1. Move the .jar file to a directory (e.g., selenium/)
  2. Open terminal in that folder
  3. Start the server using: 
  4. selenium-server-4.x.x.jar → x.x.x is your installed version.
java -jar selenium-server-4.x.x.jar standalone

This launches the Selenium server on:
http://localhost:4444

3. Install ChromeDriver

You need a driver for your browser. For Chrome:

Test it by running:

chromedriver --version

4. Install PHP WebDriver via Composer

Inside your project folder:

composer require php-webdriver/webdriver

This adds the library and autoloads the necessary classes.

Example: Automate a Web Form Using PHP Selenium

Here’s the full working script to automate form submission on Selenium’s demo page:

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

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

$host = 'http://localhost:4444'; // Selenium server
$driver = RemoteWebDriver::create($host, DesiredCapabilities::chrome());

// Go to web form
$driver->get('https://www.selenium.dev/selenium/web/web-form.html');

// Fill input fields
$driver->findElement(WebDriverBy::id('my-text-id'))->sendKeys('Hello World');
$driver->findElement(WebDriverBy::name('my-password'))->sendKeys('Secret123');
$driver->findElement(WebDriverBy::name('my-textarea'))->sendKeys('This is a test message.');

// Dropdown
$selectElement = $driver->findElement(WebDriverBy::name('my-select'));
foreach ($selectElement->findElements(WebDriverBy::tagName('option')) as $option) {
    if ($option->getAttribute('value') === '2') {
        $option->click();
        break;
    }
}

// Other fields
$driver->findElement(WebDriverBy::name('my-datalist'))->sendKeys('Chicago');
$driver->findElement(WebDriverBy::id('my-check-2'))->click();
$driver->findElement(WebDriverBy::id('my-radio-2'))->click();
$driver->findElement(WebDriverBy::name('my-colors'))->sendKeys('#ff0000');
$driver->findElement(WebDriverBy::name('my-date'))->sendKeys('07/14/2025' . WebDriverKeys::ENTER);
$driver->findElement(WebDriverBy::name('my-range'))->sendKeys(WebDriverKeys::ARROW_RIGHT);

// Submit the form
$driver->findElement(WebDriverBy::cssSelector('button[type="submit"]'))->click();

// Screenshot
$driver->takeScreenshot('screenshot.png');

// Close the browser
$driver->quit();

What Can You Automate With Selenium PHP?

  • Form submissions
  • Login and registration
  • Web scraping
  • E2E testing
  • Search results interaction
  • Admin dashboard automation

Bonus Tips

You can run Chrome in headless mode for CI:

DesiredCapabilities::chrome()->setCapability('goog:chromeOptions', 
['args' => ['--headless']])
  • Use WebDriverWait for dynamic pages (with JavaScript)
  • Combine this with PHPUnit for full testing frameworks

Conclusion

Now you know how to:

  • Install Selenium
  • Set up PHP WebDriver
  • Run a full browser automation script from PHP

This is just the beginning! PHP is fully capable of powering browser automation, UI testing, and scraping tools — all with the help of Selenium.

Whether you’re writing QA scripts, building data scrapers, or just exploring test automation, Selenium for PHP is a solid, scalable choice.

Learn more and explore advanced usage:
php-webdriver/php-webdriver Wiki

Spread the love

Leave a Reply

Your email address will not be published. Required fields are marked *