WebDriver 获取最后一个日历日

WebDriver get last calendar day

我需要在日历中获取当前月份的最后一天:http://prntscr.com/7z506w

这是 HTML 代码:

<table class="ui-datepicker-calendar">
    <thead>
    <tbody>
        <tr>
        <tr>
        <tr>
        <tr>
            <td
                class=" ui-datepicker-week-end ui-datepicker-unselectable ui-state-disabled null"
                title="Cannot choose a date earlier than a previous anchor task or the start date.">
            <td class=" ui-datepicker-unselectable ui-state-disabled null"
                title="Cannot choose a date earlier than a previous anchor task or the start date.">
            <td class=" ui-datepicker-unselectable ui-state-disabled null"
                title="Cannot choose a date earlier than a previous anchor task or the start date.">
            <td class=" ui-datepicker-unselectable ui-state-disabled null"
                title="Cannot choose a date earlier than a previous anchor task or the start date.">
            <td class=" ui-datepicker-unselectable ui-state-disabled null"
                title="Cannot choose a date earlier than a previous anchor task or the start date.">
            <td class=" ui-datepicker-unselectable ui-state-disabled null"
                title="Cannot choose a date earlier than a previous anchor task or the start date.">
            <td
                class=" ui-datepicker-week-end ui-datepicker-unselectable ui-state-disabled null"
                title="Cannot choose a date earlier than a previous anchor task or the start date.">
        </tr>
        <tr>
            <td
                class=" ui-datepicker-week-end ui-datepicker-unselectable ui-state-disabled null"
                title="Cannot choose a date earlier than a previous anchor task or the start date.">
                <span class="ui-state-default">26</span>
            </td>
            <td class=" ui-datepicker-unselectable ui-state-disabled null"
                title="Cannot choose a date earlier than a previous anchor task or the start date.">
                <span class="ui-state-default">27</span>
            </td>
            <td class=" ui-datepicker-unselectable ui-state-disabled null"
                title="Cannot choose a date earlier than a previous anchor task or the start date.">
                <span class="ui-state-default">28</span>
            </td>
            <td class=" ui-datepicker-unselectable ui-state-disabled null"
                title="Cannot choose a date earlier than a previous anchor task or the start date.">
                <span class="ui-state-default">29</span>
            </td>
            <td class=" ui-datepicker-unselectable ui-state-disabled null"
                title="Cannot choose a date earlier than a previous anchor task or the start date.">
                <span class="ui-state-default">30</span>
            </td>
            <td class=" ui-datepicker-days-cell-over null ui-datepicker-today"
                onclick="DP_jQuery_1438322656964.datepicker._selectDay('#FormElement_date_289_input',6,2015, this);return false;">
                <a class="ui-state-default ui-state-highlight ui-state-hover"
                href="#">31</a>
            </td>
            <td class=" ui-datepicker-week-end ui-datepicker-other-month ui-datepicker-unselectable ui-state-disabled"/>
        </tr>
    </tbody>
</table>

和截图


那么如何知道它是日历中的最后一个元素,并在 Selenium WebDriver 中获取它?

由于 table 中最后一行的末尾还有另一个 td 元素,因此您需要排除将此元素与其他元素区分开来的 class -> "ui-datepicker-other-month"

鉴于你的例子,你可以尝试:

WebElement lastDay = driver.findElement(By.xpath("((//table//tr)[last()]//td[not (contains(@class, 'ui-datepicker-other-month'))])[last()]"));

如果你想看 31:

System.out.println(lastDay.getText());

这里是全部HTMLtable

<table class="ui-datepicker-calendar">
<thead>
<tbody>
<tr>
<tr>
<tr>
<tr>
<tr>
<td class=" ui-datepicker-week-end ui-datepicker-unselectable ui-state-disabled null" title="Cannot choose a date earlier than a previous anchor task or the start date.">
<td class=" ui-datepicker-unselectable ui-state-disabled null" title="Cannot choose a date earlier than a previous anchor task or the start date.">
<td class=" ui-datepicker-unselectable ui-state-disabled null" title="Cannot choose a date earlier than a previous anchor task or the start date.">
<td class=" ui-datepicker-unselectable ui-state-disabled null" title="Cannot choose a date earlier than a previous anchor task or the start date.">
<td class=" ui-datepicker-unselectable ui-state-disabled null" title="Cannot choose a date earlier than a previous anchor task or the start date.">
<td class=" ui-datepicker-days-cell-over null ui-datepicker-today" onclick="DP_jQuery_1438322656964.datepicker._selectDay('#FormElement_date_289_input',6,2015, this);return false;">
<a class="ui-state-default ui-state-highlight ui-state-hover" href="#">31</a>
</td>
<td class=" ui-datepicker-week-end ui-datepicker-other-month ui-datepicker-unselectable ui-state-disabled"/>
</tr>
</tbody>
</table>