• Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • Home
  • Programming Languages
    • Java Tutorials
    • Python Tutorials
    • JavaScript Tutorials
  • Automation Tools and Different Tools
    • Web Automation
      • Selenium with Java
        • Selenium Basic
        • Selenium Advance
        • Selenium Realtime
        • Framework
        • Selenium Interview
        • Selenium Videos
        • Selenium with Docker
      • Selenium with Python
      • WebdriverIO
        • Selenium Webdriver C# Tutorial
      • Cypress
      • Playwright
    • TestNG
    • Cucumber
    • Mobile Automation
      • Appium
    • API Testing
      • Postman
      • Rest Assured
      • SOAPUI
    • testRigor
    • Katalon
    • TestProject
    • Serenity BDD
    • Gradle- Build Tool
    • RPA-UiPath
    • Protractor
    • Windows Automation
  • Automation For Manual Testers
  • Services
  • Online Training
  • Contact us
  • About me
  • Follow us
    • Linkedin
    • Facebook Group
    • Facebook Page
    • Instagram

Automation

Selenium WebDriver tutorial Step by Step

You are here: Home / Basic Selenium / How to Click in Selenium webdriver using JavaScript

How to Click in Selenium webdriver using JavaScript

September 28, 2016 by Mukesh Otwani 10 Comments

Click in Selenium webdriver using JavaScript

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.

Click in Selenium webdriver using JavaScript

 

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.

  1. First, we will identify what exception is coming from the script.
  2. If an exception is related to disable or visibility then we can create the object of JavaScriptExecutor.
  3. 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

Filed Under: Basic Selenium Tagged With: Click using JS

Reader Interactions

Comments

  1. RANJAN V says

    July 9, 2021 at 4:20 PM

    Hi Mukesh,

    Can u share the link for Different types of PopUps in Selenium?

    Reply
    • Mukesh Otwani says

      July 10, 2021 at 5:53 PM

      Hi Ranjan,

      You can use this link https://the-internet.herokuapp.com/ for your practice purpose

      Reply
  2. Siva says

    March 30, 2020 at 1:02 PM

    Javascriptexecutor click is not working

    Reply
    • Mukesh Otwani says

      March 30, 2020 at 10:41 PM

      Hi Siva,

      Could you please mention what Exception/Error observed in the console?

      Reply
  3. madhu says

    July 31, 2019 at 10:56 AM

    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.

    Reply
    • Mukesh Otwani says

      July 31, 2019 at 4:25 PM

      Hi Madhu,

      I’ll recommend you to use some attribute for button which gets change based on button behavior.

      Reply
  4. sushma says

    March 25, 2019 at 2:54 PM

    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!

    Reply
    • Mukesh Otwani says

      March 25, 2019 at 10:28 PM

      Hi Sushma,

      You’re welcome…:)

      Reply
  5. Mohammed Fareed says

    October 11, 2016 at 7:01 PM

    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.

    Reply
    • Mukesh Otwani says

      October 12, 2016 at 11:40 AM

      Hi Fareed below link will help http://learn-automation.com/highlight-element-selenium/

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Free Selenium Videos

https://www.youtube.com/watch?v=w_iPCT1ETO4

Search topic

Top Posts & Pages

  • Selenium Webdriver tutorial for beginners
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Selenium Webdriver C# Tutorial
  • WHAT ARE YOUR EXPECTATIONS FROM US?

Stay connected via Facebook

Stay connected via Facebook

Archives

Footer

Categories

Recent Post

  • API Testing Using Postman And RestAssured
  • Disable Personalise Your Web Experience Microsoft Edge Prompt In Selenium
  • How To Fix Error: No tests found In Playwright
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Best and easy way to Group test cases in selenium

Top Posts & Pages

  • Selenium Webdriver tutorial for beginners
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Selenium Webdriver C# Tutorial
  • WHAT ARE YOUR EXPECTATIONS FROM US?