How to click on hidden element in Selenium WebDriver

Hi, I have a simple test of few clicks on a webpage. But I’m getting ElementNotVisibleException

I did some search and found the reason to be Hidden element. So can anyone help how to click on a hidden element?

3 Likes

If you are using Javascript, you can click at it even if the element is hidden.

/**
* Clicks on an element that is visible or not.
*
* @param element Web element.
*/
public void jsClick(WebElement element) {
  ((JavascriptExecutor) this.driver).executeScript("return arguments[0].click();", element);
}
2 Likes

Thanks, But how can I do it in Java?

3 Likes

In Java, you cannot click if the element is hidden. So you need to make the element visible, by performing the action that can do that. For example:

public void hoverOverElementAndClick(WebElement element) {
  Actions action = new Actions(driver);
  action.moveToElement(element).perform();
  element.click();
}
3 Likes

Can I use the JavascriptExecutor in Java?

4 Likes

Yes. Here’s a sample code:

JavascriptExecutor js = (JavascriptExecutor)driver;  
js.executeScript("document.getElementByAttribute('attribute/locator').click();'");
2 Likes