admin管理员组

文章数量:1125725

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("/");
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();

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();
Share Improve this question edited Jan 9 at 5:23 JeffC 25.5k5 gold badges34 silver badges55 bronze badges asked Jan 9 at 5:13 amalamal 3,57010 gold badges30 silver badges45 bronze badges 1
  • Isnt that just because there is no elements that have ui-state-hover or ui-state-highlight? I went to check the webpage with developer tools. its ui-state-default ui-state-active or ui-state-default – pebble unit Commented Jan 9 at 5:43
Add a comment  | 

1 Answer 1

Reset to default 2

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();
    }
}

本文标签: javaHow to pick the date from dropdown date picker using SeleniumStack Overflow