How to click on hidden element with Selenium WebDriver ?
Solution :
Lets first create the instance of WebDriver :
Solution :
It is very hot question asked in the Automation Interview.
Here, I will show you how you can click on Hidden Element with Selenium & Javascript.
For interacting with Hidden Elements we need to use "JavascriptExecutor" Interface.
Now, I need to perform click operation on the button which is disabled in DOM by using style=”display: none;”
Lets first create the instance of WebDriver :
WebDriver driver; System.setProperty("webdriver.chrome.driver", "C:/Users/User/Downloads/chromedriver/chromedriver.exe"); driver = new ChromeDriver();Next, we will open the url and find the WebElement i.e Button.
driver.get("file:///C:/Users/User/Desktop/Blog/Hidden.html"); WebElement btn = driver.findElement(By.cssSelector("button[id='myButton']"));
Here, we will create the object of "JavascriptExecutor" Interface and cast our WebDriver to "JavascriptExecutor".
After that we will use executeScript Method of JavascriptExecutor which will perform our task i.e "Click on hidden button".
JavascriptExecutor jsExecutor = (JavascriptExecutor)driver; jsExecutor.executeScript("arguments[0].click();", btn);
Hope this will help you. Please share your views in Comment section below.
Thanks a lot.