How to use Selenium with PHP
You have installed the Selenium RC Server, but you are looking for a client driver to write automation scripts in PHP without getting bogged down in unit testing frameworks. This is a common friction point when moving from JavaScript or Python environments, where headless browsers and drivers are often more readily available as standalone libraries.
The short answer is that Selenium RC (Remote Control) does not have an official, native PHP client driver like it does for Java, Ruby, or C#. The architecture of Selenium RC relies heavily on a specific server protocol that was tightly coupled with the Selenium Server's internal implementation. While you can technically communicate with the Selenium Server via raw HTTP requests using cURL in PHP, this approach is fragile and difficult to maintain because the underlying protocol has evolved significantly over time.
To bridge this gap without writing your own low-level HTTP parsers, the standard industry practice involves leveraging PHPUnit. Even if your goal is not formal unit testing, PHPUnit provides the necessary abstraction layer to interact with the Selenium server. The library acts as a wrapper around the Selenium RC commands, translating high-level PHP method calls into the specific HTTP requests the server expects.
Here is how you can structure a simple automation script using the phpunit/phpunit package and the phpsecdriver or legacy selenium-server-standalone-jar approach. You will need to install PHPUnit via Composer:
composer require phpunit/phpunit
Once installed, you can create a PHP class that extends PHPUnit's base test case, even if you don't run the full test suite command immediately. The core logic involves instantiating the Selenium2WebDriver object and defining your browser actions within methods.
require 'vendor/autoload.php';
use PHPUnit\Framework\TestCase;
use Selenium\WebDriver\Remote\RemoteWebDriver;
use Selenium\WebDriver\ChromeOptions;
class AutomationTask extends TestCase {
public function testLogin() {
$driver = RemoteWebDriver::create(
"http://localhost:4444/wd/hub",
new ChromeOptions()->setArguments(["headless"])
);
try {
$driver->get('https://example.com');
$driver->findElement("css selector", "#login-form")
->click();
// Your automation logic here
} finally {
$driver->quit();
}
}
}
In this example, the code mimics standard Selenium WebDriver syntax rather than the older RC syntax. This is crucial because relying on the old RC protocol in PHP often leads to deprecated errors as modern Selenium versions drop support for it entirely. The "WebDriver" approach uses a JSON wire protocol which is stable and well-documented.
A significant trade-off when using PHPUnit for non-testing automation is that you must still adhere to its lifecycle management. You cannot simply instantiate the driver outside of the test class context without managing the startup and shutdown hooks correctly. If you skip composer install or fail to include the autoloader, the classes will not resolve, resulting in fatal errors.
Another pitfall is the dependency on the Selenium Server version. The RemoteWebDriver object expects a specific JSON response format from the server. If your local Selenium RC installation is an older version that does not support the WebDriver protocol natively, you will encounter connection refused or timeout exceptions. You may need to download and run the standalone Selenium Server JAR file separately to ensure compatibility with the PHP client.
For production-grade automation where strict testing suites are not required, consider whether a dedicated library like php-webdriver might be more appropriate. These libraries often wrap the WebDriver protocol directly without forcing you into the PHPUnit testing hierarchy, offering a cleaner separation of concerns for scripting tasks rather than test verification.