处理日历以从日历中的当前日期选择至今?硒

时间:2019-04-23 08:57:28

标签: java selenium selenium-webdriver automation automated-tests

我正在MakeMyTrip网站上处理压光机。在选择了起始日期之后,我要选择从选定起始日期起7或8天后的起始日期。

链接:https://www.makemytrip.com/

我可以使用具有今天的班级名称选择当前日期

我不知道如何选择日期,例如从日期起n天后

HTML代码: enter image description here

@FindBy(how = How.XPATH,using = "//div[@class = 'DayPicker-Month'][1]//div[@class='DayPicker-Body']//div[contains(@class,'DayPicker-Day')]")
List<WebElement> DepartureDateList;

//Selecting Departure Date
public void selectDepartureDate() {

    for ( WebElement date : DepartureDateList) {

        if (date.getAttribute("class").contains("--today")) {

            date.click();
            break;

        }

1 个答案:

答案 0 :(得分:0)

这是简单的解决方案。

首先使用以下代码获取第n天。

    int numberOfDays = 7;
    DateFormat dateFormat = new SimpleDateFormat("MMM dd yyyy");
    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.DATE, numberOfDays);
    String toDate = dateFormat.format(cal.getTime());
    System.out.println(toDate);

然后使用下面的xpath在日历中选择数据。

String toDateXpath = "//div[@class='DayPicker-Day' and contains(@aria-label,'" + toDate + "')]"
driver.findElement(By.xpath(toDateXpath)).click();