Instead of doing something like that, I would suggest writing all your test commands and assertions as retry-until-successful. That’s how for example cypress.io works. I believe Selenium has some built-in feature for that too.
It might sound sloppy but it’s actually pretty good. Here’s cypress.io’s page about it: https://docs.cypress.io/guides/core-concepts/retry-ability.html