Selenium Silver Bullet: Clicking Moving Things Eventually

How to overcome occasional ElementClickInterceptedExceptions

Michael Kutz
2 min readMar 29, 2021

--

When working with modern, dynamic websites, there are a lot of moving, animated things. Here’s a quite simple trick to click on those successfully.

Recently I had the following challenge: Click on a button, which quickly becomes visible and enabled, but might move after an expanding section above it is loaded.

This typically causes a ElementClickInterceptedException as the click was aiming at the previous position of the element and now hits the expanding section.

Usually, I try to solve this kind of issues with a WebDriverWait to wait for the expanding section to have a certain vertical height. However, this was not working perfectly for reasons I do not fully understand.

The same form has the nasty habit of disabling the button I want to click on after it was enabled before. This brought be countless ElementNotInteractableExcpetion as the WebDriverWait quite often used its chance to check at the wrong 100 ms.

Of course this form is broken. Nobody should blame Selenium here. Human users have a good chance of doing the same misclicks my script does. However, the behavior won’t be changed — at least not quickly.

So after a lot of frustration and overcoming the urge to just put a Thread.sleep, I think I found the least inelegant solution for the issue:

So what does this do? It basically tries to click on the moving or deactivated button. We know that might fail with either a ElementClickInterceptedException or a ElementNotInteractableExcpetion, so we will ignore those. So, should the element not show up at all, the test will still fail immediately, but if the button was moved or occluded or deactivated, the caused exception will be ignored and the click will be executed again until the timeout is reached.

I really don’t like silver bullets. They usually are dirty tricks that open holes for bugs to slip in. This particular one however is quite what a human user does to cope with the nasty UI: they just click again until it works.

So this code documents the nastiness of the UI and my test no longer breaks to tell me what I already know.

--

--

Michael Kutz

I've been a software engineer since 2009, worked in various agile projects & got a taste for quality assurance. Today I'm a quality engineer at REWE digital.