【问题标题】:How to find an element within a frame in IE?如何在 IE 中找到一个框架内的元素?
【发布时间】:2018-06-05 08:06:02
【问题描述】:

代码试验:

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 Status: '404' -> incorrect JSON status mapping for 'stale element reference' (400 expected)

错误堆栈跟踪:

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)

【问题讨论】:

  • 你使用什么 xpath ?用 xpath 更新问题。尝试使用不同的定位器
  • 请阅读为什么screenshot of HTML or code or error is a bad idea。考虑使用基于格式化文本的相关 HTML、代码试验和错误堆栈跟踪来更新问题。
  • @DebanjanB 我已经更新了错误堆栈跟踪。
  • @cruisepandey 我已经更新了我使用的 xpath。我已经尝试过定位器(id,cssSelector)
  • @cruisepandey 一个是输入ID,另一个是输入名称。两者具有相同的价值

标签: java selenium internet-explorer iframe iedriverserver


【解决方案1】:

此错误消息...

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 浏览器 时,请确保:

  • 您已按照InternetExplorerDriver 的文档满足Required Configuration 以及Native EventsBrowser Focus 的其他注意事项。
  • 您已将 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']")));
    

【讨论】:

  • 我试过这个,我得到了以下错误:`org.openqa.selenium.TimeoutException:预期条件失败:等待 By.xpath 定位的元素的可见性://input[@class='inputfld' 和 @id='MF:txtentdon'](在 org.openqa.selenium.support.ui.WebDriverWait.timeoutException(WebDriverWait.java:80) 的 org.openqa.selenium.support.ui.WebDriverWait.timeoutException(WebDriverWait.java:80) 尝试 20 秒,间隔为 500 MILLISECONDS) .openqa.selenium.support.ui.FluentWait.until(FluentWait.java:232) 原因:org.openqa.selenium.NoSuchElementException:无法使用 By.xpath 定位元素://input[@class='inputfld' 和@id='MF:txtentdon']`
  • 我不知道这是否重要,但它也会抛出INFO: HTTP Status: '404' -> incorrect JSON status mapping for 'stale element reference' (400 expected)
  • 嗯,也许您需要交叉检查您的 IEDriverServer 配置
  • 我已经提到我使用的是 IE9 和 IEDriverServer 版本 3.12.0。保护模式已启用,缩放级别设置为 100%。还有其他配置要检查吗?
  • 即使它没有抛出 NoSuchFrameException ,我也怀疑它是否真的切换了。
【解决方案2】:

你可以试试这段代码:

driver.switchTo().frame(driver.findElement(By.xpath("//iframe[contains(@src,'path')]")));  
System.err.println("inside frame");
WebElement element = new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.id("MF:txtentdon")));

或者

WebElement element =  new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.name("MF:txtentdon")));

【讨论】:

  • inside frame org.openqa.selenium.TimeoutException: Expected condition failed: waiting for element to be clickable: By.name: MF:txtentdon (tried for 10 second(s) with 500 MILLISECONDS interval) at org.openqa.selenium.support.ui.WebDriverWait.timeoutException(WebDriverWait.java:80)
  • 网址是公开的吗?
  • 没有。不公开
  • @Ray :您是否面临过时的元素引用?
  • 其实我在INFO: HTTP Status: '404' -> incorrect JSON status mapping for 'stale element reference' (400 expected)下面得到一个INFO
猜你喜欢
  • 1970-01-01
  • 2020-12-29
  • 1970-01-01
  • 1970-01-01
  • 2016-07-16
  • 1970-01-01
  • 2018-08-01
  • 2021-11-22
  • 2020-07-17
相关资源
最近更新 更多