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?
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?
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);
}
Thanks, But how can I do it in Java?
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();
}
Can I use the JavascriptExecutor in Java?
Yes. Here’s a sample code:
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("document.getElementByAttribute('attribute/locator').click();'");