如何在日历中选择当前日期和未来日期

时间:2019-06-06 15:37:01

标签: java selenium selenium-webdriver

如何在日历中选择当前日期和将来日期? Select current and future date

我想选择6月6日和7月7日。当我今天运行时,下面的代码可以正常工作 但是当我明天运行时,此脚本将失败,因为6月6日将被禁用。如何在日历中选择当前日期和未来日期

List<WebElement>dates1=driver.findElements(By.xpath("//div[@class='rc-calendar-date-value']"));
int dd=dates1.size();

for(int i=0; i<dd; i++)
{
    String date=dates1.get(i).getText();
    if(date.equalsIgnoreCase(“6”))
    {
        dates1.get(i).click();
        break;
    }
}
for(int i=0; i<dd; i++)
{
    String date=dates1.get(i).getText();
    if(date.equalsIgnoreCase(“7”))
    {
        dates1.get(i).click();
        break;
    }
}

上面的代码明天运行时将失败。

HTML:

<div class="rc-calendar-body">
    <table class="rc-calendar-table" cellspacing="0" role="grid">
        <thead>
            <tr role="row">
                <th role="columnheader" title="Sun" class="rc-calendar-column-header"><span class="rc-calendar-column-header-inner">Su</span></th>
                <th role="columnheader" title="Mon" class="rc-calendar-column-header"><span class="rc-calendar-column-header-inner">Mo</span></th>
                <th role="columnheader" title="Tue" class="rc-calendar-column-header"><span class="rc-calendar-column-header-inner">Tu</span></th>
                <th role="columnheader" title="Wed" class="rc-calendar-column-header"><span class="rc-calendar-column-header-inner">We</span></th>
                <th role="columnheader" title="Thu" class="rc-calendar-column-header"><span class="rc-calendar-column-header-inner">Th</span></th>
                <th role="columnheader" title="Fri" class="rc-calendar-column-header"><span class="rc-calendar-column-header-inner">Fr</span></th>
                <th role="columnheader" title="Sat" class="rc-calendar-column-header"><span class="rc-calendar-column-header-inner">Sa</span></th>
            </tr>
        </thead>
        <tbody class="rc-calendar-tbody">
            <tr role="row" class="">
                <td role="gridcell" title="2019-5-26" class="rc-calendar-cell rc-calendar-last-month-cell rc-calendar-disabled-cell-first-of-row rc-calendar-disabled-cell">
                    <div class="rc-calendar-date" aria-selected="false" aria-disabled="true">
                        <div class="rc-calendar-date-value">26</div>
                    </div>
                </td>
                <td role="gridcell" title="2019-5-27" class="rc-calendar-cell rc-calendar-last-month-cell rc-calendar-disabled-cell">
                    <div class="rc-calendar-date" aria-selected="false" aria-disabled="true">
                        <div class="rc-calendar-date-value">27</div>
                    </div>
                </td>
                <td role="gridcell" title="2019-5-28" class="rc-calendar-cell rc-calendar-last-month-cell rc-calendar-disabled-cell">
                    <div class="rc-calendar-date" aria-selected="false" aria-disabled="true">
                        <div class="rc-calendar-date-value">28</div>
                    </div>
                </td>
                <td role="gridcell" title="2019-5-29" class="rc-calendar-cell rc-calendar-last-month-cell rc-calendar-disabled-cell">
                    <div class="rc-calendar-date" aria-selected="false" aria-disabled="true">
                        <div class="rc-calendar-date-value">29</div>
                    </div>
                </td>
                <td role="gridcell" title="2019-5-30" class="rc-calendar-cell rc-calendar-last-month-cell rc-calendar-disabled-cell">
                    <div class="rc-calendar-date" aria-selected="false" aria-disabled="true">
                        <div class="rc-calendar-date-value">30</div>
                    </div>
                </td>
                <td role="gridcell" title="2019-5-31" class="rc-calendar-cell rc-calendar-last-month-cell rc-calendar-disabled-cell">
                    <div class="rc-calendar-date" aria-selected="false" aria-disabled="true">
                        <div class="rc-calendar-date-value">31</div>
                    </div>
                </td>
                <td role="gridcell" title="2019-6-1" class="rc-calendar-cell rc-calendar-first-day-of-month-cell rc-calendar-disabled-cell-last-of-row rc-calendar-disabled-cell">
                    <div class="rc-calendar-date" aria-selected="false" aria-disabled="true">
                        <div class="rc-calendar-date-value">1</div>
                    </div>
                </td>
            </tr>
            <tr role="row" class="rc-calendar-current-week rc-calendar-active-week">
                <td role="gridcell" title="2019-6-2" class="rc-calendar-cell rc-calendar-disabled-cell-first-of-row rc-calendar-disabled-cell">
                    <div class="rc-calendar-date" aria-selected="false" aria-disabled="true">
                        <div class="rc-calendar-date-value">2</div>
                    </div>
                </td>
                <td role="gridcell" title="2019-6-3" class="rc-calendar-cell rc-calendar-disabled-cell">
                    <div class="rc-calendar-date" aria-selected="false" aria-disabled="true">
                        <div class="rc-calendar-date-value">3</div>
                    </div>
                </td>
                <td role="gridcell" title="2019-6-4" class="rc-calendar-cell rc-calendar-disabled-cell">
                    <div class="rc-calendar-date" aria-selected="false" aria-disabled="true">
                        <div class="rc-calendar-date-value">4</div>
                    </div>
                </td>
                <td role="gridcell" title="2019-6-5" class="rc-calendar-cell rc-calendar-disabled-cell-last-of-row rc-calendar-disabled-cell">
                    <div class="rc-calendar-date" aria-selected="false" aria-disabled="true">
                        <div class="rc-calendar-date-value">5</div>
                    </div>
                </td>
                <td role="gridcell" title="2019-6-6" class="rc-calendar-cell rc-calendar-today rc-calendar-first-selected-day rc-calendar-selected-day">
                    <div class="rc-calendar-date" aria-selected="true" aria-disabled="false">
                        <div class="rc-calendar-date-value">6</div>
                    </div>
                </td>
                <td role="gridcell" title="2019-6-7" class="rc-calendar-cell rc-calendar-in-range-cell">
                    <div class="rc-calendar-date" aria-selected="false" aria-disabled="false">
                        <div class="rc-calendar-date-value">7</div>
                    </div>
                </td>
                <td role="gridcell" title="2019-6-8" class="rc-calendar-cell rc-calendar-last-selected-day rc-calendar-selected-day">
                    <div class="rc-calendar-date" aria-selected="true" aria-disabled="false">
                        <div class="rc-calendar-date-value">8</div>
                    </div>
                </td>
            </tr>
            <tr role="row" class="">
                <td role="gridcell" title="2019-6-9" class="rc-calendar-cell">
                    <div class="rc-calendar-date" aria-selected="false" aria-disabled="false">
                        <div class="rc-calendar-date-value">9</div>
                    </div>
                </td>
                <td role="gridcell" title="2019-6-10" class="rc-calendar-cell">
                    <div class="rc-calendar-date" aria-selected="false" aria-disabled="false">
                        <div class="rc-calendar-date-value">10</div>
                    </div>
                </td>
                <td role="gridcell" title="2019-6-11" class="rc-calendar-cell">
                    <div class="rc-calendar-date" aria-selected="false" aria-disabled="false">
                        <div class="rc-calendar-date-value">11</div>
                    </div>
                </td>
                <td role="gridcell" title="2019-6-12" class="rc-calendar-cell">
                    <div class="rc-calendar-date" aria-selected="false" aria-disabled="false">
                        <div class="rc-calendar-date-value">12</div>
                    </div>
                </td>
                <td role="gridcell" title="2019-6-13" class="rc-calendar-cell">
                    <div class="rc-calendar-date" aria-selected="false" aria-disabled="false">
                        <div class="rc-calendar-date-value">13</div>
                    </div>
                </td>
                <td role="gridcell" title="2019-6-14" class="rc-calendar-cell">
                    <div class="rc-calendar-date" aria-selected="false" aria-disabled="false">
                        <div class="rc-calendar-date-value">14</div>
                    </div>
                </td>
                <td role="gridcell" title="2019-6-15" class="rc-calendar-cell">
                    <div class="rc-calendar-date" aria-selected="false" aria-disabled="false">
                        <div class="rc-calendar-date-value">15</div>
                    </div>
                </td>
            </tr>
            <tr role="row" class="">
                <td role="gridcell" title="2019-6-16" class="rc-calendar-cell">
                    <div class="rc-calendar-date" aria-selected="false" aria-disabled="false">
                        <div class="rc-calendar-date-value">16</div>
                    </div>
                </td>
                <td role="gridcell" title="2019-6-17" class="rc-calendar-cell">
                    <div class="rc-calendar-date" aria-selected="false" aria-disabled="false">
                        <div class="rc-calendar-date-value">17</div>
                    </div>
                </td>
                <td role="gridcell" title="2019-6-18" class="rc-calendar-cell">
                    <div class="rc-calendar-date" aria-selected="false" aria-disabled="false">
                        <div class="rc-calendar-date-value">18</div>
                    </div>
                </td>
                <td role="gridcell" title="2019-6-19" class="rc-calendar-cell">
                    <div class="rc-calendar-date" aria-selected="false" aria-disabled="false">
                        <div class="rc-calendar-date-value">19</div>
                    </div>
                </td>
                <td role="gridcell" title="2019-6-20" class="rc-calendar-cell">
                    <div class="rc-calendar-date" aria-selected="false" aria-disabled="false">
                        <div class="rc-calendar-date-value">20</div>
                    </div>
                </td>
                <td role="gridcell" title="2019-6-21" class="rc-calendar-cell">
                    <div class="rc-calendar-date" aria-selected="false" aria-disabled="false">
                        <div class="rc-calendar-date-value">21</div>
                    </div>
                </td>
                <td role="gridcell" title="2019-6-22" class="rc-calendar-cell">
                    <div class="rc-calendar-date" aria-selected="false" aria-disabled="false">
                        <div class="rc-calendar-date-value">22</div>
                    </div>
                </td>
            </tr>
            <tr role="row" class="">
                <td role="gridcell" title="2019-6-23" class="rc-calendar-cell">
                    <div class="rc-calendar-date" aria-selected="false" aria-disabled="false">
                        <div class="rc-calendar-date-value">23</div>
                    </div>
                </td>
                <td role="gridcell" title="2019-6-24" class="rc-calendar-cell">
                    <div class="rc-calendar-date" aria-selected="false" aria-disabled="false">
                        <div class="rc-calendar-date-value">24</div>
                    </div>
                </td>
                <td role="gridcell" title="2019-6-25" class="rc-calendar-cell">
                    <div class="rc-calendar-date" aria-selected="false" aria-disabled="false">
                        <div class="rc-calendar-date-value">25</div>
                    </div>
                </td>
                <td role="gridcell" title="2019-6-26" class="rc-calendar-cell">
                    <div class="rc-calendar-date" aria-selected="false" aria-disabled="false">
                        <div class="rc-calendar-date-value">26</div>
                    </div>
                </td>
                <td role="gridcell" title="2019-6-27" class="rc-calendar-cell">
                    <div class="rc-calendar-date" aria-selected="false" aria-disabled="false">
                        <div class="rc-calendar-date-value">27</div>
                    </div>
                </td>
                <td role="gridcell" title="2019-6-28" class="rc-calendar-cell">
                    <div class="rc-calendar-date" aria-selected="false" aria-disabled="false">
                        <div class="rc-calendar-date-value">28</div>
                    </div>
                </td>
                <td role="gridcell" title="2019-6-29" class="rc-calendar-cell">
                    <div class="rc-calendar-date" aria-selected="false" aria-disabled="false">
                        <div class="rc-calendar-date-value">29</div>
                    </div>
                </td>
            </tr>
            <tr role="row" class="">
                <td role="gridcell" title="2019-6-30" class="rc-calendar-cell rc-calendar-last-day-of-month-cell">
                    <div class="rc-calendar-date" aria-selected="false" aria-disabled="false">
                        <div class="rc-calendar-date-value">30</div>
                    </div>
                </td>
                <td role="gridcell" title="2019-7-1" class="rc-calendar-cell rc-calendar-next-month-btn-day">
                    <div class="rc-calendar-date" aria-selected="false" aria-disabled="false">
                        <div class="rc-calendar-date-value">1</div>
                    </div>
                </td>
                <td role="gridcell" title="2019-7-2" class="rc-calendar-cell rc-calendar-next-month-btn-day">
                    <div class="rc-calendar-date" aria-selected="false" aria-disabled="false">
                        <div class="rc-calendar-date-value">2</div>
                    </div>
                </td>
                <td role="gridcell" title="2019-7-3" class="rc-calendar-cell rc-calendar-next-month-btn-day">
                    <div class="rc-calendar-date" aria-selected="false" aria-disabled="false">
                        <div class="rc-calendar-date-value">3</div>
                    </div>
                </td>
                <td role="gridcell" title="2019-7-4" class="rc-calendar-cell rc-calendar-next-month-btn-day">
                    <div class="rc-calendar-date" aria-selected="false" aria-disabled="false">
                        <div class="rc-calendar-date-value">4</div>
                    </div>
                </td>
                <td role="gridcell" title="2019-7-5" class="rc-calendar-cell rc-calendar-next-month-btn-day">
                    <div class="rc-calendar-date" aria-selected="false" aria-disabled="false">
                        <div class="rc-calendar-date-value">5</div>
                    </div>
                </td>
                <td role="gridcell" title="2019-7-6" class="rc-calendar-cell rc-calendar-next-month-btn-day">
                    <div class="rc-calendar-date" aria-selected="false" aria-disabled="false">
                        <div class="rc-calendar-date-value">6</div>
                    </div>
                </td>
            </tr>
        </tbody>
    </table>
</div>

请对此提供帮助。

2 个答案:

答案 0 :(得分:0)

由于您要在日历中选择多个日期,因此最好将页面对象用于Selenium:

public class DatePickerPageObject {
    [FindsBy(how = How.CSS, Using = "table.rc-calendar-table")]
    private WebElement table;

    private SimpleDateFormat calendarDateFormat;

    public DatePicker(WebDriver driver) {
        calendarDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        PageFactory.initElements(driver, this);
    }

    public void chooseDate(Date dateToChoose) {
        String xpath = "./tbody/td[@title = '" + calendarDateFormat.format(date) + "']";
        WebElement dateCell = table.findElement(By.xpath(xpath);

        dateCell.click();
    }

    public void chooseTodayAndTomorrow() {
        Date today = new Date();
        Calendar calendar = Calendar.getInstance();

        calendar.setTime(today);
        calendar.add(1);
        Date tomorrow = calendar.getTime();

        chooseDate(today);
        chooseDate(tomorrow);
    }
}

这至少掩盖了生成“今天”和“明天”作为适当的Date对象,然后在页面上的日历小部件中选择它们的复杂性。

然后使用日期选择器很简单:

DatePickerPageObject datePicker = new DatePickerPageObject(driver);

datePicker.chooseTodayAndTomorrow();

关于创建负责此行为的类的好处是,除了隐藏与日期选择器进行交互的复杂性之外,还可以稍后在该类中添加能够执行更复杂行为的方法,例如选择一个日期范围较大,例如:

Date today = new Date();
Calendar calendar = Calendar.getInstance();

calendar.setTime(today);
calendar.add(7);
Date oneWeekFromToday= calendar.getTime();

datePicker.chooseRange(today, oneWeekFromToday);

(但是我将由您自己决定实施)

答案 1 :(得分:0)

首先,您必须获取当前日期并传递日期,而不是传递静态值。 只需添加current_date + 1,然后运行代码以获取将来的价值。

SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");  
Date date1 = new Date(); 
String date2=formatter.format(date1);
date = new SimpleDateFormat("d").parse(date2);
String currentDate = new SimpleDateFormat("d",Locale.getDefault()).format(date);

在上面的代码中,您将获得当前日期。然后您可以选择当前日期

for (int i=0; i<dd; i++) {
    String date=dates1.get(i).getText();
    if(date.equalsIgnoreCase(currentDate)) {
        dates1.get(i).click();
        break;
    }
}

现在,将来日期的代码将是

int FutureDate=currentDate+1;
for (int i=0; i<dd; i++) {
    String date=dates1.get(i).getText();
    if(date.equalsIgnoreCase(FutureDate)) {
        dates1.get(i).click();
        break;
    }
}