You can also use the sizeof()
builtin function in PHP:
$check = $driver->findElements(WebDriverBy::xpath('image-e4e'));
if (sizeof($check) > 0) {
echo "success";
}
OR can also be used to count()
function:
if (count($driver->findElements(WebDriverBy::xpath('image-e4e'))) > 0) {
echo "success";
}
Output: If XPath element is available on a page then it will give you success
If you want to use findElement
then:
$checkXpath = 'image-e4e';
$checkXpath = $this->findElementByXpath($driver, $checkXpath);
$check = $driver->findElement($checkXpath);
if (count($check) > 0) {
echo "success";
}
OR
$checkXpath = WebDriverBy::xpath('image-e4e');
$check = $driver->findElement($checkXpath);
if (count($check) > 0) {
echo "success";
}
Output: If XPath element is available on a page then it will give you success
Note: The findElement
method throws a NoSuchElementException exception when the element is not available on the page. Whereas, the findElements
method returns an empty list when the element is not available or doesn't exist on the page.