java selenium使用xpath浏览网页

时间:2014-10-22 18:15:37

标签: java html selenium xpath

我正在尝试使用selenium进行登录,导航,填写表单和下载文件。 当我尝试导航时,我需要单击一个链接,听起来非常基本。 由于它没有id或名称,我使用FirePath获取xPath(.//*[@id='id25']/li[4]/a),然后我执行了以下操作:

driver.findElement(By.xpath(".//*[@id='id25']/li[4]/a"));

Selenium让我无法找到元素 我在这里和那里做了一些阅读并尝试了一些不同的东西:

driver.findElement(By.linkText("Network Support")).click();

driver.findElement(By.xpath("//a[@title='Network Support']")).click();

driver.findElement(By.cssSelector(".LI_Primary"));

driver.findElement(By.xpath("[@id='id25']/li[4]/a"));

driver.findElement(By.xpath("/html/body/form/span/div[1]/div/div[1]/div/ul/li[4]/a")).click();

这些都不起作用,selenium总是返回“无法找到元素:”

html:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>

    <head id="Head1"></head>
    <frameset id="EbpFrame" frameborder="0" marginwidth="0" marginheight="0" rows="111px,*">
        <frame style="border:0px !important;" noresize="noresize" frameborder="0" framespacing="0" scrolling="no" marginheight="0" marginwidth="0" name="upper" src="/general/UpperNavigationRibbon.aspx?header=noname">
            #document
                <!DOCTYPE html>
                <!--

                [if lte IE 7 ]>    <html class="ie7"> <![endif]

                -->
                <!--

                [if IE 8 ]>    <html class="ie8"> <![endif]

                -->
                <!--

                [if IE 9 ]>    <html class="ie9"> <![endif]

                -->
                <!--

                [if (gt IE 9)|!(IE)]><!

                -->
                <html>
                    <!--

                    <![endif]

                    -->
                    <head id="Head1"></head>
                    <body class="header" onload="SetFrameHeight()">
                        <form id="formm" action="UpperNavigationRibbon.aspx?header=noname" method="post">
                            <div class="aspNetHidden"></div>
                            <div class="aspNetHidden"></div>
                            <span id="spanContent">
                                <div id="page_container" class="header">
                                    <ul id="global_nav"></ul>
                                    <div id="primary_links">
                                        <a class="eLogo" title="Home" href="javascript:menu_Controll('/Portal/Home.aspx')"></a>
                                        <div style="width:810px">
                                            <div id="primary_nav">
                                                <ul id="id25" class="clearfix">
                                                    <li></li>
                                                    <li></li>
                                                    <li></li>
                                                    <li>
                                                        <a class="LI_Primary" title="Network Support" href="javascript:menu_Controll('/ContactAndhelp/ContactsAdmin.aspx?CategoryID=10')"></a>
                                                    </li>
                                                    <li></li>
                                                </ul>
                                            </div>
                                        </div>
                                        <div class="clientLogo"></div>
                                    </div>
                                </div>
                                <div class="bottomgrad"></div>
                            </span>
                            <input id="hdnSerVar" type="hidden" value="fjunior@timbrasil.com.br" name="hdnSerVar"></input>
                        </form>
                        <script type="text/javascript"></script>
                    </body>
                </html>
        </frame>
        <frame style="border:0px !important;" onload="AppBodyTrackHistory()" noresize="noresize" frameborder="0" framespacing="0" scrolling="auto" marginheight="0" marginwidth="0" name="portalmain" src="/Portal/Home.aspx"></frame>
    </frameset>

</html>

2 个答案:

答案 0 :(得分:1)

问题出在框架内,如下所示:How to select a frame using selenium?

driver.switchTo().defaultContent();
driver.switchTo().frame(driver.findElement(By.name("upper")));
driver.findElement(By.xpath(".//*[@id='id25']/li[4]/a")).click();

使用上述选项将使selenium选择所需的帧,然后单击所需的链接。

答案 1 :(得分:0)

完成你的html(它没有很好地形成)后,xpath正常工作并找到了你想要的元素。 使用http://www.xpathtester.com/xpath

进行测试

修改

这是一个有效的HTML。不知道这是不是你的页面..

<document>
<html>
<!--<![endif]-->
<head id="Head1"/>
<body class="header" onload="SetFrameHeight()">
<form id="formm" action="UpperNavigationRibbon.aspx?header=noname" method="post">
<div class="aspNetHidden"/>
<div class="aspNetHidden"/>
<span id="spanContent">
<div id="page_container" class="header">
<ul id="global_nav">
<div id="primary_links">
<a class="eLogo" title="Home" href="javascript:menu_Controll('/Portal/Home.aspx')"/>
<div style="width:810px">
<div id="primary_nav">
<ul id="id25" class="clearfix">
<li/>
<li/>
<li/>
<li>
<a class="LI_Primary" title="Network Support" href="javascript:menu_Controll('/ContactAndhelp/ContactsAdmin.aspx?CategoryID=10')">Network Support</a>
</li>
<li/>
</ul>
</div>
</div>
</div>
</ul>
</div>
</span>
</form>
</body>
</html>
</document>