selenium没有使用xpath找到正确的链接

时间:2012-05-02 08:33:57

标签: selenium xpath

我正在尝试删除先前测试(删除链接)生成的点击链接,但我对xpath有很大困难

这是我收到的错误

Invalid response while accessing the Selenium Server at 'http://localhost:4444/s
elenium-server/driver/': ERROR: Element xpath=(//td[normalize-space() ='Test tit
le 2']/following-sibling::td[3]/descendant::a[.='delete']) not found

这是我的测试

public function testdeleteStationTestCase()
    {
        $this->open("/upd8r_new/");
        $this->click("css=a > img");
        $this->waitForPageToLoad("30000");
        $this->type("name=username", "glass2");
        $this->type("name=password", "glass2");
        $this->click("name=submit");
        sleep(1);
        $this->click("xpath=(//td[normalize-space() ='Test title 2']/following-sibling::td[3]/a[.='delete'])");
        //$this->click("xpath=(//td[normalize-space() ='Test title 2']/following-sibling::td[3]/descendant::a[.='delete'][1])");

        sleep(1);


        $this->assertTrue($this->isTextPresent("Station deleted!"),"Station Deleted");
    }

你可以看到我注释掉的线路我一直在尝试各种组合来让测试运行 -

这是我正在测试的html页面 - 我添加了一行文字,我试图点击

<body>
<!-- social media header -->
<div class="main">
    <div class="header">
        Central Control Panel

    </div>

    <div class="logo">
                    <img src="http://localhost/upd8r_new/imgs/loyalty_connect2.png"/>
            </div>

    <div style="clear:both"></div>



    <ul class="Menu" id="maintab">
        <li class="selected" rel="home"><a href="http://localhost/upd8r_new/socialmedia">HOME</a>       </li>

        <li  rel="retail">
            <a href="http://localhost/upd8r_new/social_retail">RETAIL</a>       </li>




        <li   rel="stats"> <a href="http://localhost/upd8r_new/socialmedia/stats">STATS</a></li>
        <li 






            rel="social_media_settings"> <a href="http://localhost/upd8r_new/socialmedia/settings">SETTINGS</a></li>
        <li rel="logout"><a href="http://localhost/upd8r_new/login/logout">LOGOUT</a> </li>
    </ul>   


    <div style="clear:both"></div>
    <div style="text-align:left;width:980px;height:40px;background-color:#0E477A;margin:auto;padding-top:0px;">

        <div style="width:700px;text-align:left;padding-left:10px;float:left">

            <div id="logout" class="submenustyle">

            </div>
                        <div id="retail" class="submenustyle">
                <ul class="sub_menu">
                    <li><a href="http://localhost/upd8r_new/social_retail">USERS</a></li>
                    <li><a href="http://localhost/upd8r_new/social_retail/view_competitions">COMPETITIONS</a></li>

                </ul>               
            </div>
                        <div id="home" class="submenustyle">
                <ul class="sub_menu">
                    <li><a href="http://localhost/upd8r_new/socialmedia/add">ADD LIKESTATION</a></li>
                    <li><a href="http://localhost/upd8r_new/socialmedia/add_picstation">ADD PICSTATION</a></li>
                    <li><a href="http://localhost/upd8r_new/socialmedia/add_vidstation">ADD VIDSTATION</a></li>
                </ul>

            </div>
            <div id="socialmedia_users" class="submenustyle">

            </div>
            <div id="stats" class="submenustyle">
                <ul class="sub_menu">
                    <li><a href="http://localhost/upd8r_new/socialmedia/stats">STATS HOME</a></li>
                    <li><a href="http://localhost/upd8r_new/socialmedia/stats/facebook/stats">FACEBOOK STATS</a></li>


                </ul>
            </div>


            <div id="social_media_settings" class="submenustyle">
                <ul class="sub_menu">
                    <li><a href="http://localhost/upd8r_new/socialmedia/settings">SETTINGS HOME</a></li>
                    <li><a href="http://localhost/upd8r_new/socialmedia/registration_settings">NETWORKS TO USE</a></li>

                    <li><a href="http://localhost/upd8r_new/socialmedia/pre_settings">PRE REGISTRATION</a></li>
                                            <li>
                        <a href="http://localhost/upd8r_new/socialmedia/app_settings">APP SETTINGS</a>                  
                        </li>
                                    </ul>
            </div>


        </div>
        <div style="float:right;font-weight:bold;color:#fff;margin-top:12px;margin-right: 20px;">
                        Logged In As: glass2        </div>

    </div>
    <div style="clear:both"></div>


    <div style="padding-top:30px;padding-left:10px">Station deleted! </div>
    <div style="padding-left:10px">



                        <table width="700px" align="left" style="text-align:left;">
            <tr>
                <td style="font-weight:bold">Pre-Registration</td>
                <td><a href="http://localhost/upd8r_new/retail/register/80">http://localhost/upd8r_new/retail/register/80</a></td>
            </tr>

        </table>
                <div style="clear:both"></div>

        <table width="800px" align="left" style="text-align:left;">
            <tr style="font-weight:bold;background-color:#f1f1f1">
                <td colspan="6" STYLE="text-align:center;font-weight:bold">LIKESTATIONS</td>
            </tr>
            <tr style="font-weight:bold;background-color:#f1f1f1">
                <td width="10%">STATION ID</td>
                <td width="32%">STATION NAME</td>
                <td width="25%">LINK TITLE</td>
                <td width="10%">STATION STATS</td>
                <td width="15%">LAST POST</td>
                <td width="8%"></td>

            </tr>
                            <tr>
                    <td><a href="http://localhost/upd8r_new/socialmedia/edit/gla20311344">gla20311344</a> </td>
                    <td><a href="http://localhost/upd8r_new/socialmedia/edit/gla20311344">http://localhost/upd8r_new/socialmedia/edit/gla20311344</a> </td>
                    <td><a href="http://localhost/upd8r_new/socialmedia/edit/gla20311344">Webcast</a> </td>
                    <td><a href="http://localhost/upd8r_new/stats/home/station_stats/gla20311344">STATS</a> </td>
                    <td>0</td>
                    <td><a href="http://localhost/upd8r_new/socialmedia/delete/gla20311344">delete</a></td>
                </tr>
                            <tr>
                    <td><a href="http://localhost/upd8r_new/socialmedia/edit/gla2084629867">gla2084629867</a> </td>
                    <td><a href="http://localhost/upd8r_new/socialmedia/edit/gla2084629867">http://localhost/upd8r_new/socialmedia/edit/gla2084629867</a> </td>
                    <td><a href="http://localhost/upd8r_new/socialmedia/edit/gla2084629867">tes this link title3</a> </td>
                    <td><a href="http://localhost/upd8r_new/stats/home/station_stats/gla2084629867">STATS</a> </td>
                    <td>4 weeks ago</td>
                    <td><a href="http://localhost/upd8r_new/socialmedia/delete/gla2084629867">delete</a></td>
                </tr>
                    </table>

        <div style="clear:both"></div>

        <table width="620px" align="left" style="text-align:left;">
            <tr style="font-weight:bold;background-color:#f1f1f1">
                <td colspan="5" STYLE="text-align:center;font-weight:bold">FACEBOOK PIC STATIONS</td>
            </tr>
            <tr style="font-weight:bold;background-color:#f1f1f1">
                <td width="15%">STATION ID</td>
                <td width="50%">STATION TITLE</td>
                <td width="10%">STATION STATS</td>
                <td width="15%">LAST POST</td>
                <td width="10%"></td>

            </tr>
                            <tr>
                    <td><a href="http://localhost/upd8r_new/socialmedia/edit_picstation/gla1079978359">gla1079978359</a> </td>
                    <td><a href="http://localhost/upd8r_new/socialmedia/edit_picstation/gla1079978359">Test title 2</a> </td>
                    <td><a href="http://localhost/upd8r_new/stats/home/station_stats/gla1079978359">STATS</a> </td>
                    <td>0</td>
                    <td>**link I'm trying to click**<a href="http://localhost/upd8r_new/socialmedia/delete/gla1079978359">delete</a></td>
                </tr>
                            <tr>
                    <td><a href="http://localhost/upd8r_new/socialmedia/edit_picstation/gla1229158969">gla1229158969</a> </td>
                    <td><a href="http://localhost/upd8r_new/socialmedia/edit_picstation/gla1229158969">Test title 2</a> </td>
                    <td><a href="http://localhost/upd8r_new/stats/home/station_stats/gla1229158969">STATS</a> </td>
                    <td>0</td>
                    <td><a href="http://localhost/upd8r_new/socialmedia/delete/gla1229158969">delete</a></td>
                </tr>
                            <tr>
                    <td><a href="http://localhost/upd8r_new/socialmedia/edit_picstation/gla1256364596">gla1256364596</a> </td>
                    <td><a href="http://localhost/upd8r_new/socialmedia/edit_picstation/gla1256364596">Test title 2</a> </td>
                    <td><a href="http://localhost/upd8r_new/stats/home/station_stats/gla1256364596">STATS</a> </td>
                    <td>0</td>
                    <td><a href="http://localhost/upd8r_new/socialmedia/delete/gla1256364596">delete</a></td>
                </tr>
                            <tr>
                    <td><a href="http://localhost/upd8r_new/socialmedia/edit_picstation/gla1529784490">gla1529784490</a> </td>
                    <td><a href="http://localhost/upd8r_new/socialmedia/edit_picstation/gla1529784490">Test title 2</a> </td>
                    <td><a href="http://localhost/upd8r_new/stats/home/station_stats/gla1529784490">STATS</a> </td>
                    <td>0</td>
                    <td><a href="http://localhost/upd8r_new/socialmedia/delete/gla1529784490">delete</a></td>
                </tr>
                            <tr>
                    <td><a href="http://localhost/upd8r_new/socialmedia/edit_picstation/gla159062284">gla159062284</a> </td>
                    <td><a href="http://localhost/upd8r_new/socialmedia/edit_picstation/gla159062284">Test title 2</a> </td>
                    <td><a href="http://localhost/upd8r_new/stats/home/station_stats/gla159062284">STATS</a> </td>
                    <td>0</td>
                    <td><a href="http://localhost/upd8r_new/socialmedia/delete/gla159062284">delete</a></td>
                </tr>
                            <tr>
                    <td><a href="http://localhost/upd8r_new/socialmedia/edit_picstation/gla175038477">gla175038477</a> </td>
                    <td><a href="http://localhost/upd8r_new/socialmedia/edit_picstation/gla175038477">Test title 2</a> </td>
                    <td><a href="http://localhost/upd8r_new/stats/home/station_stats/gla175038477">STATS</a> </td>
                    <td>0</td>
                    <td><a href="http://localhost/upd8r_new/socialmedia/delete/gla175038477">delete</a></td>
                </tr>
                            <tr>
                    <td><a href="http://localhost/upd8r_new/socialmedia/edit_picstation/gla1777127061">gla1777127061</a> </td>
                    <td><a href="http://localhost/upd8r_new/socialmedia/edit_picstation/gla1777127061">Test title 2</a> </td>
                    <td><a href="http://localhost/upd8r_new/stats/home/station_stats/gla1777127061">STATS</a> </td>
                    <td>0</td>
                    <td><a href="http://localhost/upd8r_new/socialmedia/delete/gla1777127061">delete</a></td>
                </tr>
                            <tr>
                    <td><a href="http://localhost/upd8r_new/socialmedia/edit_picstation/gla180142977">gla180142977</a> </td>
                    <td><a href="http://localhost/upd8r_new/socialmedia/edit_picstation/gla180142977">Test title 2</a> </td>
                    <td><a href="http://localhost/upd8r_new/stats/home/station_stats/gla180142977">STATS</a> </td>
                    <td>0</td>
                    <td><a href="http://localhost/upd8r_new/socialmedia/delete/gla180142977">delete</a></td>
                </tr>
                            <tr>
                    <td><a href="http://localhost/upd8r_new/socialmedia/edit_picstation/gla1950664292">gla1950664292</a> </td>
                    <td><a href="http://localhost/upd8r_new/socialmedia/edit_picstation/gla1950664292">Test title 2</a> </td>
                    <td><a href="http://localhost/upd8r_new/stats/home/station_stats/gla1950664292">STATS</a> </td>
                    <td>0</td>
                    <td><a href="http://localhost/upd8r_new/socialmedia/delete/gla1950664292">delete</a></td>
                </tr>
                            <tr>
                    <td><a href="http://localhost/upd8r_new/socialmedia/edit_picstation/gla1971709701">gla1971709701</a> </td>
                    <td><a href="http://localhost/upd8r_new/socialmedia/edit_picstation/gla1971709701">Test title 2</a> </td>
                    <td><a href="http://localhost/upd8r_new/stats/home/station_stats/gla1971709701">STATS</a> </td>
                    <td>0</td>
                    <td><a href="http://localhost/upd8r_new/socialmedia/delete/gla1971709701">delete</a></td>
                </tr>
                            <tr>
                    <td><a href="http://localhost/upd8r_new/socialmedia/edit_picstation/gla2028774169">gla2028774169</a> </td>
                    <td><a href="http://localhost/upd8r_new/socialmedia/edit_picstation/gla2028774169">Test title 2</a> </td>
                    <td><a href="http://localhost/upd8r_new/stats/home/station_stats/gla2028774169">STATS</a> </td>
                    <td>0</td>
                    <td><a href="http://localhost/upd8r_new/socialmedia/delete/gla2028774169">delete</a></td>
                </tr>
                            <tr>
                    <td><a href="http://localhost/upd8r_new/socialmedia/edit_picstation/gla2035285297">gla2035285297</a> </td>
                    <td><a href="http://localhost/upd8r_new/socialmedia/edit_picstation/gla2035285297">Test title 2</a> </td>
                    <td><a href="http://localhost/upd8r_new/stats/home/station_stats/gla2035285297">STATS</a> </td>
                    <td>0</td>
                    <td><a href="http://localhost/upd8r_new/socialmedia/delete/gla2035285297">delete</a></td>
                </tr>
                            <tr>
                    <td><a href="http://localhost/upd8r_new/socialmedia/edit_picstation/gla2072665757">gla2072665757</a> </td>
                    <td><a href="http://localhost/upd8r_new/socialmedia/edit_picstation/gla2072665757">Test title 2</a> </td>
                    <td><a href="http://localhost/upd8r_new/stats/home/station_stats/gla2072665757">STATS</a> </td>
                    <td>0</td>
                    <td><a href="http://localhost/upd8r_new/socialmedia/delete/gla2072665757">delete</a></td>
                </tr>
                            <tr>
                    <td><a href="http://localhost/upd8r_new/socialmedia/edit_picstation/gla2115042517">gla2115042517</a> </td>
                    <td><a href="http://localhost/upd8r_new/socialmedia/edit_picstation/gla2115042517">Test title 2</a> </td>
                    <td><a href="http://localhost/upd8r_new/stats/home/station_stats/gla2115042517">STATS</a> </td>
                    <td>0</td>
                    <td><a href="http://localhost/upd8r_new/socialmedia/delete/gla2115042517">delete</a></td>
                </tr>
                            <tr>
                    <td><a href="http://localhost/upd8r_new/socialmedia/edit_picstation/gla2141659529">gla2141659529</a> </td>
                    <td><a href="http://localhost/upd8r_new/socialmedia/edit_picstation/gla2141659529">Test title 2</a> </td>
                    <td><a href="http://localhost/upd8r_new/stats/home/station_stats/gla2141659529">STATS</a> </td>
                    <td>0</td>
                    <td><a href="http://localhost/upd8r_new/socialmedia/delete/gla2141659529">delete</a></td>
                </tr>
                            <tr>
                    <td><a href="http://localhost/upd8r_new/socialmedia/edit_picstation/gla4705592">gla4705592</a> </td>
                    <td><a href="http://localhost/upd8r_new/socialmedia/edit_picstation/gla4705592">Test title 2</a> </td>
                    <td><a href="http://localhost/upd8r_new/stats/home/station_stats/gla4705592">STATS</a> </td>
                    <td>0</td>
                    <td><a href="http://localhost/upd8r_new/socialmedia/delete/gla4705592">delete</a></td>
                </tr>
                            <tr>
                    <td><a href="http://localhost/upd8r_new/socialmedia/edit_picstation/gla565900575">gla565900575</a> </td>
                    <td><a href="http://localhost/upd8r_new/socialmedia/edit_picstation/gla565900575">Test title 2</a> </td>
                    <td><a href="http://localhost/upd8r_new/stats/home/station_stats/gla565900575">STATS</a> </td>
                    <td>0</td>
                    <td><a href="http://localhost/upd8r_new/socialmedia/delete/gla565900575">delete</a></td>
                </tr>
                            <tr>
                    <td><a href="http://localhost/upd8r_new/socialmedia/edit_picstation/gla608150382">gla608150382</a> </td>
                    <td><a href="http://localhost/upd8r_new/socialmedia/edit_picstation/gla608150382">Test title 2</a> </td>
                    <td><a href="http://localhost/upd8r_new/stats/home/station_stats/gla608150382">STATS</a> </td>
                    <td>0</td>
                    <td><a href="http://localhost/upd8r_new/socialmedia/delete/gla608150382">delete</a></td>
                </tr>
                            <tr>
                    <td><a href="http://localhost/upd8r_new/socialmedia/edit_picstation/gla979271929">gla979271929</a> </td>
                    <td><a href="http://localhost/upd8r_new/socialmedia/edit_picstation/gla979271929">Test title 2</a> </td>
                    <td><a href="http://localhost/upd8r_new/stats/home/station_stats/gla979271929">STATS</a> </td>
                    <td>0</td>
                    <td><a href="http://localhost/upd8r_new/socialmedia/delete/gla979271929">delete</a></td>
                </tr>
                    </table>

        <div style="clear:both"></div>

        <table width="520px" align="left" style="text-align:left;">
            <tr style="font-weight:bold;background-color:#f1f1f1">
                <td colspan="4" STYLE="text-align:center;font-weight:bold">FACEBOOK VIDEO STATIONS</td>
            </tr>
            <tr style="font-weight:bold;background-color:#f1f1f1">
                <td width="20%">STATION ID</td>
                <td width="60%">STATION TITLE</td>
                <td width="60%">STATION STATS</td>
                <td width="20%"></td>
            </tr>
                    </table>


        <div style="clear:both"></div>


</div>
</div>
<div class="footer">
    <div style="float:left;padding-top:20px;padding-left:20px;font-size:10px;font-family:arial"></div>
    <div style="padding-top:20px;float:right;padding-right:30px;"><img src="http://localhost/upd8r_new/imgs/excelerated.jpg"/></div>
</div>
</body>
</html>

2 个答案:

答案 0 :(得分:1)

您的问题出在sleep(1); 在这个地方而不是sleep(1);你应该使用它:

$this->waitForCondition("selenium.isElementPresent(\'css=table:contains(\'FACEBOOK PIC STATIONS\') a:contains(\'delete\')\')", "20000");

顺便说一句,当您使用selenium RC时,您可以使用此css访问您的删除链接(使用webdriver它将无法正常工作,因为:contains不是css规范的一部分):

$this->click("css=table:contains('FACEBOOK PIC STATIONS') a:contains('delete')");

同时使用适当的sleep(1);命令

替换所有waitForCondition

答案 1 :(得分:0)

在Firefox中安装“XPath Checker 4.4”附加组件。因此,对于该对象,您将使用此附加组件获得正确的xpath ..

所以,

单击该对象的xpath = xpath