1

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"

@FindBy(id="create_forums")
private WebElementFacade forums;

 @FindBy(id="forum_message")
private WebElementFacade message;
forums.click();
 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();
            logger.info("message"+messagetxt);
        }
    } 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
3

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.

| improve this answer | |
2

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:

try{
  assertThat(message.isDisplayed())
}
catch(Exception e){
  //Log unable to verify message displayed
}
String messageText = ((JavascriptExecutor) driver).executeScript("return arguments[0].innerHTML", message);
assertThat(messageText,equals("expected");
| improve this answer | |
1

I had to come across a situation similar to this. The drop-down which wasn't a select element was disappearing from the DOM for any mouse click or key-press. Due to this I wasn't able to select the desired value.

This is the time you need to use Developer Options of the browser. Below are the steps that you can follow with respect to the CHROME browser.

When you have access to source code:

  1. Press F12 to activate Developer Options
  2. Click on the Sources tab
  3. CTRL+SHIFT+F to search for the element text you see in the DOM
  4. Select the appropriate source file from the search result
  5. This loads the source code
  6. Add BreakPoints by left-clicking on the lines ribbon in the code
  7. Now do the actions in the webpage. If you encounter that line of code where the breakpoint is put, then execution stops and DOM is frozen.
  8. Now you can check the DOM to get all the elements you need

When you don't have access to source code and if the element is intact for few seconds, hitting F8 or Pause button from Debug Console freezes the DOM and you can capture the elements you need.

[Developer Options to find XPATH Locators in Selenium[1]

| improve this answer | |
-2

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

| improve this answer | |
  • 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.