Selenium WebDriver Element is not visible error

Element is not visible error:

This error occurs when the target element is hidden below some other element (something like a dialog box) or it is hidden by the CSS in the page. In this case we have two options:

  1. Remove or hide the other element so that the target element is visible and the perform events on it.
    For this get the element

    //Element
    
    WebElement we = driver.findElement(By.id("elementid"));
    //Hide the element
    ((JavascriptExecutor)driver).executeScript("arguments[0].style.display='none'", we);
    

    and remove it or hide it using JavaScript or by selenium.

  2. In case visibility of the element is not important and still some action needs to performed then with the help of JavaScript webdriver can perform the click event on the target element and the visibility of the element will be ignored.Here is how we can do this:
    //Element
    
    WebElement we = driver.findElement(By.id("elementid"));
    
    // JSExecutor 
    
    JavascriptExecutor executor = (JavascriptExecutor)driver;
    
    // JS click 
    
    executor.executeScript("arguments[0].click();", we);

    This method works because the JavascriptExecutor object does not care whether the element is visible or not, but while doing it normal way the element needs to be visible.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s