【问题标题】:Selenium WebDriver CSS Selector Help - for Selecting DateSelenium WebDriver CSS 选择器帮助 - 用于选择日期
【发布时间】:2016-07-16 02:18:02
【问题描述】:

我需要从小窗口中选择日期,下面是 html 代码示例。有人可以帮我通过 CSSSelector 或任何其他最适合完成此任务的方式选择日期吗?

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

有任何问题,欢迎提问。

谢谢

【问题讨论】:

标签: css selenium


【解决方案1】:

对于JQueryUI DatePicker 这应该可以工作

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

用法

    selectDate(26, 6, 1992);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-22
    • 2018-01-17
    • 1970-01-01
    相关资源
    最近更新 更多