Cyberithub

How to Install Selenium webdriver for PHP in 9 Easy Steps

Advertisements

In this article, I will take you through the steps to install selenium webdriver library for PHP in 9 easy steps. Php-webdriver library is PHP language binding for Selenium WebDriver which allows you to control different web browsers like chrome, Firefox and Microsoft Edge from PHP. The concepts of this library are very similar to the "official" Java, JavaScript, .NET, Python and Ruby libraries which are developed as part of the Selenium project. Php-webdriver library supports modern W3C WebDriver protocol, as well as legacy JsonWireProtocol. This library is compatible with Selenium server version 2.x, 3.x and 4.x. You can check more on GitHub.

How to Install Selenium webdriver for PHP in 9 Easy Steps

How to Install Selenium webdriver for PHP in 9 Easy Steps

Also Read: Step By Step to Install Selenium WebDriver for JavaScript

Step 1: Prerequisites

a) You should have a running Linux(in our case, it is Ubuntu 20.04 LTS) System.

b) You should have PHP installed in your System.

c) You should have sudo or root access to run privileged commands.

d) You should have curl utility available in your System.

 

Step 2: Install Composer

You can download and install composer in Linux based systems using below curl command.

Advertisements
cyberithub@ubuntu:~$ curl -sS https://getcomposer.org/installer | php
All settings correct for using Composer
Downloading...

Composer (version 2.4.3) successfully installed to: /home/cyberithub/composer.phar
Use it: php composer.phar

 

Step 3: Check Composer Version

After successful installation, you can verify the installed version by using php composer.phar --version command as shown below.

cyberithub@ubuntu:~$ php composer.phar --version
Composer version 2.4.3 2022-10-14 16:56:41

 

Step 4: Install Libraries

Next step is to install Php-webdriver libraries by using php composer.phar require php-webdriver/webdriver command as shown below.

cyberithub@ubuntu:~$ php composer.phar require php-webdriver/webdriver
Info from https://repo.packagist.org: #StandWithUkraine
Cannot use php-webdriver/webdriver's latest version 1.13.1 as it requires ext-curl * which is missing from your platform.
Using version ^1.1 for php-webdriver/webdriver
./composer.json has been created
Running composer update php-webdriver/webdriver
Loading composer repositories with package information
Updating dependencies
Lock file operations: 1 install, 0 updates, 0 removals
- Locking php-webdriver/webdriver (1.1.1)
Writing lock file
Installing dependencies from lock file (including require-dev)
Package operations: 1 install, 0 updates, 0 removals
- Downloading php-webdriver/webdriver (1.1.1)
- Installing php-webdriver/webdriver (1.1.1): Extracting archive
1 package suggestions were added by new dependencies, use `composer suggest` to see details.
Generating autoload files
No security vulnerability advisories found

 

Step 5: Install Chrome Webdriver

Although you can install webdriver of any browser you are planning to use, we are installing only chrome webdriver for demo purpose by using sudo apt install chromium-chromedriver command as shown below.

cyberithub@ubuntu:~$ sudo apt install chromium-chromedriver
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following NEW packages will be installed:
chromium-chromedriver
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 2,496 B of archives.
After this operation, 77.8 kB of additional disk space will be used.
Get:1 http://in.archive.ubuntu.com/ubuntu focal-updates/universe amd64 chromium-chromedriver amd64 1:85.0.4183.83-0ubuntu0.20.04.2 [2,496 B]
Fetched 2,496 B in 1s (4,974 B/s)
Selecting previously unselected package chromium-chromedriver.
(Reading database ... 264860 files and directories currently installed.)
Preparing to unpack .../chromium-chromedriver_1%3a85.0.4183.83-0ubuntu0.20.04.2_amd64.deb ...
Unpacking chromium-chromedriver (1:85.0.4183.83-0ubuntu0.20.04.2) ...
Setting up chromium-chromedriver (1:85.0.4183.83-0ubuntu0.20.04.2) ...

 

Step 6: Test Chrome Webdriver

After successful installation, you can start chromedriver by using sudo chromedriver command as shown below. By default, it will run on Port 9515 as you can see below.

cyberithub@ubuntu:~$ sudo chromedriver
mkdir: cannot create directory '/run/user/0': Permission denied
Starting ChromeDriver 106.0.5249.119 (9f2101830b56fd2ea1408287f6c74e253ebcb7c6-refs/branch-heads/5249@{#797}) on port 9515
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.

You can also run chrome webdriver on different port let's say on Port 4444 using sudo chromedriver --port=4444 command as shown below.

cyberithub@ubuntu:~$ sudo chromedriver --port=4444
mkdir: cannot create directory '/run/user/0': Permission denied
Starting ChromeDriver 106.0.5249.119 (9f2101830b56fd2ea1408287f6c74e253ebcb7c6-refs/branch-heads/5249@{#797}) on port 4444
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.

 

Step 7: Create Browser Session

You can create the browser session by passing the URL of your running server as shown below.

$serverUrl = 'http://localhost:4444';

Then you can start chrome browser as shown below:-

$driver = RemoteWebDriver::create($serverUrl, DesiredCapabilities::chrome());

 

Step 8: Customize Capabilities

Desired capabilities define properties of the browser you are about to start. You can customize different capabilities using below.

// Create an instance of ChromeOptions:
$chromeOptions = new ChromeOptions();

// Configure $chromeOptions, see examples bellow:
$chromeOptions->addArguments(...);

// Create $capabilities and add configuration from ChromeOptions
$capabilities = DesiredCapabilities::chrome();
$capabilities->setCapability(ChromeOptions::CAPABILITY_W3C, $chromeOptions);

// Start the browser with $capabilities
// A) When using RemoteWebDriver::create()
$driver = RemoteWebDriver::create($serverUrl, $capabilities);
// B) When using ChromeDriver::start to start local Chromedriver
$driver = ChromeDriver::start($capabilities);

 

Step 9: Control Your Browser

You can visit GitHub and check all the different most used php-webdriver commands to control your browser.

// Go to URL
$driver->get('https://en.wikipedia.org/wiki/Selenium_(software)');

// Find search element by its id, write 'PHP' inside and submit
$driver->findElement(WebDriverBy::id('searchInput')) // find search input element
->sendKeys('PHP') // fill the search box
->submit(); // submit the whole form

// Find element of 'History' item in menu by its css selector
$historyButton = $driver->findElement(
WebDriverBy::cssSelector('#ca-history a')
);
// Read text of the element and print it to output
echo 'About to click to a button with text: ' . $historyButton->getText();

// Click the element to navigate to revision history page
$historyButton->click();

// Make sure to always call quit() at the end to terminate the browser session
$driver->quit();

Leave a Comment