Check if element exists Selenium in php

Below example shows how to check if specific element exists on the web page or not.
 
<?php
class TestSize extends PHPUnit_Framework_TestCase {

protected $webDriver;

public function setUp()
{
$capabilities = array(WebDriverCapabilityType::BROWSER_NAME => ‘chrome’);
$this->webDriver = RemoteWebDriver::create(‘https://localhost:4444/wd/hub’, $capabilities);
$this->webDriver->manage()->window()->maximize();
}

public function testSize()
{
$this->webDriver->get(“https://www.softpost.org/selenium-test-page”);

$elements = $this->webDriver->findElements(WebDriverBy::id(‘fn’));

if (sizeof($elements)==0)
echo(“
 Element with id fn not found”);
else
echo(“
 Element with id fn found”);

}

public function tearDown()
{
//Quit the driver
$this->webDriver->quit();
}
}
?>

Complex problems, Simple Solutions