简体   繁体   中英

java selenium navigate through a webpage using xpath

I'm trying to use selenium to make a login, navigate, fill a form and download a file. When i'm trying to navigate i need to click a link, sounds pretty basic. Since it has no id or name, i used FirePath to get the xPath( .//*[@id='id25']/li[4]/a ) then i did the following:

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

Selenium returns me Unable to locate element: I did some reading here and there and tried a few different things:

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();

None of those work, selenium always return "Unable to locate element:"

The 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>

The problem lies whitin the frames, as it can be seen here: 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();

Using the above will make selenium select the frame you need, and then click the link you want.

After completing your html (it wasn't well formed) the xpath worked properly and found the element you want. Tested using http://www.xpathtester.com/xpath

EDIT

This is a html that works. Don't know if it's exactly your page..

<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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM