【问题标题】:Handling calender to select to date from the current date in the calender? Selenium处理日历以从日历中的当前日期选择日期?硒
【发布时间】:2019-09-12 10:14:45
【问题描述】:

我在 MakeMyTrip 网站工作以处理日历。选择起始日期后,我想选择从所选起始日期起 7 或 8 天后的日期..

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

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

我不知道如何在从日期后 n 天后选择截止日期

HTML 代码:

@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;

        }

【问题讨论】:

  • 您要选择/选择哪个日期?
  • 想选择当前日期后 7 天!

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


【解决方案1】:

这是简单的解决方案。

首先使用下面的代码获取第 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();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-09
    • 1970-01-01
    • 2021-10-29
    • 2021-02-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多