Watir-webdriver 如何访问最后一个匹配元素而不是第一个?
在动态网页上,我不知道预先匹配元素的数量,因此我不知道要使用哪个索引。
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">Â 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>
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这个(示例假设您正在寻找 div 元素)
大多数元素的复数将返回匹配元素的集合(例如将
s
或es
添加到元素/标签名称div -> divs,address -> 地址)而 Ruby,它是一种很棒的语言,可以通过.last
方法快速获取集合中的最后一个元素。Try this (example assumes you are looking for div elements)
The plural for most elements will return a collection of matching elements (e.g. add
s
ores
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.tmp 帖子。
有问题的 HTML 是一个弹出日历:
tmp post.
The HTML in question is a popup calendar: