用 selenium java 扩展框架内的菜单

Expand a menu inside frame with selenium java

我正在尝试扩展“应用程序”选项:

这是它的 html:

到目前为止我做了什么:

// Wait until presence of navigation frame
element = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("/html/frameset/frameset/frame[1]")));
// switch to navigation frame
driver.switchTo().frame(driver.findElement(By.name("navigation")));
// Wait until presence of Aplications option
element = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("/html/body/div/div/table/tbody/tr[2]/td/div[5]/a")));
// Click to expand Aplicactions options
element.click();

当它进入点击时,我可以看到它位于 Aplications 选项上方,因为它显示了工具提示/标题“Aplications”,就像我们将鼠标放在该选项上和该选项的 href 左下角 javascript:expandCollapse('2');

我知道代码是 运行 直到这里,因为另一段登录代码正在运行。

我不确定是否切换到框架,因为我查找了框架集,它显示我们可以直接切换到框架。

我是新手,如有打扰请见谅。如果缺少某些内容以查找错误,请告诉我编辑 post.

提前致谢。

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

    <link href="" rel="styleSheet" type="text/css">
    <title></title>
</head>
<frameset name="ISCmain" rows="35,*" frameborder="1" border="1" resize="yes">
    <frame title="Header mark message" src="" name="header"
        noresize="" scrolling="no" marginwidth="0" marginheight="0">
        <frameset cols="25%,*" resize="yes">
            <frame title="Navigation mark" src="" name="navigation"
                style="border-right: 1px solid #d7d7d7;" resize="yes" marginwidth="10" marginheight="10">
                <html>
                <head>
                </head>
                <body style="direction:ltr" marginwidth="10" marginheight="10" onload="initAll();" class="navtree"
                    leftmargin="0" topmargin="0">
                    <div style="direction:ltr">
                        <link href="" rel="styleSheet" type="text/css">
                        <script language="javascript">
                        </script>
                        <div class="navBody" style="background-color:#FFFFFF;">
                            <table height="26" width="100%" cellspacing="0" cellpadding="0"
                                style="background-color:#FFFFFF;">
                                <tbody>
                                    <tr height="26" style="border-left: 1px solid #3970B1;background-color:#FFFFFF">
                                        <td class="wpsGpFilter" valign="center" align="left" width="100%"
                                            style="background-image:url(/ibm/console/images/isclite/grpfilter_background.gif);background-repeat: repeat-x;">
                                            <select name="navFilterSelection" id="navFilterSelection" dir="ltr"
                                                onchange="determineAction();">
                                                <option value="allTasks" selected="" label=""></option>
                                                <option value="com.ibm.isclite.MyTaskFilter" label=""></option>
                                                <option value="PF+com.ibm.websphere.product" label="">
                                                </option>
                                            </select>
                                            <noscript>
                                                <input type="submit" title='' value=''>
                                            </noscript>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td align="left" colspan="2" class="navtree" style="background-color:#FFFFFF">
                                            <ul class="nav-child" dir="ltr">
                                                <li class="navigation-bullet"><a style="text-decoration:none"
                                                        href="/ibm/console/navigation.do?wpageid=com.ibm.isclite.welcomeportlet.layoutElement.A&amp;moduleRef=com.ibm.isclite.ISCAdminPortlet"
                                                        target="detail" dir="ltr" title=""></a></li>
                                            </ul>
                                            <div nowrap="" class="main-task" style="margin-left:0.3em;"><a
                                                    style="color:#000000;text-decoration:none;"
                                                    href="javascript:expandCollapse('0');" title=""><img
                                                        src="/ibm/console/images/arrow_collapsed.gif" title="Expand"
                                                        alt="Expand" id="I0" border="0" align="absmiddle"></a></div>
                                            <div class="nav-child-container" style="margin-left: 0.3em; display: none;"
                                                id="N0">
                                                <ul class="nav-child" dir="ltr">
                                                    <li class="navigation-bullet"><a style="text-decoration:none"
                                                            href="/ibm/console/navigatorCmd.do?forwardName=guidedactivity.resource&amp;WSC=true"
                                                            target="detail" dir="ltr" title=""></a></li>
                                                </ul>
                                                <ul class="nav-child" dir="ltr">
                                                    <li class="navigation-bullet"><a style="text-decoration:none"
                                                            href="/ibm/console/navigatorCmd.do?forwardName=guidedactivity.web&amp;WSC=true"
                                                            target="detail" dir="ltr" title=""></a></li>
                                                </ul>
                                            </div>
                                            <div nowrap="" class="main-task" style="margin-left:0.3em;"><a
                                                    style="color:#000000;text-decoration:none;"
                                                    href="javascript:expandCollapse('1');" title=""><img
                                                        src="/ibm/console/images/arrow_collapsed.gif" title="Expand"
                                                        alt="Expand" id="I1" border="0" align="absmiddle"></a></div>
                                            <div class="nav-child-container" style="margin-left: 0.3em; display: none;"
                                                id="N1">
                                                <ul class="nav-child" dir="ltr">
                                                    <li class="navigation-bullet"><a style="text-decoration:none"
                                                            href="/ibm/console/navigatorCmd.do?forwardName=ApplicationServer.content.main&amp;WSC=true"
                                                            target="detail" dir="ltr" title=""></a></li>
                                                </ul>
                                                <ul class="nav-child" dir="ltr">
                                                    <li class="navigation-bullet"><a style="text-decoration:none"
                                                            href="/ibm/console/navigatorCmd.do?forwardName=WebServer.content.main&amp;WSC=true"
                                                            target="detail" dir="ltr" title=""></a></li>
                                                </ul>
                                                <ul class="nav-child" dir="ltr">
                                                    <li class="navigation-bullet"><a style="text-decoration:none"
                                                            href="/ibm/console/com.ibm.ws.console.sib.sibresources.forwardCmd.do?forwardName=SIBMQServer.content.main&amp;WSC=true"
                                                            target="detail" dir="ltr" title=""></a>
                                                    </li>
                                                </ul>
                                            </div>
                                            <div nowrap="" class="main-task" style="margin-left:0.3em;"><a
                                                    style="color:#000000;text-decoration:none;"
                                                    href="javascript:expandCollapse('2');" title="Aplications"><img
                                                        src="/ibm/console/images/arrow_expanded.gif" title="Expand"
                                                        alt="Expand" id="I2" border="0"
                                                        align="absmiddle">Aplications</a></div>
                                            <div class="nav-child-container" style="margin-left: 0.3em; display: block;"
                                                id="N2">
                                                <ul class="nav-child" dir="ltr">
                                                    <li class="navigation-bullet"><a style="text-decoration:none"
                                                            href="/ibm/console/navigatorCmd.do?forwardName=ApplicationDeployment.content.main&amp;WSC=true"
                                                            target="detail" dir="ltr" title=""></a>
                                                    </li>
                                                </ul>
                                                <ul class="nav-child" dir="ltr">
                                                    <li class="navigation-bullet"><a style="text-decoration:none"
                                                            href="/ibm/console/com.ibm.ws.console.appmanagement.action.forwardCmd.do?forwardName=appmanagement.upload&amp;WSC=true"
                                                            target="detail" dir="ltr" title=""></a></li>
                                                </ul>
                                            </div>
                                        </td>
                                    </tr>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </body>
                </html>
                <frame title="Content Mark"
                    src="/ibm/console/navigation.do?wpageid=com.ibm.isclite.welcomeportlet.layoutElement.A&amp;moduleRef=com.ibm.isclite.ISCAdminPortlet"
                    name="detail" resize="yes" marginwidth="0" marginheight="0">
        </frameset>
</frameset>
<noframes>
    ...
</noframes>
</html>

每当你遇到frameset记住你不需要切换它让selenium与内部元素交互。

改为切换到框架本身(实施 WebDriverWait 以获得更高的稳定性):

WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.name("navigation")));

最后你可以选择你想要的元素:

element = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("/html/body/div/div/table/tbody/tr[2]/td/div[5]/a")));
// Click to expand Aplicactions options
element.click();

不建议使用 绝对 xpath 切换到相对 xpath。