提问者:小点点

Selenium Webdriver-Java-单击iframe内部框架内的链接


我一直在尝试使用cucumber和selenium自动执行一些手动检查,并尝试在iframe内的菜单项上创建一个单击事件。 下面是源头的骨架。

<html>
<head>...</head>
<frameset rows="100%,*" border="0">
    <frame src="/XXXX/index.dsp" cd_frame_id_="03af6e390xxxxxxxxx0b209e24f67b9fab">
        <html>
            <body>  
                <iframe class="menuframe" name="menu" src="menu.dsp" scrolling="yes" seamless="seamless"></iframe>
                <html>
                <head>..</head>
                <body class="menu"...........>
                    <table class="menuTable".............>
                    <tbody>     

                        <tr>......</tr>
                        <tr>......</tr>
                        <tr>......</tr>

                        <tr manualhide="true" onclick="toggle(this, 'XXX_subMenu', 'XXXX_twistie');" onmouseover="this.className='cursor';" class="cursor">
                            <td class="menusection menusection-collapsed" id="elmt_XXXX_subMenu">
                                <img id="XXXX_twistie" src="/XXXX/images/collapsed_blue.png">
                                  &nbsp;XXXX  
                            </td>
                        </tr>
                        <tr>......</tr>
                        <tr>......</tr>
                </iframe>

                <iframe class="contentframe" name="body" id="body" src="stats-general.dsp">
                .............
                </iframe>

            </body>
        </html>


    </frame>
</frameset>

null

我正在尝试用id ELMT_XXXX_SUBMENU点击上面的内容。

正如我在一些博客中读到的,它需要切换到框架。 我已经尝试了以下操作。

driver.switchTo().defaultContent();
// switch to frame
driver.switchTo().frame(0); 

// switch to menu iframe, It throws an exception
driver.switchTo().frame("menu"); 

// When replaced by following line also it throws same exception
// driver.switchTo().frame(driver.findElement(By.name("menu")));

下面是堆栈跟踪。

 org.openqa.selenium.JavascriptException: javascript error: Failed to execute 'getComputedStyle' on 'Window': parameter 1 is not of type 'Element'.
  (Session info: chrome=83.0.4103.97)
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:17:03'
System info: host: 'XXXX', ip: 'XX.XX.XXX.XXX', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_221'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 83.0.4103.97, chrome: {chromedriverVersion: 83.0.4103.39 (ccbf011cb2d2b..., userDataDir: C:\Users\XXXXXX~1\AppData\L...}, goog:chromeOptions: {debuggerAddress: localhost:58320}, javascriptEnabled: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: WINDOWS, platformName: WINDOWS, proxy: Proxy(), setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify, webauthn:virtualAuthenticators: true}
Session ID: f2e850e3e43d7782902297879bc70bc4
    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$RemoteTargetLocator.frame(RemoteWebDriver.java:892)
    at XXX.XXXX.XXXX.pages.TestPage.test(TestPage.java:40)
    at XXX.XXXX.XXXX.stepdefinitions.TestSteps.test_method(TestSteps.java:137)
    at ?.Then Check menu(basicChecks.feature:12)

如果这不是正确的方法,请提出建议? 事先多谢。


共2个答案

匿名用户

我不确定这是否与此有关,但会不会是您的桌子上少了一个结束标签?

匿名用户

.frame()用于将元素切换到iframe,它以元素作为参数,因此您需要将元素传递到那里。 另外,带“0”的元素不是iframe的类型,因此不需要driver.switchTo().frame(0)代码行。

您只需将驱动程序切换到菜单iframe,然后单击元素。

要切换到菜单iframe,需要使用:

driver.switchTo().frame(driver.findElement(By.name("menu")));