我一直在尝试使用cucumber和selenium自动执行一些手动检查,并尝试使iframe内的菜单项发生click事件。下面是源代码的骨架。
<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">
XXXX
</td>
</tr>
<tr>......</tr>
<tr>......</tr>
</iframe>
<iframe class="contentframe" name="body" id="body" src="stats-general.dsp">
.............
</iframe>
</body>
</html>
</frame>
</frameset>
我正在尝试单击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)
请提示这是否是正确的方法?提前非常感谢。
.frame()
用于将元素切换为iframe
,它以元素为参数,因此您需要将元素传递到那里。另外,带有“ 0”的元素也不是iframe类型,因此您不需要代码的driver.switchTo().frame(0)
行。
您只需要将驱动程序切换到menu
iframe,然后单击该元素即可。
要切换到menu
iframe,您需要使用:
driver.switchTo().frame(driver.findElement(By.name("menu")));
到click()
内<iframe>
内元素的<frame>
,因此您必须:
cssSelector
new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.cssSelector("frame[src*='index']")));
new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.cssSelector("iframe.menuframe[name='menu']")));
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.cssSelector("td.menusection.menusection-collapsed#elmt_XXXX_subMenu"))).click();
xpath
new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//frame[contains(@src, 'index')]")));
new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[@class='menuframe' and @name='menu']")));
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//td[@class='menusection menusection-collapsed' and @id='elmt_XXXX_subMenu']"))).click();
注意:根据当前HTML,您提供了以下
<iframe>
看起来是关闭的,可能不正确:<iframe class="menuframe" name="menu" src="menu.dsp" scrolling="yes" seamless="seamless"></iframe>
参考您可以在以下位置找到几个相关的讨论: