简体   繁体   中英

Selenium WebDriver CSS Selector Help - for Selecting Date

I need to select the date from the small window and below is the sample of html code. Can someone please help me to select a date by CSSSelector or by any other means which is best to do this task ?

<td class=" " onclick="DP_jQuery_1468593787056.datepicker._selectDay('#dp1468593787059',11,2016, this);return false;">
<a class="ui-state-default" href="#">1</a>
</td>
<td class=" " onclick="DP_jQuery_1468593787056.datepicker._selectDay('#dp1468593787059',11,2016, this);return false;">
<a class="ui-state-default" href="#">2</a>
</td>
<td class=" ui-datepicker-week-end " onclick="DP_jQuery_1468593787056.datepicker._selectDay('#dp1468593787059',11,2016, this);return false;">
<a class="ui-state-default" href="#">3</a>

any questions, please ask.

thank you

For JQueryUI DatePicker This should work

   public void selectDate(int day, int month, int year) {
        int currentYear = Integer.valueOf(driver.findElement(By.xpath("//td[@data-year]")).getAttribute("data-year"));
        int currentMonth = Integer.valueOf(driver.findElement(By.xpath("//td[@data-month]")).getAttribute("data-month"));

        Boolean isNext = (year == currentYear && month >= currentMonth)
                || year > currentYear;

        String navTo = isNext ? "next" : "prev";

        String dayXpath = "//td[@data-year='" + year + "' and @data-month='" + month + "'][a[text()='" + day + "']]";

        Boolean found = false;
        do {
            List<WebElement> dayEl = driver.findElements(By.xpath(dayXpath));
            if (!dayEl.isEmpty()) {
                dayEl.get(0).click();
                found = true;
                break;
            }
        } while (navigateTo(navTo));

        if (!found) {
            System.out.println("Couldn't Find the date");
        }

    }

    public Boolean navigateTo(String nav) {
        List<WebElement> navigate = driver.findElements(By.xpath("//a[@data-handler='" + nav + "']"));
        if (!navigate.isEmpty()) {
            navigate.get(0).click();
            return true;
        } else {
            System.out.println("not found" + nav);
        }
        return false;
    }

Usage

    selectDate(26, 6, 1992);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM