0

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.

  • Possible duplicate of [Selenium WebDriver - Test if element is present](https://stackoverflow.com/questions/7991522/selenium-webdriver-test-if-element-is-present) – vinS Dec 17 '17 at 05:52
  • Do you have an implicit wait in the code? – Grasshopper Dec 17 '17 at 06:01
  • I do. I'm not a smart person sometimes. It's set to ten seconds. That'd resolve the issue. Thanks! – Mr. Dollar Press Dec 17 '17 at 06:11
  • 1
    Actually, this raises a second question. Is there a way to remove the implicit wait entirely? – Mr. Dollar Press Dec 17 '17 at 06:31
  • Yes, remove all references to implicit wait and when you need a wait, add `WebDriverWait`. – JeffC Dec 17 '17 at 06:34
  • If you already have a working test automation be carefully removing all implicit waits, as it may cause other issues of elements not being found. For calls where you want to remove implicit wait set it to 0 and after making those calls switch it back. Otherwise explicit wait is a better option. – Grasshopper Dec 17 '17 at 06:45

1 Answers1

1

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.
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352