Java – Is it faster to call driver.findElement or WebElement.findElement?

Is it faster to call driver.findElement or WebElement.findElement?… here is a solution to the problem.

Is it faster to call driver.findElement or WebElement.findElement?

I’ve looked around online and in the docs to try to find a concrete answer to this question, but have had no luck so far.

Let’s say I have a custom container element that contains some part of my website, let’s say there are a bunch of nested elements in it. Is it faster to use driver.findElement or WebElement.findElement to find inner elements? Do they have to interact with the browser every time, or does the latter happen in memory?

Hopefully the question makes sense.

Cheers.

Solution

I would say that in general, it takes the same amount of time / you won’t notice the difference. Unless it’s a very complex page, but in this case you may have run into a bigger problem managing page state in your tests.

Both of the calls you mentioned will result in a remote HTTP call to the selenium server, which takes more time than actually searching for an element in the HTML tree. So the final time will be about the same.

Now consider your page load time, which is usually orders of magnitude slower than the findElement call….

So do you want to speed up testing or are you just asking this question out of curiosity?

Related Problems and Solutions