Selenium Element interaction in php

Below example demonstrates how to interact with web elements using Selenium in PHP.
  • Sending keys
  • Clearing the text box
  • Getting value from text box
  • Selecting value from drop down
  • Checking if element is disabled/enabled
  • Checking if element is selected or not selected
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 testInteractions()



$firstName = $this->driver->findElement(WebDriverBy::id(‘fn’))->getAttribute(‘value’);

$this->assertEquals(‘Shaun’, $firstName);





//To get value of an attribute of an element, use getAttribute(attributeName) method.
$attribute = $this->driver->findElement(WebDriverBy::linkText(‘Selenium in Java’))->getAttribute(‘href’);
 href attribute of Link is ” . $attribute);

//To check if an element is enabled, use isEnabled() method.
$enabled = $this->driver->findElement(WebDriverBy::xpath(“//input[@value=’QTP’]”))->isEnabled();
 QTP checkbox is enabled? ” . $enabled);

//To check if an element is enabled, use isSelected() method.
$selected = $this->driver->findElement(WebDriverBy::xpath(“//input[@value=’QTP’]”))->isSelected();
 QTP checkbox is selected? ” . $selected);

//To get the text of an element, use getText() method.
$txt = $this->driver->findElement(WebDriverBy::tagName(“p”))->getText();
 Text in first paragraph -> ” . $txt);

//To get the text of an element, use getText() method.
$displayed = $this->driver->findElement(WebDriverBy::xpath(“//input[@value=’Sign up’]”))->isDisplayed();
 is Sign up button displayed? -> ” . $displayed);

//To get css value of an element , use getCssValue() method.

//To get tag name of an element , use getTagName() method.

$tagName = $this->driver->findElement(WebDriverBy::linkText(‘Selenium in Java’))->getTagName();
TagName of Link is ” . $tagName);

//Select city from the dropdown


//We can also use WebDriverSelect class to work with drop downs
$dropDown = $this->driver->findElement(WebDriverBy::xpath(“//select”));
(new WebDriverSelect($dropDown))->selectByVisibleText(“Pune”);

//You can use below methods of WebDriverSelect class
//isMultiple(), getOptions(), getAllSelectedOptions(), getFirstSelectedOption()
//deselectAll(), selectByIndex(), selectByValue(), selectByVisibleText()
//deselectByIndex(), deselectByValue(), deselectByVisibleText(), escapeQuotes()

echo (“
The selected option is -> ” . (new WebDriverSelect($dropDown))->getFirstSelectedOption()->getText());


public function tearDown()
//Quit the driver

Web development and Automation testing

solutions delivered!!