java - 如何在 IE 中查找框架内的元素?

标签 java selenium internet-explorer iframe iedriverserver

代码试验:

driver.switchTo().frame(driver.findElement(By.xpath("//iframe[contains(@src,'path')]")));
driver.findElement(By.id());

切换到 Frame 可能成功,但没有抛出任何 NoSuchFrameException。

Chrome 和 FireFox 中的情况也是如此。但在 IE 中却不起作用(在 IE9 和 IE11 中尝试过)。 使用的 WebDriver:IEDriverServer 版本:3.12.0.0

是因为文档模式吗?或者由于任何页面渲染问题? 什么情况下会发生这种情况?

HTML 源代码链接:Source Code

尝试查找 ID MF:txtentdon

我不知道这是否重要,但它还会抛出HTTP状态:'404' ->“过时元素引用”的JSON状态映射不正确(预期为400)

错误堆栈跟踪:

org.openqa.selenium.NoSuchElementException: Unable to find element with css selector == #MF\:txtentdon
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '3.4.0', revision: 'unknown', time: 'unknown'
System info: host: 'N9776', ip: '172.29.18.139', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_131'
Driver info: org.openqa.selenium.ie.InternetExplorerDriver
Capabilities [{proxy=Proxy(), acceptInsecureCerts=false, browserVersion=9, se:ieOptions={nativeEvents=true, browserAttachTimeout=0.0, ie.ensureCleanSession=false, elementScrollBehavior=0.0, enablePersistentHover=true, ie.browserCommandLineSwitches=, ie.forceCreateProcessApi=false, requireWindowFocus=false, initialBrowserUrl=http://localhost:6017/, ignoreZoomSetting=false, ie.fileUploadDialogTimeout=3000.0, ignoreProtectedModeSettings=false}, timeouts={implicit=0.0, pageLoad=300000.0, script=30000.0}, browserName=internet explorer, pageLoadStrategy=normal, javascriptEnabled=true, platformName=windows, setWindowRect=true, platform=ANY}]
Session ID: 48da0488-5d2e-46db-996e-77f27f26ff28
*** Element info: {Using=id, value=MF:txtentdon}
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:150)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:115)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:45)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:164)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:82)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:637)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:410)
    at org.openqa.selenium.remote.RemoteWebDriver.findElementById(RemoteWebDriver.java:453)
    at org.openqa.selenium.By$ById.findElement(By.java:218)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:402)

最佳答案

此错误消息...

org.openqa.selenium.NoSuchElementException: Unable to find element with css selector == #MF\:txtentdon

...意味着 InternetExplorerDriver 服务器无法找到所需的元素。

您的主要问题似乎是您正在使用的二进制文件版本之间的不兼容性,如下所示:

  • 您正在使用Selenium Java Client v3.4.0
  • 您正在使用IEDriverServer v3.12.0.0
  • 您使用的是JDK v1.8.0_131

因此,JDK v1.8.0-131Selenium Client v3.4.0IEDriverServer v3.12.0.0 之间存在明显不匹配.

解决方案

当您使用Selenium Java 客户端InternetExplorerDriverInternet Explorer 浏览器时,请确保:

  • 您已履行Required Configuration以及 Native Events 的其他注意事项和 Browser Focus根据 InternetExplorerDriver 的文档.
  • 您已将 JDK 升级到最新级别 JDK 8u171 .
  • 您已将Selenium升级到当前级别 Version 3.12.0 .
  • 您已将 InternetExplorerDriver 升级到当前的 IEDriverServer v3.12.0级别。
  • 通过 IDE清理您的项目工作区,并仅使用所需的依赖项重建项目。
  • 使用CCleaner工具可以清除执行测试套件之前和之后的所有操作系统杂务。
  • 重新启动系统
  • 执行您的@Test
  • 始终在 tearDown(){} 方法中调用 driver.quit() 来关闭和销毁 WebDriverWeb Client 优雅地实例化。

切换框架以定位元素时需要考虑的一些其他事实:

  • 在切换到所需的框架时始终引发WebDriverwait
  • 一旦切换到所需的框架,就会在您寻找所需的元素时引发WebDriverwait
  • 由于您所需的元素具有属性 readOnly="readonly",因此您需要使用 ExpectedConditions 作为 visibilityOfElementLocated()如下:
  • 因此您的有效代码块将是:

    new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[contains(@src,'path')]")));
    WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//input[@class='inputfld' and @id='MF:txtentdon']")));
    

关于java - 如何在 IE 中查找框架内的元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50695149/

相关文章:

java - 更改相邻 ImageButton 中的图像 (Android)

java - 通过 Apache Drill 访问 Postgres 中的复合数据

java - Tomcat 中的 Selenium Chrome 驱动程序为什么不工作?

selenium - 我们如何循环不同的基本URL而不是默认基本URL并运行功能测试?

javascript - 带有 JQuery-Slider 的 IE-Bug

java - android权限唤醒锁有什么用?

java - Full GC变得非常频繁

html - 基于 CSS 的悬停菜单在 IE7 中被隐藏

python - 使用 selenium python 从下拉框中选择每个选项

jquery - 使用 jQuery animate CSS opacity fade 和 @font-face 给 Internet Explorer 带来非常难看的字体渲染?