ElementNotVisibleException failure while implementing Selenium waits

Modified on Tue, 28 Mar, 2023 at 4:24 PM

Error Name:

 ElementNotVisibleException


Description: 

ElementNotVisibleException is a Selenium WebDriver error that occurs when it is unable to interact with an element on the webpage because it is not visible or hidden. This can happen if the element is obscured by another element, or if it is present in the DOM but not yet fully loaded.


Understanding the error: The ElementNotVisibleException occurs when Selenium is unable to interact with a certain element on the webpage due to its invisibility.


Causes: 

This error can occur for various reasons such as:


The element is hidden or obscured

The element is present in an iFrame

The element is disabled

Line Number and File Name: Check the line number and file name where the ElementNotVisibleException is being thrown to identify the location of the error.


Check Causes: Once the error is identified, check the above-mentioned causes to determine which one is causing the error.


Debugs:

Debug-1: 

Verify the presence of the element by using browser developer tools or the Page Object Model.

Example:

public class LoginPage {

    private WebDriver driver;

    private WebElement usernameField;

    private WebElement passwordField;

    private WebElement loginButton;


    public LoginPage(WebDriver driver) {

        this.driver = driver;

        PageFactory.initElements(driver, this);

    }


    @FindBy(id = "username")

    public void setUsernameField(WebElement usernameField) {

        this.usernameField = usernameField;

    }


    @FindBy(id = "password")

    public void setPasswordField(WebElement passwordField) {

        this.passwordField = passwordField;

    }


    @FindBy(id = "loginButton")

    public void setLoginButton(WebElement loginButton) {

        this.loginButton = loginButton;

    }


    public void enterUsername(String username) {

        usernameField.clear();

        usernameField.sendKeys(username);

    }


    public void enterPassword(String password) {

        passwordField.clear();

        passwordField.sendKeys(password);

    }


    public void clickLoginButton() {

        loginButton.click();

    }

}


Debug-2: 

Check the visibility of the element by verifying the CSS properties of the element in the browser developer tools.

Debug-3: 

Implement explicit waits using the WebDriverWait class to wait for the element to become visible before interacting with it.

Debug-4: 

Adjust the timing of the implicit or explicit wait if the default wait time is not sufficient.


Recommendation: It is important to understand the root cause of the ElementNotVisibleException to implement the correct debugging method.



Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select at least one of the reasons
CAPTCHA verification is required.

Feedback sent

We appreciate your effort and will try to fix the article