【发布时间】:2017-05-03 01:46:07
【问题描述】:
在使用 Selenium 编写自动化脚本时,我尝试使用 css 选择器定位所需的 web 元素,并使用 firebug 获取该元素的唯一 css 路径。然而,我很惊讶,因为有时萤火虫会给我一个不正确的 css 路径,尽管它确实适用于 selenium。
<table width="100%" cellspacing="1" cellpadding="4" border="0" class="forumline">
<tbody><tr>
<th nowrap="nowrap" height="25" align="center" colspan="2" class="thcornerl"> Topic </th>
<th width="50" nowrap="nowrap" align="center" class="thtop"> Answers </th>
<th width="100" nowrap="nowrap" align="center" class="thtop"> Author </th>
<th width="50" nowrap="nowrap" align="center" class="thtop"> Views </th>
<th nowrap="nowrap" align="center" class="thcornerr"> Last message </th>
</tr>
<!-- TOPICS LISTING -->
<tr class="bg_small_yellow">
<td width="20" valign="middle" align="center" class="row1Announce"> <img alt="" src="/jforum/images/transp.gif" class="icon_folder_announce">
<span class="topictitle">
<a href="/jforum/posts/list/2.page">
Support JForum - Please read
</a>
</span>
</td>
<td valign="middle" align="center" class="row2Announce"><span class="postdetails">0</span></td>
<td valign="middle" align="center" class="row3Announce">
<span class="name"><a href="/jforum/user/profile/2.page">Admin</a></span>
</td>
<td valign="middle" align="center" class="row2Announce"><span class="postdetails">4</span></td>
<td valign="middle" nowrap="nowrap" align="center" class="row3Announce">
<span class="postdetails">20/03/2016 13:58:24<br>
<a href="/jforum/user/profile/2.page">Admin</a>
<a href="/jforum/posts/list/2.page#2"><img border="0" alt="[Latest Reply]" src="/jforum/templates/default/images/icon_latest_reply.gif"></a></span>
</td>
</tr>
<tr class="bg_small_yellow">
<td width="20" valign="middle" align="center" class="row1"> <img alt="" src="/jforum/images/transp.gif" class="icon_folder">
<span class="topictitle">
<a href="/jforum/posts/list/3.page">
[Poll]
Which one is your favorite?
</a>
</span>
</td>
<td valign="middle" align="center" class="row2"><span class="postdetails">0</span></td>
<td valign="middle" align="center" class="row3">
<span class="name"><a href="/jforum/user/profile/3.page">yinzhenzhixin</a></span>
</td>
<td valign="middle" align="center" class="row2"><span class="postdetails">8</span></td>
<td valign="middle" nowrap="nowrap" align="center" class="row3">
<span class="postdetails">20/03/2016 14:34:25<br>
<a href="/jforum/user/profile/3.page">yinzhenzhixin</a>
<a href="/jforum/posts/list/3.page#4"><img border="0" alt="[Latest Reply]" src="/jforum/templates/default/images/icon_latest_reply.gif"></a></span>
</td>
</tr>
<tr class="bg_small_yellow">
<td width="20" valign="middle" align="center" class="row1"> <img alt="" src="/jforum/images/transp.gif" class="icon_folder">
<span class="topictitle">
<a href="/jforum/posts/list/1.page">
Welcome to JForum
</a>
</span>
</td>
<td valign="middle" align="center" class="row2"><span class="postdetails">1</span></td>
<td valign="middle" align="center" class="row3">
<span class="name"><a href="/jforum/user/profile/2.page">Admin</a></span>
</td>
<td valign="middle" align="center" class="row2"><span class="postdetails">6</span></td>
<td valign="middle" nowrap="nowrap" align="center" class="row3">
<span class="postdetails">20/03/2016 14:09:51<br>
<a href="/jforum/user/profile/3.page">yinzhenzhixin</a>
<a href="/jforum/posts/list/1.page#3"><img border="0" alt="[Latest Reply]" src="/jforum/templates/default/images/icon_latest_reply.gif"></a></span>
</td>
</tr>
<!-- END OF TOPICS LISTING -->
<tr align="center">
<td valign="middle" height="28" align="right" colspan="6" class="catbottom">
<table cellspacing="0" cellpadding="0" border="0">
<tbody><tr>
<td align="center"><span class="gensmall"> </span></td>
</tr>
</tbody></table>
</td>
</tr>
</tbody></table>
我需要第三个 tr 下的跨度,其类名为“bg_small_yellow”。但是,萤火虫给了我独特的 css 路径“tr.bg_small_yellow:nth-child(4) > td:nth-child(2) .topictitle > a:nth-child(1)”,你看只有 3总共 bg_small_yellow。第4个在哪里?它是如何工作的?
【问题讨论】:
-
欢迎来到 Stack Overflow!请阅读How to Ask。请提供您尝试过的代码和执行结果,包括任何错误消息等。同时提供指向页面和/或相关 HTML 的链接。
标签: html css selenium automated-tests