Selenium WebDriver error for IE

2019-02-24 09:34发布

问题:

I am trying to automate test cases using selenium webdriver, junit and ant build. I am getting weird errors since morning. A test case contains button click command. The test runs success on Chrome and FF but not on IE. Earlier, it was at least saying that unable to find some element X, but this one says server did not provide any information.

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)

回答1:

I notice the following in the exception

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

This generally happens when the element you are clicking is obscured or hidden in the page. WebDriver uses native events, hence fails whenever you ask it to perform action on a hidden WebElement.

This wasn't a problem in Selenium RC since it deployed synthetic events (JS events) and could simulate a click on any DOM element irrespective of its visibility.



回答2:

In internet explorer, at least in most recent version 10 and previous 9, the DOM fails to get fully reloaded or visible to WebDriver in single-page apps or heavy ajax pages where the DOM is dynamically created. I have found a workaround for now is to simply refresh the page

driver.navigate().refresh();

I realize this may seem like a hack but it does force the IE browser to reload the page and draw the current expected DOM elements. Even inserting WebDriverWait's didn't help (though this is best practice and should be implemented in most all cases when working with ajax heavy apps).

During my experience I was using the latest webdriver (2.31.0) version within a Java project and IE 10 (in and out of compat mode).

Once I figure out why IE does this, I will update this answer to a more longterm portable solution then just refreshing the page. For now, I moved on to use Chrome Driver and implement Chrome Frame in IE.



回答3:

Insert following block before you fire click event

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

In my case, I have super class for the test case, that is why I can do

session().somecommand

But, you can translate my solution into yours.



回答4:

in my case the problem was that the submit process was taking too long, like more than two minutes and my problem was solved wrapping click action on a try catch and add a sleep for the process to finish, then continue. Code as follows

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