Executing JavaScript in Selenium in PHP

Below example shows how to execute the JavaScript in Selenium in PHP.

class MyTest extends PHPUnit_Framework_TestCase {

protected $driver;

public function setUp()
$capabilities = array(\WebDriverCapabilityType::BROWSER_NAME => ‘chrome’);
$this->driver = RemoteWebDriver::create(‘https://localhost:4444/wd/hub’, $capabilities);

public function testJS()

$src = $this->driver->executeScript(‘return document.body.innerHTML;’);

echo (“Source of page -> ” . $src);

//click on the radio button using java script
//Notice how we have passed the argument to the java script code.
$elements = $this->driver->findElements(WebDriverBy::xpath(“//input[@value=’female’]”));



public function tearDown()
//Quit the driver

You may also like...