Mr. Dollar Press
Mr. Dollar Press

Reputation: 11

Selenium Fast Way To Find If Element Exists

I'm using Selenium to do some searching and testing on a web page. On this page there is a large number of elements that may or may not exist. Most of the time they do not exist but it is critical to find them when they do. I've used

WebElement.findElement(By);
WebElement.findElements(By);
WebDriver.findElement(By);
WebDriver.findElements(By);

All of these methods are very time consuming when they don't find any elements that match the By parameter. Is there a faster way? At this point I'm considering multithreading just for the elements that may or may not exist but that's a whole other can of worms I'd rather not open.

The linked duplicate question does not address the main issue of my question. That main issue being the time it takes for findElement and findElements to return when they don't find any elements.

Upvotes: 0

Views: 1580

Answers (1)

undetected Selenium
undetected Selenium

Reputation: 193308

Let me address your questions individually :

  • Large number of elements that may or may not exist : We souldn't search for elements which doesn't exist. Rather if the element is not_visible we should try to bring the element within the View Port to interact with it.
  • methods are very time consuming : findElement and findElements are based on same Algorithm. So functionally both have similar kind of performance. So to interact with those elements better we need to construct unique css or xpath along with a matching clause for ExpectedCondition
  • WebDriver.findElement(By) and WebElement.findElement(By) : As WebElement.findElement(By) always carry the base reference of the WebDriver instance, most possibly WebDriver.findElement(By); will always have an edge with respect to performance.

Upvotes: 1

Related Questions