0

代码试验:

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源码链接:源码

试图找到idMF: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)
4

2 回答 2

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 ClientInternetExplorerDriverInternet Explorer Browser时,请确保:

  • 根据InternetExplorerDriver的文档,您已完成所需的配置以及本机事件浏览器焦点的其他注意事项。
  • 您已将JDK升级到最新级别JDK 8u171
  • 您已将Selenium升级到当前级别Version 3.12.0
  • 您已将InternetExplorerDriver升级到当前的IEDriverServer v3.12.0级别。
  • 通过IDE清理项目工作区并仅使用所需的依赖项重建项目。
  • 使用CCleaner工具在执行测试套件之前和之后清除所有操作系统杂务。
  • 重新启动系统
  • 执行你的@Test.
  • 始终driver.quit()tearDown(){}方法内调用以优雅地关闭和销毁WebDriverWeb 客户端实例。

在切换框架以定位元素时,您需要考虑几个额外的事实:

  • 在切换到所需的时始终诱导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']")));
    
于 2018-06-05T10:53:41.707 回答
0

您可以尝试使用此代码:

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")));
于 2018-06-05T10:21:50.220 回答