3

I looked around online and in the documentation to try and find a specific answer to this question with no luck so far.

Assuming I have a custom container element that encompasses some section on a site, say and within that there are a bunch of nested elements. Would it be faster to find an inner element using driver.findElement or WebElement.findElement? Do they both have to interact with the browser each time, or does the latter happen in memory?

Hope this question makes sense.

Cheers.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • It's actually a good question I guess webelement.findElement must be faster because driver.findElement has to search for whole HTML. – Rajagopalan May 08 '18 at 00:17

2 Answers2

2

I would say in general, it would take same time / you would not notice the difference. Unless it's a VERY complex page, but in this case you probably already have bigger problems of managing page state within test.

Both calls you mentioned would produce remote HTTP call to selenium server which would take way more time than actually search for element within HTML tree. So resulting time would be around the same.

Now take into account your page load time which usually by few orders slower than findElement call...

So are you trying to speedup your tests or this question was just out of curiosity?

artkoshelev
  • 872
  • 7
  • 22
  • I don't think it's correct, because it searches with that given tag and it's child tag, if you give driver, then it searches within whole html. – Rajagopalan May 08 '18 at 06:15
  • It depends on locator of child element, e.g. if you specify xpath starting with `//` it would scan entire html tree. And it still sends http request to selenium server which probably takes way more time than actually searching for elements. – artkoshelev May 08 '18 at 07:18
  • I'm automating some user tests of a site and was wondering if there was any noticeable difference between the two out of interest. It would also probably influence my decision a bit about how I structure the code. Thanks for the answer. – Inane question factory May 08 '18 at 22:43
1

driver.findElement() and webelement.findElement() basically works on the same principle to find the first WebElement using the given method. findElement() invocation will return a matching row, or try again repeatedly until the configured timeout (through implicitWait or explicitWait) is reached.

So from performance point of view using driver.findElement() or webelement.findElement() won't have an impact.

So what matters?

What matters most is the Locator Strategy you choose and the way you implement them. The documentation clearly mentions that :

When using xpath be aware that webdriver follows standard conventions:

  • A search prefixed with // will search the entire document not just the children of this current node.
  • Use .// to limit your search to the children of this WebElement.

Conclusion

When using xpath use the prefix .// to limit your search to the children of the referred WebElement only.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352