Do you know we can also Click in Selenium webdriver using JavaScript using different approaches. I have covered previously as well like How to perform click operation in Selenium Webdriver but now we are going to talk about something new I mean how to click in Selenium webdriver using JavaScript for an element which is disabled.
While working with disabled web element you will get Illegal State exception which might be new for you. You can easily come out using JAVAScript. This is one of the most important questions in Interviews as well.
If you are completely new to JavaScript then I would highly recommend you to visit JavaScript in Selenium and Different usage.
Overview
Let me tell you what exactly you will learn today and why I am covering this.
I had one scenario where some element was invisible and the requirement was I had to click on that elements. As we know Selenium does not that capability that it can change the visibility of Element.
JavaScript is much powerful which takes the complete access of DOM, so using JavaScript we can make any element visible forcefully.
Finally, I completed my script using JavaScript and performed all the required operation.
Approach for – How to click in Selenium webdriver using JavaScript.
If the element is invisible and if you try to click on any operation then it will throw Element Not visible exception which will fail your script. You can handle this exception using try catch approach as well but we have another approach using JavaScript.
- First, we will identify what exception is coming from the script.
- If an exception is related to disable or visibility then we can create the object of JavaScriptExecutor.
- Use Selenium code to perform or execute JavaScript code.
We can execute any JavaScript using JavaScriptExecutor interface in Selenium. It has a method called executeScript which will perform our task.
If you are new in Java then check Java topics for Selenium which will help you to understand Selenium easily.
Program click in Selenium webdriver using JavaScript
// First identify element WebElement elem = driver.findElement(By.xpath(".//*[@id='__dialog1-footer']/button[1]/div")); // This will enable this element if element is invisible String js = "arguments[0].style.height='auto'; arguments[0].style.visibility='visible';"; // Execute the Java Script for the element which we find out ((JavascriptExecutor) driver).executeScript(js, elem); // Click on element elem.click();
This is one of the shortest posts but it is useful when you get stuck with some scenario. Let me know your thoughts on this in below comment section.
For More updates Learn Automation page
For any query join Selenium group- Selenium Group
RANJAN V says
Hi Mukesh,
Can u share the link for Different types of PopUps in Selenium?
Mukesh Otwani says
Hi Ranjan,
You can use this link https://the-internet.herokuapp.com/ for your practice purpose
Siva says
Javascriptexecutor click is not working
Mukesh Otwani says
Hi Siva,
Could you please mention what Exception/Error observed in the console?
madhu says
Hi Mukesh,
I am verifying button is enabled or disabled in UI. For that i am using enable method,but enable method is returning true when button is disabled on UI.
Mukesh Otwani says
Hi Madhu,
I’ll recommend you to use some attribute for button which gets change based on button behavior.
sushma says
You are right Mukesh sir, It is very helpful code to my application and we used it in our script as well.
Thanks a lot!
Mukesh Otwani says
Hi Sushma,
You’re welcome…:)
Mohammed Fareed says
how to highlight a particular field (for example if we have entered a wrong UN, p/w)? & also create this method & call it wherever we seem to have an exception or where we do negative testing.
Mukesh Otwani says
Hi Fareed below link will help http://learn-automation.com/highlight-element-selenium/