Watir-webdriver 如何访问最后一个匹配元素而不是第一个?

发布于 2024-12-27 01:21:04 字数 6732 浏览 2 评论 0原文

在动态网页上,我不知道预先匹配元素的数量,因此我不知道要使用哪个索引。

watir-webdriver 是否提供了一种访问最后一个匹配元素的方法,而不是默认的第一个匹配元素?

相关 HTML 形成日历控件

<table border="0" class="body-style">
  <tbody>
    <tr>
      <td width="27" align="right"><b>Sun</b></td>
      <td width="27" align="right"><b>Mon</b></td>
      <td width="27" align="right"><b>Tue</b></td>
      <td width="27" align="right"><b>Wed</b></td>
      <td width="27" align="right"><b>Thu</b></td>
      <td width="27" align="right"><b>Fri</b></td>
      <td width="27" align="right"><b>Sat</b></td></tr>
    <tr>
      <td align="right"><p class="end-of-weekday-style">Â&nbsp;1Â&nbsp;</p></td>
      <td align="right"><p class="end-of-weekday-style">Â&nbsp;2Â&nbsp;</p></td>
      <td align="right"><p class="end-of-weekday-style">Â&nbsp;3Â&nbsp;</p></td>
      <td align="right"><p class="end-of-weekday-style">Â&nbsp;4Â&nbsp;</p></td>
      <td align="right"><p class="end-of-weekday-style">Â&nbsp;5Â&nbsp;</p></td>
      <td align="right"><p class="end-of-weekday-style">Â&nbsp;6Â&nbsp;</p></td>
      <td align="right"><p class="end-of-weekday-style">Â&nbsp;7Â&nbsp;</p></td></tr>
    <tr>
      <td align="right"><p class="end-of-weekday-style">Â&nbsp;8Â&nbsp;</p></td>
      <td align="right"><p class="end-of-weekday-style">Â&nbsp;9Â&nbsp;</p></td>
      <td align="right"><p class="end-of-weekday-style">Â&nbsp;10Â&nbsp;</p></td>
      <td align="right"><p class="end-of-weekday-style">Â&nbsp;11Â&nbsp;</p></td>
      <td align="right"><p class="end-of-weekday-style">Â&nbsp;12Â&nbsp;</p></td>
      <td align="right"><a class="current-day-style selected-day-style" onmousemove="window.status=&quot; &quot;" onmouseout="window.status=&quot; &quot;" title="" href="javascript:dateSelected=13;closeCalendar();doOtherFunctions();">Â&nbsp;13Â&nbsp;</a></td>
      <td align="right"><p class="end-of-weekday-style">Â&nbsp;14Â&nbsp;</p></td></tr>
    <tr>
      <td align="right"><p class="end-of-weekday-style">Â&nbsp;15Â&nbsp;</p></td>
      <td align="right"><a class="normal-day-style" onmousemove="window.status=&quot; &quot;" onmouseout="window.status=&quot; &quot;" title="" href="javascript:dateSelected=16;closeCalendar();doOtherFunctions();">Â&nbsp;16Â&nbsp;</a></td>
      <td align="right"><a class="normal-day-style" onmousemove="window.status=&quot; &quot;" onmouseout="window.status=&quot; &quot;" title="" href="javascript:dateSelected=17;closeCalendar();doOtherFunctions();">Â&nbsp;17Â&nbsp;</a></td>
      <td align="right"><a class="normal-day-style" onmousemove="window.status=&quot; &quot;" onmouseout="window.status=&quot; &quot;" title="" href="javascript:dateSelected=18;closeCalendar();doOtherFunctions();">Â&nbsp;18Â&nbsp;</a></td>
      <td align="right"><a class="normal-day-style" onmousemove="window.status=&quot; &quot;" onmouseout="window.status=&quot; &quot;" title="" href="javascript:dateSelected=19;closeCalendar();doOtherFunctions();">Â&nbsp;19Â&nbsp;</a></td>
      <td align="right"><a class="normal-day-style" onmousemove="window.status=&quot; &quot;" onmouseout="window.status=&quot; &quot;" title="" href="javascript:dateSelected=20;closeCalendar();doOtherFunctions();">Â&nbsp;20Â&nbsp;</a></td>
      <td align="right"><p class="end-of-weekday-style">Â&nbsp;21Â&nbsp;</p></td></tr>
    <tr>
      <td align="right"><p class="end-of-weekday-style">Â&nbsp;22Â&nbsp;</p></td>
      <td align="right"><a class="normal-day-style" onmousemove="window.status=&quot; &quot;" onmouseout="window.status=&quot; &quot;" title="" href="javascript:dateSelected=23;closeCalendar();doOtherFunctions();">Â&nbsp;23Â&nbsp;</a></td>
      <td align="right"><a class="normal-day-style" onmousemove="window.status=&quot; &quot;" onmouseout="window.status=&quot; &quot;" title="" href="javascript:dateSelected=24;closeCalendar();doOtherFunctions();">Â&nbsp;24Â&nbsp;</a></td>
      <td align="right"><a class="normal-day-style" onmousemove="window.status=&quot; &quot;" onmouseout="window.status=&quot; &quot;" title="" href="javascript:dateSelected=25;closeCalendar();doOtherFunctions();">Â&nbsp;25Â&nbsp;</a></td>
      <td align="right"><a class="normal-day-style" onmousemove="window.status=&quot; &quot;" onmouseout="window.status=&quot; &quot;" title="" href="javascript:dateSelected=26;closeCalendar();doOtherFunctions();">Â&nbsp;26Â&nbsp;</a></td>
      <td align="right"><a class="normal-day-style" onmousemove="window.status=&quot; &quot;" onmouseout="window.status=&quot; &quot;" title="" href="javascript:dateSelected=27;closeCalendar();doOtherFunctions();">Â&nbsp;27Â&nbsp;</a></td>
      <td align="right"><p class="end-of-weekday-style">Â&nbsp;28Â&nbsp;</p></td></tr>
    <tr>
      <td align="right"><p class="end-of-weekday-style">Â&nbsp;29Â&nbsp;</p></td>
      <td align="right"><a class="normal-day-style" onmousemove="window.status=&quot; &quot;" onmouseout="window.status=&quot; &quot;" title="" href="javascript:dateSelected=30;closeCalendar();doOtherFunctions();">Â&nbsp;30Â&nbsp;</a></td>
      <td align="right"><a class="normal-day-style" onmousemove="window.status=&quot; &quot;" onmouseout="window.status=&quot; &quot;" title="" href="javascript:dateSelected=31;closeCalendar();doOtherFunctions();">Â&nbsp;31Â&nbsp;</a></td></tr>
  </tbody>
</table>

On a dynamic web page I don't know the number of matching elements upfront, so I won't know which index # to use.

Does watir-webdriver provide a way to access the LAST matching element, not first as by default?

The HTML in question forms a calendar control

<table border="0" class="body-style">
  <tbody>
    <tr>
      <td width="27" align="right"><b>Sun</b></td>
      <td width="27" align="right"><b>Mon</b></td>
      <td width="27" align="right"><b>Tue</b></td>
      <td width="27" align="right"><b>Wed</b></td>
      <td width="27" align="right"><b>Thu</b></td>
      <td width="27" align="right"><b>Fri</b></td>
      <td width="27" align="right"><b>Sat</b></td></tr>
    <tr>
      <td align="right"><p class="end-of-weekday-style"> 1 </p></td>
      <td align="right"><p class="end-of-weekday-style"> 2 </p></td>
      <td align="right"><p class="end-of-weekday-style"> 3 </p></td>
      <td align="right"><p class="end-of-weekday-style"> 4 </p></td>
      <td align="right"><p class="end-of-weekday-style"> 5 </p></td>
      <td align="right"><p class="end-of-weekday-style"> 6 </p></td>
      <td align="right"><p class="end-of-weekday-style"> 7 </p></td></tr>
    <tr>
      <td align="right"><p class="end-of-weekday-style"> 8 </p></td>
      <td align="right"><p class="end-of-weekday-style"> 9 </p></td>
      <td align="right"><p class="end-of-weekday-style"> 10 </p></td>
      <td align="right"><p class="end-of-weekday-style"> 11 </p></td>
      <td align="right"><p class="end-of-weekday-style"> 12 </p></td>
      <td align="right"><a class="current-day-style selected-day-style" onmousemove="window.status=" "" onmouseout="window.status=" "" title="" href="javascript:dateSelected=13;closeCalendar();doOtherFunctions();"> 13 </a></td>
      <td align="right"><p class="end-of-weekday-style"> 14 </p></td></tr>
    <tr>
      <td align="right"><p class="end-of-weekday-style"> 15 </p></td>
      <td align="right"><a class="normal-day-style" onmousemove="window.status=" "" onmouseout="window.status=" "" title="" href="javascript:dateSelected=16;closeCalendar();doOtherFunctions();"> 16 </a></td>
      <td align="right"><a class="normal-day-style" onmousemove="window.status=" "" onmouseout="window.status=" "" title="" href="javascript:dateSelected=17;closeCalendar();doOtherFunctions();"> 17 </a></td>
      <td align="right"><a class="normal-day-style" onmousemove="window.status=" "" onmouseout="window.status=" "" title="" href="javascript:dateSelected=18;closeCalendar();doOtherFunctions();"> 18 </a></td>
      <td align="right"><a class="normal-day-style" onmousemove="window.status=" "" onmouseout="window.status=" "" title="" href="javascript:dateSelected=19;closeCalendar();doOtherFunctions();"> 19 </a></td>
      <td align="right"><a class="normal-day-style" onmousemove="window.status=" "" onmouseout="window.status=" "" title="" href="javascript:dateSelected=20;closeCalendar();doOtherFunctions();"> 20 </a></td>
      <td align="right"><p class="end-of-weekday-style"> 21 </p></td></tr>
    <tr>
      <td align="right"><p class="end-of-weekday-style"> 22 </p></td>
      <td align="right"><a class="normal-day-style" onmousemove="window.status=" "" onmouseout="window.status=" "" title="" href="javascript:dateSelected=23;closeCalendar();doOtherFunctions();"> 23 </a></td>
      <td align="right"><a class="normal-day-style" onmousemove="window.status=" "" onmouseout="window.status=" "" title="" href="javascript:dateSelected=24;closeCalendar();doOtherFunctions();"> 24 </a></td>
      <td align="right"><a class="normal-day-style" onmousemove="window.status=" "" onmouseout="window.status=" "" title="" href="javascript:dateSelected=25;closeCalendar();doOtherFunctions();"> 25 </a></td>
      <td align="right"><a class="normal-day-style" onmousemove="window.status=" "" onmouseout="window.status=" "" title="" href="javascript:dateSelected=26;closeCalendar();doOtherFunctions();"> 26 </a></td>
      <td align="right"><a class="normal-day-style" onmousemove="window.status=" "" onmouseout="window.status=" "" title="" href="javascript:dateSelected=27;closeCalendar();doOtherFunctions();"> 27 </a></td>
      <td align="right"><p class="end-of-weekday-style"> 28 </p></td></tr>
    <tr>
      <td align="right"><p class="end-of-weekday-style"> 29 </p></td>
      <td align="right"><a class="normal-day-style" onmousemove="window.status=" "" onmouseout="window.status=" "" title="" href="javascript:dateSelected=30;closeCalendar();doOtherFunctions();"> 30 </a></td>
      <td align="right"><a class="normal-day-style" onmousemove="window.status=" "" onmouseout="window.status=" "" title="" href="javascript:dateSelected=31;closeCalendar();doOtherFunctions();"> 31 </a></td></tr>
  </tbody>
</table>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寄居人 2025-01-03 01:21:04

试试这个(示例假设您正在寻找 div 元素)

browser.divs(:how => 'what').last

大多数元素的复数将返回匹配元素的集合(例如将 ses 添加到元素/标签名称div -> divs,address -> 地址)而 Ruby,它是一种很棒的语言,可以通过 .last 方法快速获取集合中的最后一个元素。

Try this (example assumes you are looking for div elements)

browser.divs(:how => 'what').last

The plural for most elements will return a collection of matching elements (e.g. add s or es to the element/tag name as would be appropriate. div -> divs, address -> addresses) And Ruby, great language that it is has a fast way to get the last element in collection via the .last method.

木落 2025-01-03 01:21:04

tmp 帖子。

有问题的 HTML 是一个弹出日历:

<table border="0" class="body-style">
  <tbody>
    <tr>
        <td width="27" align="right"><b>Sun</b>
        </td>
        <td width="27" align="right"><b>Mon</b>
        </td>
        <td width="27" align="right"><b>Tue</b>
        </td>
        <td width="27" align="right"><b>Wed</b>
        </td>
        <td width="27" align="right"><b>Thu</b>
        </td>
        <td width="27" align="right"><b>Fri</b>
        </td>
        <td width="27" align="right"><b>Sat</b>
        </td>
    </tr>
    <tr>
        <td align="right">
            <p class="end-of-weekday-style"> 1 </p>
        </td>
        <td align="right">
            <p class="end-of-weekday-style"> 2 </p>
        </td>
        <td align="right">
            <p class="end-of-weekday-style"> 3 </p>
        </td>
        <td align="right">
            <p class="end-of-weekday-style"> 4 </p>
        </td>
        <td align="right">
            <p class="end-of-weekday-style"> 5 </p>
        </td>
        <td align="right">
            <p class="end-of-weekday-style"> 6 </p>
        </td>
        <td align="right">
            <p class="end-of-weekday-style"> 7 </p>
        </td>
    </tr>
    <tr>
        <td align="right">
            <p class="end-of-weekday-style"> 8 </p>
        </td>
        <td align="right">
            <p class="end-of-weekday-style"> 9 </p>
        </td>
        <td align="right">
            <p class="end-of-weekday-style"> 10 </p>
        </td>
        <td align="right">
            <p class="end-of-weekday-style"> 11 </p>
        </td>
        <td align="right">
            <p class="end-of-weekday-style"> 12 </p>
        </td>
        <td align="right"><a class="current-day-style selected-day-style" onmousemove="window.status=" "" onmouseout="window.status=" "" title="" href="javascript:dateSelected=13;closeCalendar();doOtherFunctions();"> 13 </a>
        </td>
        <td align="right">
            <p class="end-of-weekday-style"> 14 </p>
        </td>
    </tr>
    <tr>
        <td align="right">
            <p class="end-of-weekday-style"> 15 </p>
        </td>
        <td align="right"><a class="normal-day-style" onmousemove="window.status=" "" onmouseout="window.status=" "" title="" href="javascript:dateSelected=16;closeCalendar();doOtherFunctions();"> 16 </a>
        </td>
        <td align="right"><a class="normal-day-style" onmousemove="window.status=" "" onmouseout="window.status=" "" title="" href="javascript:dateSelected=17;closeCalendar();doOtherFunctions();"> 17 </a>
        </td>
        <td align="right"><a class="normal-day-style" onmousemove="window.status=" "" onmouseout="window.status=" "" title="" href="javascript:dateSelected=18;closeCalendar();doOtherFunctions();"> 18 </a>
        </td>
        <td align="right"><a class="normal-day-style" onmousemove="window.status=" "" onmouseout="window.status=" "" title="" href="javascript:dateSelected=19;closeCalendar();doOtherFunctions();"> 19 </a>
        </td>
        <td align="right"><a class="normal-day-style" onmousemove="window.status=" "" onmouseout="window.status=" "" title="" href="javascript:dateSelected=20;closeCalendar();doOtherFunctions();"> 20 </a>
        </td>
        <td align="right">
            <p class="end-of-weekday-style"> 21 </p>
        </td>
    </tr>
    <tr>
        <td align="right">
            <p class="end-of-weekday-style"> 22 </p>
        </td>
        <td align="right"><a class="normal-day-style" onmousemove="window.status=" "" onmouseout="window.status=" "" title="" href="javascript:dateSelected=23;closeCalendar();doOtherFunctions();"> 23 </a>
        </td>
        <td align="right"><a class="normal-day-style" onmousemove="window.status=" "" onmouseout="window.status=" "" title="" href="javascript:dateSelected=24;closeCalendar();doOtherFunctions();"> 24 </a>
        </td>
        <td align="right"><a class="normal-day-style" onmousemove="window.status=" "" onmouseout="window.status=" "" title="" href="javascript:dateSelected=25;closeCalendar();doOtherFunctions();"> 25 </a>
        </td>
        <td align="right"><a class="normal-day-style" onmousemove="window.status=" "" onmouseout="window.status=" "" title="" href="javascript:dateSelected=26;closeCalendar();doOtherFunctions();"> 26 </a>
        </td>
        <td align="right"><a class="normal-day-style" onmousemove="window.status=" "" onmouseout="window.status=" "" title="" href="javascript:dateSelected=27;closeCalendar();doOtherFunctions();"> 27 </a>
        </td>
        <td align="right">
            <p class="end-of-weekday-style"> 28 </p>
        </td>
    </tr>
    <tr>
        <td align="right">
            <p class="end-of-weekday-style"> 29 </p>
        </td>
        <td align="right"><a class="normal-day-style" onmousemove="window.status=" "" onmouseout="window.status=" "" title="" href="javascript:dateSelected=30;closeCalendar();doOtherFunctions();"> 30 </a>
        </td>
        <td align="right"><a class="normal-day-style" onmousemove="window.status=" "" onmouseout="window.status=" "" title="" href="javascript:dateSelected=31;closeCalendar();doOtherFunctions();"> 31 </a>
        </td>
    </tr>
</tbody>

tmp post.

The HTML in question is a popup calendar:

<table border="0" class="body-style">
  <tbody>
    <tr>
        <td width="27" align="right"><b>Sun</b>
        </td>
        <td width="27" align="right"><b>Mon</b>
        </td>
        <td width="27" align="right"><b>Tue</b>
        </td>
        <td width="27" align="right"><b>Wed</b>
        </td>
        <td width="27" align="right"><b>Thu</b>
        </td>
        <td width="27" align="right"><b>Fri</b>
        </td>
        <td width="27" align="right"><b>Sat</b>
        </td>
    </tr>
    <tr>
        <td align="right">
            <p class="end-of-weekday-style"> 1 </p>
        </td>
        <td align="right">
            <p class="end-of-weekday-style"> 2 </p>
        </td>
        <td align="right">
            <p class="end-of-weekday-style"> 3 </p>
        </td>
        <td align="right">
            <p class="end-of-weekday-style"> 4 </p>
        </td>
        <td align="right">
            <p class="end-of-weekday-style"> 5 </p>
        </td>
        <td align="right">
            <p class="end-of-weekday-style"> 6 </p>
        </td>
        <td align="right">
            <p class="end-of-weekday-style"> 7 </p>
        </td>
    </tr>
    <tr>
        <td align="right">
            <p class="end-of-weekday-style"> 8 </p>
        </td>
        <td align="right">
            <p class="end-of-weekday-style"> 9 </p>
        </td>
        <td align="right">
            <p class="end-of-weekday-style"> 10 </p>
        </td>
        <td align="right">
            <p class="end-of-weekday-style"> 11 </p>
        </td>
        <td align="right">
            <p class="end-of-weekday-style"> 12 </p>
        </td>
        <td align="right"><a class="current-day-style selected-day-style" onmousemove="window.status=" "" onmouseout="window.status=" "" title="" href="javascript:dateSelected=13;closeCalendar();doOtherFunctions();"> 13 </a>
        </td>
        <td align="right">
            <p class="end-of-weekday-style"> 14 </p>
        </td>
    </tr>
    <tr>
        <td align="right">
            <p class="end-of-weekday-style"> 15 </p>
        </td>
        <td align="right"><a class="normal-day-style" onmousemove="window.status=" "" onmouseout="window.status=" "" title="" href="javascript:dateSelected=16;closeCalendar();doOtherFunctions();"> 16 </a>
        </td>
        <td align="right"><a class="normal-day-style" onmousemove="window.status=" "" onmouseout="window.status=" "" title="" href="javascript:dateSelected=17;closeCalendar();doOtherFunctions();"> 17 </a>
        </td>
        <td align="right"><a class="normal-day-style" onmousemove="window.status=" "" onmouseout="window.status=" "" title="" href="javascript:dateSelected=18;closeCalendar();doOtherFunctions();"> 18 </a>
        </td>
        <td align="right"><a class="normal-day-style" onmousemove="window.status=" "" onmouseout="window.status=" "" title="" href="javascript:dateSelected=19;closeCalendar();doOtherFunctions();"> 19 </a>
        </td>
        <td align="right"><a class="normal-day-style" onmousemove="window.status=" "" onmouseout="window.status=" "" title="" href="javascript:dateSelected=20;closeCalendar();doOtherFunctions();"> 20 </a>
        </td>
        <td align="right">
            <p class="end-of-weekday-style"> 21 </p>
        </td>
    </tr>
    <tr>
        <td align="right">
            <p class="end-of-weekday-style"> 22 </p>
        </td>
        <td align="right"><a class="normal-day-style" onmousemove="window.status=" "" onmouseout="window.status=" "" title="" href="javascript:dateSelected=23;closeCalendar();doOtherFunctions();"> 23 </a>
        </td>
        <td align="right"><a class="normal-day-style" onmousemove="window.status=" "" onmouseout="window.status=" "" title="" href="javascript:dateSelected=24;closeCalendar();doOtherFunctions();"> 24 </a>
        </td>
        <td align="right"><a class="normal-day-style" onmousemove="window.status=" "" onmouseout="window.status=" "" title="" href="javascript:dateSelected=25;closeCalendar();doOtherFunctions();"> 25 </a>
        </td>
        <td align="right"><a class="normal-day-style" onmousemove="window.status=" "" onmouseout="window.status=" "" title="" href="javascript:dateSelected=26;closeCalendar();doOtherFunctions();"> 26 </a>
        </td>
        <td align="right"><a class="normal-day-style" onmousemove="window.status=" "" onmouseout="window.status=" "" title="" href="javascript:dateSelected=27;closeCalendar();doOtherFunctions();"> 27 </a>
        </td>
        <td align="right">
            <p class="end-of-weekday-style"> 28 </p>
        </td>
    </tr>
    <tr>
        <td align="right">
            <p class="end-of-weekday-style"> 29 </p>
        </td>
        <td align="right"><a class="normal-day-style" onmousemove="window.status=" "" onmouseout="window.status=" "" title="" href="javascript:dateSelected=30;closeCalendar();doOtherFunctions();"> 30 </a>
        </td>
        <td align="right"><a class="normal-day-style" onmousemove="window.status=" "" onmouseout="window.status=" "" title="" href="javascript:dateSelected=31;closeCalendar();doOtherFunctions();"> 31 </a>
        </td>
    </tr>
</tbody>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文