4

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)
hrishikeshp19
  • 8,838
  • 26
  • 78
  • 141
  • 1
    Provide a sample page and sample code where you can reproduce this. Also look at WebDriverWait - just to ensure it isn't a timing issue. – Arran Jun 16 '12 at 11:26

4 Answers4

4

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.

Ashwin Prabhu
  • 9,285
  • 5
  • 49
  • 82
  • In general you can click on invisible elements on the page. If you think, the click happened too early, before the element could disclose itself, you should wait for the element to become visible before clicking. There is no other way around this using standard webdriver API's. – Ashwin Prabhu Jun 18 '12 at 09:45
  • You can try JavascriptExecutor interface to perform a click using JavaScript - which will definitely work, since you are stepping out of WebDriver sandbox and doing things yourself. That should be your last resort. – Ashwin Prabhu Jun 18 '12 at 09:46
  • how to wait for an element? can we specify some constant time wait using selenium IDE? – hrishikeshp19 Jun 18 '12 at 16:50
  • Hey, I used a workaround...if I wait for some text on the page under consideration it works. i.e. before firing click event, I do waitForTextPresent, that causes IE to wait until some text to be present before firing click event. @ashwin: Thanks for ur answer :) – hrishikeshp19 Jun 18 '12 at 17:19
  • Ok, That might work. But the right approach is to wait for visibility change. Text present looks into the page source and does not consider visibility AKAIK. – Ashwin Prabhu Jun 18 '12 at 17:47
  • 5
    WebDriverWait wait = new WebDriverWait(webDriver, timeoutInMillis); wait.until(ExpectedConditions.visibilityOfElementLocated(By.id)); or wait.until(ExpectedConditions.elementToBeClickable(By.id)); – Ashwin Prabhu Jun 18 '12 at 17:50
  • I am using wait but and element is visible but when click on it it returns javascripterror – Deepak Goel Jul 30 '12 at 10:00
  • @deepakgoel Please post a new question on SO with the stack trace. – Ashwin Prabhu Jul 30 '12 at 10:08
  • please hav a look http://stackoverflow.com/questions/11720126/javascripterror-while-using-internet-explorer-with-webdriverbackedselenium – Deepak Goel Jul 30 '12 at 11:01
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.

SilverColt
  • 409
  • 3
  • 4
1

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.

hrishikeshp19
  • 8,838
  • 26
  • 78
  • 141
0

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);
}
Romita Dinda
  • 81
  • 1
  • 4