Your suggested solution only waits for DOM readyState
to signal complete
. But Selenium by default tries to wait for those (and a little bit more) on page loads via the driver.get()
and element.click()
methods. They are already blocking, they wait for the page to fully load and those should be working ok.
Problem, obviously, are redirects via AJAX requests and running scripts - those can't be caught by Selenium, it doesn't wait for them to finish. Also, you can't reliably catch them via readyState
- it waits for a bit, which can be useful, but it will signal complete
long before all the AJAX content is downloaded.
There is no general solution that would work everywhere and for everyone, that's why it's hard and everyone uses something a little bit different.
The general rule is to rely on WebDriver to do his part, then use implicit waits, then use explicit waits for elements you want to assert on the page, but there's a lot more techniques that can be done. You should pick the one (or a combination of several of them) that works best in your case, on your tested page.
See my two answers regarding this for more information:
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…