Selenium Interview Question 8 – What is Difference between WebDriver click and JavaScript click methods

In this post we will see another frequently asked interview question:-

Difference between WebDriver click() and JavaScript click() method in Selenium.

We can perform click on a web element of a web page in two ways:-

  1. Using WebDriver
  2. Using JavaScript click((JavascriptExecutor)driver).executeScript(“arguments[0].click()”, element); ( Note- driver is a WebDriver instance.)

So question must be arised in your mind, which is better and when to use.

Since Selenium 3, maximum browser are providing their respective browser driver which implements WebDriver API. Selenium communicates to these browser drivers through HTTP commands and these drivers communicates natively with browser. Official documentation of Selenium says:-

Selenium-WebDriver makes direct calls to the browser using each browser’s native support for automation.

So we can say that the way chrome browser allows a click on a web element may be different from other browsers.

So when we click on a webelement using Selenium WebDriver , it checks two preconditions before clicking:-

  1. The element must be visible.
  2. It must have a height and width greater than 0.

If preconditions are not satisfied, you will get exceptions stating element is not clickable or interactable. Refer click() method in official document here.

But in JavaScript clicks does not check these preconditions before clicking. The method simulates a mouse click on an element. When click() is used with supported elements (such as an <input>), it fires the element’s click event. Reference here.

So, JavaScript could click on a web element which may not be clickable by WebDriver API. So use WebDriver click as much as possible and go for JavaScript click when WebDriver click does not work.

WebDriver APIs are designed to simulate the actions which a real user will perform on front end of application. When we go for JavaScript, this is not taking into consideration. JavaScript ways may not catch the bugs which is main goal of automation scripting.

JavaScript operations may not call actions sometimes. Suppose when you click on a button, some popup should appear. That can be relate as onClick event. You may encounter these issues. These issues will not come if you use WebDriver APIs.

I would like to share a problem I faced while automating. There was a Date of birth field which takes input from user and calculates actual age. I typed DOB using Javascript way and it was not calculating actual age and event set on DOB field was not called.

If you like above post and feel it is worth to share to reach it to maximum people, please do.


2 thoughts on “Selenium Interview Question 8 – What is Difference between WebDriver click and JavaScript click methods

  1. If we are having 1000 test cases, what type of testing carried
    for automation testing? Can we write a method that returns
    two or more values? If then, how?
    How many test cases in your regression testsuite? How much
    time it will take to execute?

    There are 250 manual test cases , how will you segregate (on
    what basis) the regression , sanity , smoke suite?

    You have 30 + sprints in your release how will you design your
    test scripts and run them?

    If your testsuite takes 1 and half to run, what you will do to
    reduce the time?

Leave a Reply

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