我一直在尝试使用黄瓜和硒自动进行一些手动检查,并尝试在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">
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>
我正在尝试单击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)
请提出这是否是正确的方法?提前谢谢了。
更新:按照@DebanjanB的答案,使用建议的代码段(css选择器和xpath)进行了尝试,
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();
在第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)
通过按索引切换内容,我能够使它正常工作。使用System.out.println(driver.getPageSource());
登录页面的源代码,以安慰,并确保我在正确的位置工作。我试图通过标签找到iframe,并按以下方式打印其名称。
List<WebElement> elements = DriverContext.driver.findElements(By.tagName("iframe"));
elements.forEach(element -> System.out.println(element.getAttribute("name"))); // printed 'menu' and 'body', perfect
但是,在使用名称将内容切换到iframe的同时,还尝试使用WebDriverWait,
driver.switchTo().frame(driver.findElement(By.name("menu")));
无论出于什么原因,它都无法正常工作,并不断抛出异常。
使用索引切换到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"));
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句