How to speed up Java Selenium Script,with minimum wait time

There are a few issues with your approach.

  1. .implicitlyWait() doesn’t actually wait. It sets the timeout for the driver instance so you only need to set it once, not call it each time you want to wait.

  2. driver.findElement(...).isEmpty() won’t compile. Maybe you meant .findElements()? Either way, .isEmpty() vs .size() > 0 is going to be a negligible difference in speed.

  3. The main problem is that you have an implicit wait enabled when checking for something to NOT be present… especially a 10s wait. That means that each time an element is checked for, Selenium will wait for 10s even if it’s expecting it to NOT be there.

You would be better served by turning off implicit wait (setting it to 0) and then do your existence check for elements you expect not to be there and then turn it back on. That will be 10s x # of existence checks you expect to not be there. Depending on how many existence checks you do, that could add up to a LOT of time. One downside of this, if you have a complex page with background processes, you will need to have a specific wait for the page (or portion of a page) to finish loading before checking for existence of elements with implicit wait off.

Side note… Selenium contributors have stated that implicit waits shouldn’t be used period. Use WebDriverWait instead but that’s a whole other discussion.

Leave a Comment