Check element exists using PHP Selenium 2 Webdriver?
Asked Answered
R

6

7

I am trying to check if an element on a page exists by CSS using Selenium 2. Anyone have any examples using PHP Selenium Webdriver Facebook wrapper?

I have tried the below code:

if($driver->findElement(WebDriverBy::xpath("image-e4e")) != 0)
{
}

But gives me this error:

Fatal error: Uncaught exception 'NoSuchElementWebDriverError' with message 'Unable to locate element: {"method":"xpath","selector":"image-e4e"}

Rexer answered 21/10, 2013 at 14:8 Comment(0)
S
17

By design, findElement returns the WebDriverElement if it is found but it throws an exception when it is not found.

To check whether the element is on the page without getting an exception, the trick is to use findElements. findElements returns an array of all elements found on the page. It returns an empty array if nothing is found.

if (count($driver->findElements(WebDriverBy::xpath("image-e4e"))) === 0) {
  echo 'not found';
}
Swellhead answered 23/10, 2013 at 19:23 Comment(0)
P
3

Use: $driver->findElements intstead of findElement

findElements will return an empty array if element not exists and will not throw an exception.

Paramnesia answered 15/6, 2016 at 8:2 Comment(0)
T
1

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.

Thamos answered 11/1, 2020 at 16:20 Comment(0)
D
0

That's because your xpath is bad.

If you want to continue using xpath, then your seletor would be

//*[@id='image-e4e']

If you have an open mind, get used to CSS Selectors. They are faster and much more readable

WebDriverBy::cssSelector("#image-e4e")

This is of course assuming that the ID is image-e4e. The reason your xpath was failing was because xpath was attempting to locate an immedate child with tag name image-e4e. you want to parse the entire pom for an element with an attribute that equals image-e4e. Whether that's ID or name, I do not know.

Dirk answered 21/10, 2013 at 14:49 Comment(3)
It still gives me the error of uncaught exception. At the time I test the element is not on page, so I want to check if it doesnt exist.. if the element exists I dont get error..Rexer
I tried both WebDriverBy::xpath("//*[@id='image-e4e']")) and WebDriverBy::cssSelector("#image-e4e")... any other ideas..Rexer
I was thinking of some sort of assert function, but not really sure.. seems like a very straightforward thing to do...but cant quite figure it outRexer
D
0
//https://www.guru99.com/images/1/073118_0611_Installatio8.png
if (count($crdriver->findElements(WebDriverBy::xpath('//*[@id="topOfPag"]/div[7]/div[1]/h1'))) === 1) {
    echo 'con1';
} else if (count($crdriver->findElements(WebDriverBy::xpath('//*[@id="topOfPage"]/div[7]/div[1]/h1'))) === 1){
    echo 'con2';
}else {
    echo 'fail';
}
Decima answered 24/7, 2023 at 7:59 Comment(0)
T
-1
if($driver->findElement(WebDriverBy::id("image-e4e")) != 0)
{

}

Try the above logic I am sure definitely it will work out.

Typology answered 7/4, 2014 at 5:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.