Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用SeleniumWebDriver和Java单击位于框架内的iframe内的链接_Java_Selenium_Selenium Webdriver_Iframe_Webdriverwait - Fatal编程技术网

如何使用SeleniumWebDriver和Java单击位于框架内的iframe内的链接

如何使用SeleniumWebDriver和Java单击位于框架内的iframe内的链接,java,selenium,selenium-webdriver,iframe,webdriverwait,Java,Selenium,Selenium Webdriver,Iframe,Webdriverwait,我一直在尝试使用cumber和selenium自动执行一些手动检查,并尝试在iframe内的菜单项上创建一个单击事件。下面是源代码的框架 <html> <head>...</head> <frameset rows="100%,*" border="0"> <frame src="/XXXX/index.dsp" cd_frame_id_="03af6e390xxxxxxxxx0b209e24f67b9fab">

我一直在尝试使用cumberselenium自动执行一些手动检查,并尝试在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">
                    <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>
                            </tbody>
                    </body>
                </iframe>

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

            </body>
        </html>
    </frame>
</frameset>
下面是stacktrace

 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)
如果这不是正确的方法,请提出建议? 非常感谢

更新: 根据@DebanjanB的回答,尝试使用建议的代码段(css选择器和xpath)

在第2行引发相同的异常

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: 79f14698ee49ff2f4308b7f7930ee8b1
    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 org.openqa.selenium.support.ui.ExpectedConditions$17.apply(ExpectedConditions.java:501)
    at org.openqa.selenium.support.ui.ExpectedConditions$17.apply(ExpectedConditions.java:497)
    at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:249)
    at XXX.XXXX.XXXX.pages.TestPage.test(TestPage.java:26)
    at XXX.XXXX.XXXX.stepdefinitions.TestSteps.test_method(TestSteps.java:137)
    at ?.Then Check menu(basicChecks.feature:12)
.frame()
用于将元素切换到
iframe
,它将元素作为参数,因此需要将元素传递到那里。另外,带有“0”的元素不是iframe的类型,因此不需要
驱动程序.switchTo().frame(0)
代码行

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

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

driver.switchTo().frame(driver.findElement(By.name("menu"))); 
单击
的元素()
,该元素位于
中,因此您必须:

  • 诱导WebDriverWait使第一个框架可用并切换到它
  • 诱导WebDriverWait使第二个框架可用并切换到它
  • 将所需元素的WebDriverWait诱导为可禁用
  • 您可以使用以下任一选项:

    • 使用cssSelector:

    • 使用xpath:

注意:根据您提供的当前HTML,以下
看起来已关闭,但可能不正确:



参考文献 您可以在以下内容中找到一些相关讨论:


通过按索引切换内容,我可以让它正常工作。使用
System.out.println(driver.getPageSource())
将页面源代码记录到控制台,并确保我在正确的位置工作。 我试图通过标记找到iFrame,并按如下方式打印它们的名称

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")));
List<WebElement> elements = DriverContext.driver.findElements(By.tagName("iframe"));
elements.forEach(element -> System.out.println(element.getAttribute("name"))); // printed 'menu' and 'body', perfect
不管出于什么原因,它都不起作用,并像问题中那样不断抛出异常

使用index切换到iframe,然后单击元素

driver.switchTo().frame(0); // switch to frame
driver.switchTo().frame(0); // switch to first iframe

driver.findElement(By.id("elmt_XXXX_subMenu")).click();
此外,我还使用WebDriverWait等待页面加载

WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(webDriver -> ((JavascriptExecutor) driver).executeScript("return document.readyState").toString().equals("complete"));

第二个
结束标记从未在
内启动。交叉检查HTML。非常感谢您的详细回答。很抱歉,在问题中编写html框架时出错。我刚刚更新了它。使用css选择器和xpath,我在第二行得到了相同的异常(问题中的stacktrace)。新的WebDriverWait(驱动程序,10).until(ExpectedConditions.FrameToBeavailable和SwitchToIt(通过.cssSelector(“iframe.menuframe[name='menu'])));Log:org.openqa.selenium.JavascriptException:javascript错误:未能在“窗口”上执行“getComputedStyle”:参数1不是“元素”类型。@NagarajaJB而不是手工编制的HTML。请使用基于文本的实际HTML更新问题?我可以使用索引再次将其切换到iframe。非常感谢你的回答。如果我试图直接切换到iframe,我会在org.openqa.selenium.NoSuchElementException下面看到。我也尝试过添加WebDriverWait。原因:org.openqa.selenium.NoSuchElementException:没有这样的元素:无法定位元素:{“方法”:“css选择器”,“选择器”:“*[name='menu']”如果切换到框架并重试,则会出现问题中的异常。
<iframe class="menuframe" name="menu" src="menu.dsp" scrolling="yes" seamless="seamless"></iframe>
List<WebElement> elements = DriverContext.driver.findElements(By.tagName("iframe"));
elements.forEach(element -> System.out.println(element.getAttribute("name"))); // printed 'menu' and 'body', perfect
driver.switchTo().frame(driver.findElement(By.name("menu")));
driver.switchTo().frame(0); // switch to frame
driver.switchTo().frame(0); // switch to first iframe

driver.findElement(By.id("elmt_XXXX_subMenu")).click();
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(webDriver -> ((JavascriptExecutor) driver).executeScript("return document.readyState").toString().equals("complete"));