You can achieve synchronization using waits in Selenium. There are basically 3 types of wait statements: Implicit Wait, Explicit Wait and Fluent Wait.
Implicit wait: instructs the WebDriver to wait for some time by polling the DOM. Once you have declared implicit wait, it will be available for the entire life of the WebDriver instance. By default, the value will be 0. If you set a longer default, then the behavior will poll the DOM on a periodic basis depending on the browser/driver implementation.
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS) ;
Explicit wait: instructs the execution to wait for some time until some condition is achieved. Some of those conditions to be attained are: elementToBeClickable, elementToBeSelected, presenceOfElementLocated.
WebDriverWait wait = new WebDriverWait(driver, 20); WebElement element; element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("")));
Fluent Wait: The fluent wait is used to tell the web driver to wait for a condition, as well as the frequency with which we want to check the condition before throwing an "ElementNotVisibleException" exception.
Wait wait = new FluentWait(WebDriver reference) .withTimeout(timeout, SECONDS) .pollingEvery(timeout, SECONDS) .ignoring(Exception.class);