java selenium 使用 xpath 浏览网页

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

最佳答案

问题出在框架中,如下所示: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选择你需要的框架,然后点击你想要的链接。

关于java selenium 使用 xpath 浏览网页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26514305/

相关文章:

java - Java applet 可以使用其他 Web 库吗

java - ActionListener 和 SystemUtil.invokeLater 的问题

html - 如何创建与父div 相同的子div 的边框高度?

python - 如何使用 Selenium、Python 从 Google 搜索中提取链接

Java Assert Double 是 NaN

java - 在 Java 中遍历 ArrayList 的 ArrayList

html - Angular 元素应该被视为 block 还是包装器

html - 如何设置由图像和文本组成的div内容的最大宽度

python - 单击()在 Selenium 中不起作用

python-3.x - selenium.common.exceptions.WebDriverException : Message: unknown error: unable to discover open pages using ChromeDriver through Selenium