I am automating a site in which there is a section to create forums. Every time I create a forum it displays message as forums created and hides after 1 section. I have to check the displayed message, But many times it throws exception as "Element is not usable"

private WebElementFacade forums;

private WebElementFacade message;
 assertThat(message.getText(),equals("expected"); //here the element message throws "Element is not usable exception" as it hides with in two seconds.. 

public String getMessage() {
    String messagetxt = "";
    try {
        if (Message.isDisplayed()) {
            messagetxt = Message.getText();
        } else {
            messagetext = ((JavascriptExecutor) getDriver()).executeScript(
                    "return arguments[0].innerHTML", Message).toString();
    } catch(NoSuchElementException ne) {
        logger.info("status message not displayed..... checking from the page source"+ nsee);
    return messagetxt;

How can I find the element or at least handle the exception

  • as per the reply by sam woods i have made some changes in the code and my new code is available above, here I have handled exception. But the else block is not executed, Is there any way to execute the else block once the exception occurs?? – Ajmal Feb 7 '14 at 10:44

You may or may not be able to interact with the element in time, depending on the speed of your computer and your browser.

There are selectors that are faster or slower. Typically, selecting by ID is the fastest, followed by selecting by Name, and in most browsers, CSS selectors are the next fastest. Since you are already selecting by ID, there's probably not a whole lot you can do to make it find the element faster.

If you are just trying to validate the text of the message, you can use the javascript executor to execute some javascript to get back the text of the element even if it is hidden, but you won't be able to validate that the message was actually displayed - only that the text of the message is what you expect.

Not sure exactly what language you're using, but in Java you could write something like this:

catch(Exception e){
  //Log unable to verify message displayed
String messageText = ((JavascriptExecutor) driver).executeScript("return arguments[0].innerHTML", message);

How to Pause a WebPage to get a locater

There are times when there is something on a page that we need to get the WebDriver locater for, but the object disappears too quickly to inspect it.

Here is how to get it:

  1. Open Chrome (I believe this only works in Chrome)
  2. Go to the desired URL
  3. Open the Development Tools (Ctrl+Shift+I)

    a. For this case, I like to have the Development Tool window as a new Window

  1. Do the action needed to get to your tricky element
  2. As soon as the element is in the state you wish, hit F8

    a. This will pause the DOM.
    b. You can then expect the element
    c. There is a icon on the page to resume
    d. NOTE: This takes a ton of memory, your system might slow way down. Work fast and then un-pause.


Here you can find a lot of help. There are lot of people who use selenium/webdriver.

  • Why I get -1? I show him/here where he can find help. Did I do something wrong? – SayusiAndo Feb 6 '14 at 17:40
  • Probably because you didn't answer the question you just linked to a user group with no context. – Ardesco Feb 7 '14 at 14:39
  • Probably. Tahnks for the answer! :) – SayusiAndo Feb 7 '14 at 14:49
  • How about you write the solution here and use the link only as a reference? – IAmMilinPatel Oct 1 '16 at 7:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.