Time out after waiting for visibility of element
<p>I'm having trouble with consistently selecting the flight return date from a travel search site. Sometimes it works, but most of the time it doesn't and I'm pretty stumped at this point. The error I'm receiving is: <code>Exception in thread "main" org.openqa.selenium.TimeoutException: Timed out after 20 seconds waiting for visibility of element located by By.id: ui-datepicker-div</code></p>

<p>I've set up a few Wait statements for the date elements I'm clicking against, but I'm still receiving an error. I'm trying to find a solution where I can avoid using Thread.sleep. Any help would be greatly appreciated!</p>

<p>Here is my script:</p>

<pre><code>public class DatePickerTest {
    private static WebDriver driver = new FirefoxDriver();
    static WebDriverWait wait = new WebDriverWait(driver, 20);

    public static void main(String[] args) {    
        driver.get("http://lowfares.com");
        selectDepartDate(2016, 5, 18);
        selectReturnDate(2016, 5, 21);
    }

    public static void selectDepartDate(int year, int month, int day) {
        month--; // replacing zero based index
        Integer.toString(year);
        Integer.toString(month);
        Integer.toString(day);

        wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("depart_date")));

        // click on text box to open up date picker calendar
        driver.findElement(By.id("depart_date")).click();

        // close pop up window
        String parentWindowHandler = driver.getWindowHandle();
        String subWindowHandler = null;
        Set&lt;String&gt; handles = driver.getWindowHandles();
        Iterator&lt;String&gt; iterator = handles.iterator();
        while (iterator.hasNext()) {
            subWindowHandler = iterator.next();
        }
        driver.switchTo().window(subWindowHandler);
        driver.close();
        driver.switchTo().window(parentWindowHandler);

        // wait for calendar interface to appear
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("ui-datepicker-div")));

        // select depart date
        driver.findElement(By.xpath("//td[contains(@data-month, " + month + ") and contains(@data-year, " + year + ")]//a[contains(text(), " + day + ")]")).click();
    }

    public static void selectReturnDate(int year, int month, int day) {
        month--; // replacing zero based index
        Integer.toString(year);
        Integer.toString(month);
        Integer.toString(day);

        wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("return_date")));

        // click on text box to open up date picker calendar
        driver.findElement(By.id("return_date")).click();

        // wait for calendar interface to appear
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("ui-datepicker-div")));

        // select return date
        driver.findElement(By.xpath("//td[contains(@data-month, " + month + ") and contains(@data-year, " + year + ")]//a[contains(text(), " + day + ")]")).click();
    }
}
</code></pre>