javaselenium-webdriver

How to pick the date from dropdown date picker using Selenium


Please help me understand how to pick the date from the date picker. I tried options 1 and 2, below, but I get NoSuchElementException.

driver.get("https://rahulshettyacademy.com/dropdownsPractise/");
driver.findElement(By.id("ctl00_mainContent_ddl_originStation1_CTXT")).click();
driver.findElement(By.xpath("//a[@value='BLR']")).click();
driver.findElement(By.xpath("(//a[@value='MAA'])[2]")).click();
Thread.sleep(5000);

// 1
driver.findElement(By.xpath("(//a[@class='ui-state-default ui-state-active ui-state-hover'])[1]")).click();

// 2
driver.findElement(By.cssSelector("a[class='ui-state-default ui-state-highlight']")).click();

Solution

  • Your locators were not correct. Also, you need to add WebDriverWaits for all of these elements because the airport and date panels don't show up instantly.

    I rewrote your code to use methods for setting the origination airport, destination airport, and departure date. This makes the code much easier to read and to reuse. Ideally you'd put all of this into a page object but I didn't go that far. I'll leave that as an exercise for you to investigate.

    import java.time.Duration;
    import java.time.LocalDate;
    
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.support.ui.ExpectedConditions;
    import org.openqa.selenium.support.ui.WebDriverWait;
    
    public class SeleniumSandbox {
        static WebDriver driver;
        static WebDriverWait wait;
    
        public static void main(String[] args) throws Exception {
            String url = "https://rahulshettyacademy.com/dropdownsPractise";
    
            driver = new ChromeDriver();
            driver.manage().window().maximize();
            driver.get(url);
            wait = new WebDriverWait(driver, Duration.ofSeconds(10));
    
            setOriginationAirport("BLR");
            setDestinationAirport("MAA");
            setDepartureDate(LocalDate.of(2019, 5, 25));
    
            driver.quit();
        }
    
        public static void setDepartureDate(LocalDate departureDate) {
            wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@id='ui-datepicker-div']//td[@data-month='" + departureDate.getMonthValue() + "']/a[text()='" + departureDate.getDayOfMonth() + "']"))).click();
        }
    
        public static void setDestinationAirport(String airportCode) {
            wait.until(ExpectedConditions.elementToBeClickable(By.id("ctl00_mainContent_ddl_destinationStation1_CTXT"))).click();
            wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#ctl00_mainContent_ddl_destinationStation1_CTNR table a[value='" + airportCode + "']"))).click();
        }
    
        public static void setOriginationAirport(String airportCode) {
            wait.until(ExpectedConditions.elementToBeClickable(By.id("ctl00_mainContent_ddl_originStation1_CTXT"))).click();
            wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#ctl00_mainContent_ddl_originStation1_CTNR table a[value='" + airportCode + "']"))).click();
        }
    }