【问题标题】:How does the firebug identify the unique css path for selenium?firebug 如何识别 selenium 的唯一 css 路径?
【发布时间】: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">&nbsp;Topic&nbsp;</th>
                <th width="50" nowrap="nowrap" align="center" class="thtop">&nbsp;Answers&nbsp;</th>
                <th width="100" nowrap="nowrap" align="center" class="thtop">&nbsp;Author&nbsp;</th>
                <th width="50" nowrap="nowrap" align="center" class="thtop">&nbsp;Views&nbsp;</th>
                <th nowrap="nowrap" align="center" class="thcornerr">&nbsp;Last message&nbsp;</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">&nbsp;</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


【解决方案1】:

您可以使用唯一的 X 路径来访问 Web 元素。

Selenium Command

希望上述链接能帮助您解决问题!

【讨论】:

  • 谢谢。其实我想知道为什么 firedebug 给索引为 4 而不是 3,因为总共只有 3 个名为 bg_small_yellow 的 css 类。
猜你喜欢
  • 2011-09-09
  • 2013-06-05
  • 1970-01-01
  • 2017-04-12
  • 2020-08-16
  • 2018-11-14
  • 2020-08-05
  • 2014-09-11
  • 1970-01-01
相关资源
最近更新 更多