0

In my PHP script I am using php-webdriver library with Edge webdriver to get data from a web page and I need to start Selenium standalone server from the script. I copied selenium-server-4.35.0.jar, msedgedriver.exe and NodeWebDriver.json to the current user directory C:\Users\pnfst, and also created 2 bat files for running hab and for running node: selenium-server-hub.bat and selenium-server-node.bat respectively.

NodeWebDriver.json

{
 "capabilities": [
 {
 "browserName": "MicrosoftEdge",
 "maxInstances": 5,
 "seleniumProtocol": "WebDriver",
 "platform": "WINDOWS"
 },
 {
 "browserName": "firefox",
 "maxInstances": 5,
 "seleniumProtocol": "WebDriver",
 "platform": "WINDOWS"
 },
 {
 "browserName": "chrome",
 "maxInstances": 5,
 "seleniumProtocol": "WebDriver",
 "platform": "WINDOWS"
 },
 {
 "browserName": "internet explorer",
 "maxInstances": 1,
 "seleniumProtocol": "WebDriver",
 "platform": "WINDOWS"
 }
 ],
 "proxy": "org.openqa.grid.selenium.proxy.DefaultRemoteProxy",
 "maxSession": 5,
 "port": 5555,
 "register": true,
 "registerCycle": 5000,
 "hub": "http://Администратор:[email protected]:4444",
 "nodeStatusCheckTimeout": 5000,
 "nodePolling": 5000,
 "role": "node",
 "unregisterIfStillDownAfter": 60000,
 "downPollingLimit": 2,
 "debug": false,
 "servlets": [],
 "withoutServlets": [],
 "custom": {}
}

selenium-server-hub.bat

java -Dwebdriver.edge.driver=msedgedriver.exe -jar ".\selenium-server-4.35.0.jar" hub

selenium-server-node.bat

java -Dwebdriver.edge.driver=msedgedriver.exe -jar ".\selenium-server-4.35.0.jar" node --config ".\NodeWebDriver.json"

Then I tried to start Selenium standalone server from PHP using exec as follows.

Variant 1

$hubCommand = 'C:\\Program Files\\Java\\jdk-24\\bin\\java.exe -jar "C:\\Users\\pnfst\\selenium-server-4.35.0.jar" hub 2>&1 &';
$nodeCommand = 'C:\\Program Files\\Java\\jdk-24\\bin\\java.exe -Dwebdriver.edge.driver=C:\\Users\\pnfst\\msedgedriver.exe -jar "C:\\Users\\pnfst\\selenium-server-4.35.0.jar" node --config "C:\\Users\\pnfst\\NodeWebDriver.json" 2>&1 &';
$hubResult = exec($hubCommand, $hubOutput, $hubResult_code); // start hab
$nodeResult = exec($nodeCommand, $nodeOutput, $nodeResult_code); // start node

Variant 2

$psHubCommand = 'Start-Process -FilePath "C:\\Program Files\\Java\\jdk-24\\bin\\java.exe" -ArgumentList "-jar", "selenium-server-4.35.0.jar", "hub" -WorkingDirectory "C:\\Users\\pnfst" -Verb RunAs';
$psNodeCommand = 'Start-Process -FilePath "C:\\Program Files\\Java\\jdk-24\\bin\\java.exe" -ArgumentList "-Dwebdriver.edge.driver=msedgedriver.exe", "-jar", "selenium-server-4.35.0.jar", "node", "--config", ".\\NodeWebDriver.json" -WorkingDirectory "C:\\Users\\pnfst" -Verb RunAs';
$hubCommand = "powershell.exe -NonInteractive -NoProfile -ExecutionPolicy Bypass -Command " . escapeshellarg($psHubCommand) . " 2>&1 &";
$nodeCommand = "powershell.exe -NonInteractive -NoProfile -ExecutionPolicy Bypass -Command " . escapeshellarg($psNodeCommand) . " 2>&1 &";
$hubResult = exec($hubCommand, $hubOutput, $hubResult_code); // start hab
$nodeResult = exec($nodeCommand, $nodeOutput, $nodeResult_code); // start node

Variant 3

$psHubCommand = 'Start-Process -FilePath "C:\\Windows\\system32\\cmd.exe" -ArgumentList "/c", "C:\\Users\\pnfst\\selenium-server-hub.bat" -WorkingDirectory "C:\\Users\\pnfst" -Verb RunAs';
$psNodeCommand = 'Start-Process -FilePath "C:\\Windows\\system32\\cmd.exe" -ArgumentList "/c", "C:\\Users\\pnfst\\selenium-server-node.bat" -WorkingDirectory "C:\\Users\\pnfst" -Verb RunAs';
$hubCommand = "powershell.exe -NonInteractive -NoProfile -ExecutionPolicy Bypass -Command " . escapeshellarg($psHubCommand) . " 2>&1 &";
$nodeCommand = "powershell.exe -NonInteractive -NoProfile -ExecutionPolicy Bypass -Command " . escapeshellarg($psNodeCommand) . " 2>&1 &";
$hubResult = exec($hubCommand, $hubOutput, $hubResult_code); // start hab
$nodeResult = exec($nodeCommand, $nodeOutput, $nodeResult_code); // start node

All variants return result code 0 and don't start hab and node. Variant 2 throws with PowerShell MissingArgument (hub command) and PositionalParameterNotFound (node command) exceptions.
How to properly start a Selenium standalone server from PHP?

Solved

I created subfolders selenium\server in the project publish directory C:\inetpub\wwwroot\medservices\wwwroot and copied all the necessary files there: selenium-server-4.35.0.jar, msedgedriver.exe, NodeWebDriver.json, selenium-server-hub.bat and selenium-server-node.bat. I also made the following changes to the PHP script and bat files.

selenium-server-hub.bat

chcp 65001
chdir C:\inetpub\wwwroot\medservices\wwwroot\selenium\server
"C:\Program Files\Java\jdk-24\bin\java.exe" -jar "selenium-server-4.35.0.jar" hub

selenium-server-node.bat

chcp 65001
chdir C:\inetpub\wwwroot\medservices\wwwroot\selenium\server
"C:\Program Files\Java\jdk-24\bin\java.exe" -Dwebdriver.edge.driver=msedgedriver.exe -jar "selenium-server-4.35.0.jar" node --config "NodeWebDriver.json"

PHP script, which start Selenium server

<?php
function CheckIfSeleniumServerStarted(string $role) : bool
{
 $isStarted = false;
 $handle = curl_init();
 if($handle instanceof CurlHandle)
 {
 curl_setopt_array($handle,
 [
 CURLOPT_URL => $role === "node" ? "http://localhost:5555" : "http://localhost:4444",
 CURLOPT_HTTPHEADER => [ "Connection: keep-alive", "Accept: */*" ],
 CURLOPT_RETURNTRANSFER => true,
 CURLOPT_CONNECTTIMEOUT => 60,
 CURLOPT_TIMEOUT => 60,
 CURLOPT_FOLLOWLOCATION => true,
 CURLOPT_USERAGENT => $_SERVER["HTTP_USER_AGENT"],
 CURLOPT_SSL_VERIFYHOST => FALSE,
 CURLOPT_SSL_VERIFYPEER => FALSE
 ]);
 $result = curl_exec($handle);
 $isStarted = is_string($result) && ($statusCode = curl_getinfo($handle, CURLINFO_HTTP_CODE)) >= 200 && $statusCode < 300;
 curl_close($handle);
 }
 return $isStarted;
}
ini_set("display_errors", true);
header('Content-Type: text/html; charset=utf-8');
$cmdPath = "C:\\Windows\\system32\\cmd.exe";
$workingDir = __DIR__ . "\\selenium\\server";
$shell = null;
if(!CheckIfSeleniumServerStarted("hub"))
{
 $command = "{$cmdPath} /C {$workingDir}\\selenium-server-hub.bat 2>&1 &";
 $shell = new COM("WScript.Shell");
 $shell->Run($command, 1, false);
 sleep(1);
}
else
{
 echo "<pre>Selenium server hub is already started.</pre>";
}
if(!CheckIfSeleniumServerStarted("node"))
{
 $command = "{$cmdPath} /C {$workingDir}\\selenium-server-node.bat 2>&1 &";
 $shell = $shell ?? new COM("WScript.Shell");
 $shell->Run($command, 1, false);
 sleep(1);
}
else
{
 echo "<pre>Selenium server node is already started.</pre>";
}
?>
asked Aug 30 at 16:12
3
  • the batch files have no error handling. Commented 2 days ago
  • Suggestion: change all the relative paths to absolute paths and try again (e.g. .\selenium-server-4.35.0.jar change it to c:\xxxx\xxxx\selenium-server-4.35.0.jar) Commented yesterday
  • ok php code , please add more info about how do you run php script powershell? scheduled task? click on a webserver? Commented yesterday

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.