There are three ways to solve this, you can use any of these or combine them:
1.) Immediately after creating the Selenium web driver instance, set implicit wait : driver.manage().timeouts().implicitlyWait(). This will tell the driver, the amount of time it should wait when finding an element if it is not immediately present after the page is loaded.
2.) After page navigation, you'd have to call JavaScript return document.readyState until "complete" is returned. It means that the document (i.e. the .java file) is now parsed and loaded.
Sample code in C# is shown below:
new WebDriverWait(driver, MyDefaultTimeout).Until(
d => ((IJavaScriptExecutor) d).ExecuteScript("return document.readyState").Equals("complete"));
Java:
new WebDriverWait(firefoxDriver, pageLoadTimeout).until(
webDriver -> ((JavascriptExecutor) webDriver).executeScript("return document.readyState").equals("complete"));
3.) After performing step 2, check if the URL matches the pattern you expect.
To know more about Selenium, you can go through the Selenium course page by Intellipaat.