Selenium WebDriver CSS选择器帮助 - 用于选择日期 [英] Selenium WebDriver CSS Selector Help - for Selecting Date

查看:159
本文介绍了Selenium WebDriver CSS选择器帮助 - 用于选择日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从小窗口中选择日期,下面是html代码的示例。有人可以请帮助我通过CSSSelector或任何其他方法选择一个日期,这是最好的做这个任务吗?

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>

任何问题,请询问。

谢谢

推荐答案

对于 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);

这篇关于Selenium WebDriver CSS选择器帮助 - 用于选择日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆