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
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.
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.
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.
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.
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']")
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")
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")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With