Internet explorer IE的Selenium WebDriver错误

Internet explorer IE的Selenium WebDriver错误,internet-explorer,selenium,webdriver,Internet Explorer,Selenium,Webdriver,我正在尝试使用SeleniumWebDriver、junit和AntBuild自动化测试用例。从早上开始我就犯了奇怪的错误。测试用例包含按钮单击命令。该测试在Chrome和FF上成功运行,但在IE上没有。之前,它至少说找不到某些元素X,但这一个说服务器没有提供任何信息 Testcase: testMethod took 10.342 sec Caused an ERROR Cannot click on element (WARNING: The server did not provi

我正在尝试使用SeleniumWebDriver、junit和AntBuild自动化测试用例。从早上开始我就犯了奇怪的错误。测试用例包含按钮单击命令。该测试在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)

我注意到以下例外情况

Caused by: org.openqa.selenium.ElementNotVisibleException: Cannot click on element
这通常发生在您正在单击的元素在页面中被遮挡或隐藏时。WebDriver使用本机事件,因此在您要求它对隐藏的WebElement执行操作时失败


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

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

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

但是,您可以将我的解决方案转换为您的解决方案。

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

driver.navigate().refresh()

我意识到这看起来像是一种黑客行为,但它确实迫使IE浏览器重新加载页面并绘制当前预期的DOM元素。即使插入WebDriverWait也没有帮助(尽管这是最佳实践,在大多数情况下,在使用ajax应用程序时都应该实现)

在我的经历中,我在一个Java项目和IE10中使用了最新的webdriver(2.31.0)版本(在compat模式下和在compat模式下)


一旦我弄明白IE为什么会这样做,我会将这个答案更新为一个更长期的可移植解决方案,然后刷新页面。现在,我开始使用Chrome驱动程序并在IE中实现Chrome框架。

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

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

提供一个示例页面和示例代码,您可以在其中复制此内容。还可以看看WebDriverWait——只是为了确保它不是一个时间问题。一般来说,你可以点击页面上不可见的元素。如果您认为,单击发生得太早,在元素暴露自己之前,您应该等待元素变得可见,然后再单击。使用标准的webdriver API没有其他方法可以解决这个问题。您可以尝试使用JavascriptExecutor界面来使用JavaScript执行单击操作——这肯定会起作用,因为您正在走出webdriver沙箱,自己做事情。那应该是你最后的选择。如何等待一个元素?我们可以使用selenium IDE指定一些固定的等待时间吗?嘿,我使用了一个变通方法…如果我等待正在考虑的页面上的一些文本,它就会工作。i、 e.在触发click事件之前,我确实waitForTextPresent,这会导致IE在触发click事件之前等待一些文本出现@阿什温:谢谢你的回答:)WebDriverWait wait=newwebdriverwait(webDriver,timeoutinmilis);等待.直到(预期条件.元素的可视性(通过.id));或者等到(ExpectedConditions.elementtobelickable(By.id));