【问题标题】:Selenium WebDriver error for IEIE 的 Selenium WebDriver 错误
【发布时间】:2017-05-29 15:37:18
【问题描述】:

我正在尝试使用 selenium webdriver、junit 和 ant build 自动化测试用例。从早上开始,我就遇到了奇怪的错误。一个测试用例包含按钮单击命令。测试在 Chrome 和 FF 上运行成功,但在 IE 上运行失败。之前,至少是说无法找到某些元素 X,但是这个说服务器没有提供任何信息。

Testcase: testMethod took 10.342 sec
    Caused an ERROR
Cannot click on element (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 172 milliseconds
Build info: version: '2.23.1', revision: '17143', time: '2012-06-08 18:59:04'
System info: os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.6.0_33'
Driver info: driver.version: RemoteWebDriver
Session ID: 8dfc5072-2755-40a7-bb32-05708c51101f
com.thoughtworks.selenium.SeleniumException: Cannot click on element (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 172 milliseconds
Build info: version: '2.23.1', revision: '17143', time: '2012-06-08 18:59:04'
System info: os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.6.0_33'
Driver info: driver.version: RemoteWebDriver
Session ID: 8dfc5072-2755-40a7-bb32-05708c51101f
    at org.openqa.selenium.internal.seleniumemulation.SeleneseCommand.apply(SeleneseCommand.java:41)
    at org.openqa.selenium.internal.seleniumemulation.Timer.run(Timer.java:38)
    at org.openqa.selenium.WebDriverCommandProcessor.execute(WebDriverCommandProcessor.java:144)
    at org.openqa.selenium.WebDriverCommandProcessor.doCommand(WebDriverCommandProcessor.java:74)
    at com.thoughtworks.selenium.DefaultSelenium.click(DefaultSelenium.java:193)
    at dmswebui.IE.TestLogin.testMethod(TestLogin.java:19)
Caused by: org.openqa.selenium.ElementNotVisibleException: Cannot click on element (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 172 milliseconds
Build info: version: '2.23.1', revision: '17143', time: '2012-06-08 18:59:04'
System info: os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.6.0_33'
Driver info: driver.version: RemoteWebDriver
Session ID: 8dfc5072-2755-40a7-bb32-05708c51101f
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:188)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:145)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:458)
    at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:244)
    at org.openqa.selenium.remote.RemoteWebElement.click(RemoteWebElement.java:77)
    at org.openqa.selenium.internal.seleniumemulation.Click.handleSeleneseCommand(Click.java:36)
    at org.openqa.selenium.internal.seleniumemulation.Click.handleSeleneseCommand(Click.java:1)
    at org.openqa.selenium.internal.seleniumemulation.SeleneseCommand.apply(SeleneseCommand.java:32)

【问题讨论】:

  • 提供一个示例页面和示例代码,您可以在其中重现此内容。另请查看 WebDriverWait - 只是为了确保它不是时间问题。

标签: internet-explorer selenium webdriver


【解决方案1】:

我在异常中注意到以下内容

Caused by: org.openqa.selenium.ElementNotVisibleException: Cannot click on element

当您单击的元素在页面中被遮挡或隐藏时,通常会发生这种情况。 WebDriver 使用本机事件,因此每当您要求它对隐藏的 WebElement 执行操作时都会失败。

这在 Selenium RC 中不是问题,因为它部署了合成事件(JS 事件)并且可以模拟对任何 DOM 元素的点击,而不管其可见性如何。

【讨论】:

  • 一般你可以点击页面上的不可见元素。如果您认为单击发生得太早,在元素可以显示自己之前,您应该等待元素变得可见,然后再单击。没有其他方法可以使用标准的 webdriver API。
  • 您可以尝试使用 JavascriptExecutor 界面来使用 JavaScript 执行单击 - 这肯定会起作用,因为您正在走出 WebDriver 沙箱并自己做事。这应该是你最后的手段。
  • 如何等待一个元素?我们可以使用 selenium IDE 指定一些恒定的时间等待吗?
  • 嘿,我使用了一种解决方法...如果我在考虑的页面上等待一些文本,它就可以工作。即在触发点击事件之前,我会waitForTextPresent,这会导致IE在触发点击事件之前等待一些文本出现。 @ashwin:谢谢你的回答:)
  • WebDriverWait 等待 = new WebDriverWait(webDriver, timeoutInMillis); wait.until(ExpectedConditions.visibilityOfElementLocated(By.id));或 wait.until(ExpectedConditions.elementToBeClickable(By.id));
【解决方案2】:

在 Internet Explorer 中,至少在最新版本 10 和之前的 9 中,DOM 无法在单页应用程序或动态创建 DOM 的重型 ajax 页面中完全重新加载或对 WebDriver 可见。我现在找到一个解决方法是简单地刷新页面

driver.navigate().refresh();

我意识到这可能看起来像是一个 hack,但它确实会强制 IE 浏览器重新加载页面并绘制当前预期的 DOM 元素。即使插入 WebDriverWait 也无济于事(尽管这是最佳实践,并且在使用 ajax 繁重的应用程序时应在大多数情况下实施)。

根据我的经验,我在 Java 项目和 IE 10 中使用最新的 webdriver (2.31.0) 版本(进出兼容模式)。

一旦我弄清楚 IE 这样做的原因,我会将此答案更新为更长期的可移植解决方案,然后只需刷新页面即可。现在,我继续使用 Chrome 驱动程序并在 IE 中实现 Chrome 框架。

【讨论】:

    【解决方案3】:

    在触发点击事件之前插入以下块

    for (int second = 0;; second++) {
        if (second >= 60) return "Page load failed";
        try {
            if (session().isTextPresent("Logoff")) 
                break;
        } 
        catch (Exception e) {}
        Thread.sleep(1000);
    }
    

    就我而言,我有测试用例的超类,这就是为什么我可以这样做

    session().somecommand
    

    但是,您可以将我的解决方案翻译成您的解决方案。

    【讨论】:

      【解决方案4】:

      在我的情况下,问题是提交过程花费的时间太长,比如超过两分钟,我的问题已解决,将单击操作包装在 try catch 上并添加睡眠以完成该过程,然后继续。代码如下

      try {
              button.click();
          } 
          catch (Exception e) 
      {
          Thread.sleep(1000);
      }
      

      【讨论】:

        猜你喜欢
        • 2015-03-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-09-29
        • 2015-08-26
        • 2017-04-11
        • 2016-01-04
        • 2018-04-04
        相关资源
        最近更新 更多