Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium Webdriver: Element Not Visible Exception

Here is my code to click a simple login button on this Website

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;    
import org.openqa.selenium.WebDriver;    
import org.openqa.selenium.firefox.FirefoxDriver;    

public class Reports {

    public static void main(String[] args) {

        WebDriver driver = new FirefoxDriver();
        driver.get("https://platform.drawbrid.ge");
        driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);
        driver.findElement(By.xpath(".//*[@id='_loginButton']")).click();

    }
}

I am getting following error:

Exception in thread "main" org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with Command duration or timeout: 2.05 seconds

like image 935
Nik_stack Avatar asked Mar 02 '15 22:03

Nik_stack


People also ask

What is element not visible exception in Selenium?

Class ElementNotVisibleExceptionThrown to indicate that although an element is present on the DOM, it is not visible, and so is not able to be interacted with.

What do you do if the Webelement is not visible?

Element not visible exception can be resolved by using Explicit wait. Explicit wait in selenium will wait until the element is visible. Once it's visible you can perform the necessary operation. WebDriverWait wait = new WebDriverWait(driver,30); WebElement e = wait.

How do you handle invisible hidden elements in Selenium?

Selenium by default cannot handle hidden elements and throws ElementNotVisibleException while working with them. Javascript Executor is used to handle hidden elements on the page. Selenium runs the Javascript commands with the executeScript method. The commands to be run are passed as arguments to the method.

What is the difference between no such element and element not visible exception?

1) NoSuchElementException : FindBy method can't find the element. 2) StaleElementReferenceException : This tells that element is no longer appearing on the DOM page. 3) TimeoutException: This tells that the execution is failed because the command did not complete in enough time.


3 Answers

You have two buttons with given xpath on this page, first is not visible, thats why you are getting ElementNotVisibleException

One is under <div class="loginPopup">

Second (the one you need) is under <div class="page">

So change your xpath to look like this, and it will fix your problem:

By.xpath("//div[@class='page']//div[@id='_loginButton']")
like image 173
Dmitry Shyshkin Avatar answered Nov 10 '22 18:11

Dmitry Shyshkin


There are even 3 elements with id="_loginButton" on the page, and only one is visible - the one located inside the login form, you can get it by a CSS selector:

By.cssSelector("form#_loginForm div#_loginButton")
like image 40
alecxe Avatar answered Nov 10 '22 16:11

alecxe


There are 3 occurrences of id="_loginButton".

Used the id="_loginButton" under class="signIn" by cssSelector to get the exact button in the page.

By.cssSelector("div.signIn div#_loginButton")
like image 2
Abinaya Veluswamy Avatar answered Nov 10 '22 17:11

Abinaya Veluswamy