如何使用Selenium WebDriver从预订网站的日历中选择今天和明天的两个日期

时间:2018-06-21 19:13:03

标签: selenium selenium-webdriver automated-tests

Screenshot from the calender

我尝试使用下面的代码来检测“签到”按钮,但是我无法确定如何选择日期,如上图所示。

public class Hotel_Search {

    void search(WebDriver driver) {
        // find destination WebElement des = driver.findElement(By.name("ss"));
        // fill destination des.sendKeys("Ain Sokhna");
        // select checkin button WebElement Checkinhbutton = driver.findElement(By.xpath("html/body/div[3]/div/div/div[2]/form/div1/div[2]/div/div[2]/div/div/div/div1/div/button")); Checkinhbutton.click(); //call select date of today method SelectDateOfToday(driver.findElement(By.xpath("html/body/div[3]/div/div/div[2]/form/div1/div[2]/div/div[2]/div/div/div/div[2]/div[2]/div[3]/div/div/div1/table/thead/tr1/th"))); //find search button WebElement searchbutton = driver.findElement(By.xpath("html/body/div[3]/div/div/div[2]/form/div1/div[4]/div[2]/button"));
        // searchbutton.click(); }
        // public void SelectDateOfToday(WebElement Calender_Xpath) {

        String today = getCurrentDay();
        List<WebElement> columns = Calender_Xpath.findElements(By.tagName("td"));
        for (WebElement cell : columns) {
            //If you want to click 18th Date if (cell.getText().equals("18")) {
            // Select Today's Date
            if (cell.getText().equals(today)) {
                cell.click();
                break;
            }
        }
    }

    private String getCurrentDay() {
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate localDate = LocalDate.now();
        String date = dtf.format(localDate);
        return date;

    }
}

2 个答案:

答案 0 :(得分:0)

从您的问题中我了解到,您正在尝试与https://www.booking.com/进行互动。要选择日期,您可以尝试以下操作:

ember build --environment=qa

输出:

enter image description here

答案 1 :(得分:0)

这是替代解决方案

@Test
public void calendarSelector() throws Exception {
    driver.get("https://www.booking.com/");
    WebDriverWait wait = new WebDriverWait(driver, 15, 100);

    //Define locators in advance (Really should move this stuff into a page object)
    By checkInLocator = By.xpath("//div[contains(@class, 'b-datepicker')][@data-mode='checkin']");
    By calendarLocator = By.cssSelector(".bui-calendar__content");

    //Wait until checkIn element is displayed and then click on it
    wait.until(ExpectedConditions.visibilityOfElementLocated(checkInLocator)).click();
    //Wait until calendar is displayed
    wait.until(ExpectedConditions.visibilityOfElementLocated(calendarLocator));

    //Work out today and tomorrow
    LocalDate today = LocalDate.now();
    LocalDate tomorrow = today.plusDays(1L);

    //Use selectDate method to click on the relevant dates
    selectDate(driver, today);
    selectDate(driver, tomorrow);
}

private void selectDate(WebDriver driver, LocalDate date) {
    //Looking at the markup the attribute data-date is formatted as an ISO_LOCAL_DATE
    DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE;
    WebDriverWait wait = new WebDriverWait(driver, 15, 100);
    //Programmatically generate dateLocator based on date passed in
    By dateLocator = By.xpath(String.format("//td[@data-date='%s']", formatter.format(date)));
    //Wait for date element to be visible, then click on it
    wait.until(ExpectedConditions.visibilityOfElementLocated(dateLocator)).click();
}

如果您要搜索未在日历小部件的首次打开时显示的日期,则选择日期方法将需要稍微复杂一些,您需要确定要显示的日期,并将其与所需日期进行比较然后适当地单击“月份”导航按钮,虽然您当前的情况并不需要该附加功能。