简体   繁体   中英

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>

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM