【问题标题】:Cannot click element inside frame for JSP webpage using Java selenium无法使用 Java selenium 为 JSP 网页单击框架内的元素
【发布时间】:2019-12-24 06:33:40
【问题描述】:

我有一个带有框架的私有 JSP URL。

我在这里分享相同的快照。

正如您在快照中看到的那样,我希望单击“复制管理”,然后单击左侧框架中的“管理队列”。

我在这里共享相同的视图页面源和视图框架源。

查看页面来源:

<!-- Mark the file as a JSP and set up the classes to import -->

<HEAD>
<link rel="icon" href="/IDSWebApp/IDSgui/tds-16.ico" type="image/x-icon" />
<LINK REL="SHORTCUT ICON" HREF="/IDSWebApp/IDSgui/tds-16.ico" type="image/x-icon">
</HEAD>



<HTML dir='ltr'>

<HEAD>
<TITLE>
IBM Security Directory Server Web Administration Tool
</TITLE>
</HEAD>

<FRAMESET ROWS="40,*">


<FRAME SRC="/IDSWebApp/IDSjsp/IDSBannerFrame.jsp" TITLE="Banner Frame" NAME="BannerFrame" MARGINWIDTH="1" MARGINHEIGHT="1" SCROLLING="NO" NORESIZE FRAMEBORDER="1" FRAMESPACING="1" BORDERCOLOR="#000000">

<FRAMESET COLS="20%,80%">

<FRAME SRC="/IDSWebApp/IDSjsp/IDSNavFrame.jsp" TITLE="Navigation frame" NAME="NavFrame" MARGINWIDTH="1" MARGINHEIGHT="1" SCROLLING="AUTOMATIC" FRAMEBORDER="1" FRAMESPACING="1" BORDERCOLOR="#000000">

<FRAME SRC="/IDSWebApp/IDSjsp/IDSWorkFrame.jsp" TITLE="Work Area Frameset" NAME="WorkArea" MARGINWIDTH="1" MARGINHEIGHT="1" SCROLLING="AUTOMATIC" FRAMEBORDER="1" FRAMESPACING="1" BORDERCOLOR="#000000">

<NOFRAMES>
<BODY>
The IBM Security Directory Server Web Administration Tool does not currently support Web browsers that do not support frames. Contact the Directory Server administrator to find a Web browser that supports frames.
</BODY>
</NOFRAMES>

</FRAMESET>

<NOFRAMES>
<BODY>
The IBM Security Directory Server Web Administration Tool does not currently support Web browsers that do not support frames. Contact the Directory Server administrator to find a Web browser that supports frames.
</BODY>
</NOFRAMES>

</FRAMESET>

</HTML>

查看帧源:

<!-- Mark the file as a JSP and set up the classes to import -->

<HTML dir='ltr'>

<HEAD>
<style type="text/css">
FONT { font-size: 100%; }
.navHeading {PADDING-TOP: 4px;PADDING-BOTTOM: 4px;PADDING-LEFT: 2px;FONT-WEIGHT: bold;FONT-SIZE: 70%;FONT-FAMILY: Arial,Helvetica,sans-serif;BACKGROUND-COLOR: #D1D9E8}
.navTask {PADDING-LEFT: 2px;FONT-SIZE: 60%;FONT-FAMILY: Arial,Helvetica,sans-serif;BACKGROUND-COLOR: #D1D9E8}
</style>
<TITLE>
Navigation frame
</TITLE>
</HEAD>
<Body bgColor="#D1D9E8">

<table border=0 cellpadding=0 cellspacing=0 width="100%" bgcolor="#D1D9E8" xbgcolor="#000000">

<tr><td nowrap class="navHeading">
<IMG SRC="/IDSWebApp/IDSgui/Blank.gif" alt="" border="0" HEIGHT="16" WIDTH="16">
<A HREF="/IDSWebApp/IDSWelcome" Target="WorkFrame" BORDERCOLOR="#000000"><IMG SRC="/IDSWebApp/IDSgui/WnDocument.gif" title="Click here to launch Introduction" alt="Click here to launch Introduction" align="texttop" border="0" HEIGHT="16" WIDTH="16"><IMG SRC="/IDSWebApp/IDSgui/Blank.gif" alt="" border="0" align="texttop" HEIGHT="16" WIDTH="2"><font size="2" color="#454589" face="Arial,Helvetica,sans-serif" >Introduction</Font></A>
</td></tr>
<tr><td nowrap class="navHeading">
<A HREF="/IDSWebApp/IDSjsp/IDSNavFrame.jsp?opennav=idspasswd#spot" BORDERCOLOR="#000000"><IMG SRC="/IDSWebApp/IDSgui/WnTnClosed.gif" title="Click here to open User properties" alt="Click here to open User properties" align="texttop" border="0" HEIGHT="16" WIDTH="16"><IMG SRC="/IDSWebApp/IDSgui/WnFolderClosed.gif" title="Click here to open User properties" alt="" align="texttop" border="0" HEIGHT="16" WIDTH="16"><IMG SRC="/IDSWebApp/IDSgui/Blank.gif" alt="" border="0" align="texttop" HEIGHT="16" WIDTH="2"><Font size="2" color="#454589" face="Arial,Helvetica,sans-serif">User properties</Font></A></td></tr>
<tr><td nowrap class="navHeading">
<A HREF="/IDSWebApp/IDSjsp/IDSNavFrame.jsp?opennav=idssrvadm#spot" BORDERCOLOR="#000000"><IMG SRC="/IDSWebApp/IDSgui/WnTnClosed.gif" title="Click here to open Server administration" alt="Click here to open Server administration" align="texttop" border="0" HEIGHT="16" WIDTH="16"><IMG SRC="/IDSWebApp/IDSgui/WnFolderClosed.gif" title="Click here to open Server administration" alt="" align="texttop" border="0" HEIGHT="16" WIDTH="16"><IMG SRC="/IDSWebApp/IDSgui/Blank.gif" alt="" border="0" align="texttop" HEIGHT="16" WIDTH="2"><Font size="2" color="#454589" face="Arial,Helvetica,sans-serif">Server administration</Font></A></td></tr>
<tr><td nowrap class="navHeading">
<A HREF="/IDSWebApp/IDSjsp/IDSNavFrame.jsp?opennav=idsproxy#spot" BORDERCOLOR="#000000"><IMG SRC="/IDSWebApp/IDSgui/WnTnClosed.gif" title="Click here to open Proxy administration" alt="Click here to open Proxy administration" align="texttop" border="0" HEIGHT="16" WIDTH="16"><IMG SRC="/IDSWebApp/IDSgui/WnFolderClosed.gif" title="Click here to open Proxy administration" alt="" align="texttop" border="0" HEIGHT="16" WIDTH="16"><IMG SRC="/IDSWebApp/IDSgui/Blank.gif" alt="" border="0" align="texttop" HEIGHT="16" WIDTH="2"><Font size="2" color="#454589" face="Arial,Helvetica,sans-serif">Proxy administration</Font></A></td></tr>
<tr><td nowrap class="navHeading">
<A HREF="/IDSWebApp/IDSjsp/IDSNavFrame.jsp?opennav=idsdmt#spot" BORDERCOLOR="#000000"><IMG SRC="/IDSWebApp/IDSgui/WnTnClosed.gif" title="Click here to open Schema management" alt="Click here to open Schema management" align="texttop" border="0" HEIGHT="16" WIDTH="16"><IMG SRC="/IDSWebApp/IDSgui/WnFolderClosed.gif" title="Click here to open Schema management" alt="" align="texttop" border="0" HEIGHT="16" WIDTH="16"><IMG SRC="/IDSWebApp/IDSgui/Blank.gif" alt="" border="0" align="texttop" HEIGHT="16" WIDTH="2"><Font size="2" color="#454589" face="Arial,Helvetica,sans-serif">Schema management</Font></A></td></tr>
<tr><td nowrap class="navHeading">
<A HREF="/IDSWebApp/IDSjsp/IDSNavFrame.jsp?opennav=idsdirmgmt#spot" BORDERCOLOR="#000000"><IMG SRC="/IDSWebApp/IDSgui/WnTnClosed.gif" title="Click here to open Directory management" alt="Click here to open Directory management" align="texttop" border="0" HEIGHT="16" WIDTH="16"><IMG SRC="/IDSWebApp/IDSgui/WnFolderClosed.gif" title="Click here to open Directory management" alt="" align="texttop" border="0" HEIGHT="16" WIDTH="16"><IMG SRC="/IDSWebApp/IDSgui/Blank.gif" alt="" border="0" align="texttop" HEIGHT="16" WIDTH="2"><Font size="2" color="#454589" face="Arial,Helvetica,sans-serif">Directory management</Font></A></td></tr>
<tr><td nowrap class="navHeading">
<A HREF="/IDSWebApp/IDSjsp/IDSNavFrame.jsp?opennav=idsreplication#spot" BORDERCOLOR="#000000"><IMG SRC="/IDSWebApp/IDSgui/WnTnClosed.gif" title="Click here to open Replication management" alt="Click here to open Replication management" align="texttop" border="0" HEIGHT="16" WIDTH="16"><IMG SRC="/IDSWebApp/IDSgui/WnFolderClosed.gif" title="Click here to open Replication management" alt="" align="texttop" border="0" HEIGHT="16" WIDTH="16"><IMG SRC="/IDSWebApp/IDSgui/Blank.gif" alt="" border="0" align="texttop" HEIGHT="16" WIDTH="2"><Font size="2" color="#454589" face="Arial,Helvetica,sans-serif">Replication management</Font></A></td></tr>
<tr><td nowrap class="navHeading">
<A HREF="/IDSWebApp/IDSjsp/IDSNavFrame.jsp?opennav=idstid#spot" BORDERCOLOR="#000000"><IMG SRC="/IDSWebApp/IDSgui/WnTnClosed.gif" title="Click here to open Realms and templates" alt="Click here to open Realms and templates" align="texttop" border="0" HEIGHT="16" WIDTH="16"><IMG SRC="/IDSWebApp/IDSgui/WnFolderClosed.gif" title="Click here to open Realms and templates" alt="" align="texttop" border="0" HEIGHT="16" WIDTH="16"><IMG SRC="/IDSWebApp/IDSgui/Blank.gif" alt="" border="0" align="texttop" HEIGHT="16" WIDTH="2"><Font size="2" color="#454589" face="Arial,Helvetica,sans-serif">Realms and templates</Font></A></td></tr>
<tr><td nowrap class="navHeading">
<A HREF="/IDSWebApp/IDSjsp/IDSNavFrame.jsp?opennav=idsusrgrp#spot" BORDERCOLOR="#000000"><IMG SRC="/IDSWebApp/IDSgui/WnTnClosed.gif" title="Click here to open Users and groups" alt="Click here to open Users and groups" align="texttop" border="0" HEIGHT="16" WIDTH="16"><IMG SRC="/IDSWebApp/IDSgui/WnFolderClosed.gif" title="Click here to open Users and groups" alt="" align="texttop" border="0" HEIGHT="16" WIDTH="16"><IMG SRC="/IDSWebApp/IDSgui/Blank.gif" alt="" border="0" align="texttop" HEIGHT="16" WIDTH="2"><Font size="2" color="#454589" face="Arial,Helvetica,sans-serif">Users and groups</Font></A></td></tr>
<tr><td nowrap class="navHeading">
<IMG SRC="/IDSWebApp/IDSgui/Blank.gif" alt="" border="0" HEIGHT="16" WIDTH="16">
<A HREF="/IDSWebApp/IDSLogout" Target="_top" BORDERCOLOR="#000000"><IMG SRC="/IDSWebApp/IDSgui/WnDocument.gif" title="Click here to launch Logout" alt="Click here to launch Logout" align="texttop" border="0" HEIGHT="16" WIDTH="16"><IMG SRC="/IDSWebApp/IDSgui/Blank.gif" alt="" border="0" align="texttop" HEIGHT="16" WIDTH="2"><font size="2" color="#454589" face="Arial,Helvetica,sans-serif">Logout</Font></A>
</td></tr>

</table>
</BODY>
</HTML>

我尝试使用下面的 selenium java 代码(3 次尝试)获取框架,但它不起作用。

driver.switchTo().defaultContent();

driver.switchTo().frame(driver.findElement(By.xpath("//iframe[@title='Navigation frame']")));
driver.switchTo().frame(driver.findElement(By.xpath(".//iframe[@title='Navigation frame']")));
driver.switchTo().frame(driver.findElement(By.xpath(".//iframe[@src='http://myhost:1200/IDSWebApp/IDSjsp/IDSNavFrame.jsp?opennav=idsreplication#spot']")));

我收到以下错误:

*** Element info: {Using=xpath, value=//iframe[@title='Navigation frame']}
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:187)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:122)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49)
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158)
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:552)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:323)
at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:428)
at org.openqa.selenium.By$ByXPath.findElement(By.java:353)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:315)
at pack.QueueMonitor.testQualysScan(QueueMonitor.java:112)
at pack.QueueMonitor.main(QueueMonitor.java:185)

你能推荐一下吗?

【问题讨论】:

    标签: java selenium jsp automated-tests frame


    【解决方案1】:

    当您不确定嵌套 iframe 的工作方式时,您可以尝试以下方法来检查 iframe 是否存在,然后切换它并再次检查另一个 iframe,否则检查下一个 iframe 等等。值得一试。

    //Checking nested frames here
            if (driver.findElements(By.xpath("//iframe[@name='BannerFrame']")).size()>0)
            {
                System.out.println("inside frame 1");
                driver.switchTo().frame(driver.findElement(By.xpath("//iframe[@name='BannerFrame']")));
                if (driver.findElements(By.xpath("//iframe[@name='NavFrame']")).size()>0)
                {
                    System.out.println("inside frame 2");
                    driver.switchTo().frame(driver.findElement(By.xpath("//iframe[@name='NavFrame']")));
    
                    if (driver.findElements(By.xpath("//iframe[@name='WorkArea']")).size()>0)
                    {
                        System.out.println("inside frame 3");
                        driver.switchTo().frame(driver.findElement(By.xpath("//iframe[@name='WorkArea']")));
    
                    }
    
                }
                else
                {
                    if (driver.findElements(By.xpath("//iframe[@name='WorkArea']")).size()>0)
                    {
                        System.out.println("inside frame 3");
                        driver.switchTo().frame(driver.findElement(By.xpath("//iframe[@name='WorkArea']")));
    
                    }
    
                }
    
    
            }
            else
            {
                if (driver.findElements(By.xpath("//iframe[@name='NavFrame']")).size()>0)
                {
                    System.out.println("inside frame 2");
                    driver.switchTo().frame(driver.findElement(By.xpath("//iframe[@name='NavFrame']")));
    
                    if (driver.findElements(By.xpath("//iframe[@name='WorkArea']")).size()>0)
                    {
                        System.out.println("inside frame 3");
                        driver.switchTo().frame(driver.findElement(By.xpath("//iframe[@name='WorkArea']")));
    
                    }       
    
                }
                else
                {
                    if (driver.findElements(By.xpath("//iframe[@name='WorkArea']")).size()>0)
                    {
                        System.out.println("inside frame 3");
                        driver.switchTo().frame(driver.findElement(By.xpath("//iframe[@name='WorkArea']")));
    
                    }   
    
                }
    
    
            }
    
            //Click on Replication management link
            WebDriverWait wait=new WebDriverWait(driver, 10);
            wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//a[./font[text()='Replication management']]"))).click();
    

    【讨论】:

    • 我运行了您的代码,但没有满足任何 if 条件。你能提出一个解决方案吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-20
    • 2023-03-10
    • 2018-11-09
    • 2017-07-25
    • 2019-02-19
    相关资源
    最近更新 更多