user2796068
user2796068

Reputation: 1

isDisplayed() method always returns true for some elements

I'm using Selenium to test appearance of error message when input field is empty. Error message designed as a label to input element. When the message is invisible, it has attribute "display: none;".

When I find that message by text and call isDisplayed() method, it always returns true, even when message is invisible. I write tests on Java, so I have no isVisible() message.

I've tried method getAttribute("style"), but it returns empty string. Method getCssValue("display") returns "block" even when on page it has value "none".

I expected ElementNotVisibleException after calling click() method, but nothing happened!

Any ideas? Workarounds?

Here example of HTML:

<form id="from id" style="display: block;">

<input id="input" name="input">

<label for="input" generated="true" style="display: none;">Error text here.</label>

</from>

Upvotes: 0

Views: 2370

Answers (1)

Lori Johnson
Lori Johnson

Reputation: 438

Try using a WebDriverWait() to find the WebElement, and you can wait for the visibility of the element:

/**
 * 
 * Get a Web Element using WebDriverWait()
 * 
 */

public WebElement getInputBox() throws TimeoutException {

    WebElement webElement = null;
    WebDriverWait driverWait = new WebDriverWait(5);

    // find an element using a By selector

    driverWait.until(
            ExpectedConditions.visibilityOfElementLocated(By.cssSelector("#input")));

    webElement = driver.findElement(By.cssSelector("#input"));

    return webElement;
}

Upvotes: 1

Related Questions