Php-webdriver: findElements() throws exception when no elements found.

Created on 7 Oct 2015  路  10Comments  路  Source: php-webdriver/php-webdriver

According to the docs findElements() should return an empty array when no elements are found, but i get a WebDriverCurlException when running this snippet :

$element = \WebDriverBy::cssSelector("#not_exists tr > td:first-child");
$found   = $webDriver->findElements($element);
help wanted

Most helpful comment

I was in a similar situation and I did this with success in case someone is on the same boat.

1. Under my name space

use \Facebook\WebDriver\Exception\NoSuchElementException;

2. In my code

try
{
  $search = $driver->findElement(WebDriverBy::xpath('//*[@id="content"]/p[1]'))->getText();
}
catch (NoSuchElementException $e)
{
  // the element did not exist
  var_dump($e);
}

All 10 comments

This has been brought up before as well, but I don't think was ever resolved.

Sounds like a relatively easy fix... Contributions welcome!

According to the docs findElements() should return an empty array when no elements are found

It does. This method is just unsafe (missing @throws tag).

WebDriverCurlException signals some kinds of important problems. IMO there is no need to fix this method.

:+1:

I have the same issue.
How can I check if an element exist then ?

To answer my own question, the curl exception would happen only if there was an implicit wait.

driver->manage()->timeouts()->implicitlyWait(30);

once removed the implicit wait, findElements() would just return an empty array when no element was found.

I was in a similar situation and I did this with success in case someone is on the same boat.

1. Under my name space

use \Facebook\WebDriver\Exception\NoSuchElementException;

2. In my code

try
{
  $search = $driver->findElement(WebDriverBy::xpath('//*[@id="content"]/p[1]'))->getText();
}
catch (NoSuchElementException $e)
{
  // the element did not exist
  var_dump($e);
}

@emilorol However you are using findElement() method here, which is supposed to throw NoSuchElementException in this case.

The original issue was about findElements() (note the s) method, which should always return an array - an empty one if no elements were found. However I cannot reproduce this original issue (findElements() throwing an exception instead of returning empty array), but if anyone can, an reproducible testcase or pull request is definitely welcome :)

Oh, my bad.

I visit this page looking for a solution on how to tell when the element is not present on the page so that the test will continue on one direction or another.

Thank you.

any one tell me which method decide that how many test case should be automate in selenium

Was this page helpful?
0 / 5 - 0 ratings

Related issues

boussou picture boussou  路  6Comments

Eddcapone picture Eddcapone  路  4Comments

Gadzillion picture Gadzillion  路  6Comments

syryl picture syryl  路  5Comments

xiejie picture xiejie  路  11Comments