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 clickelement.click()
  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 HTMLElement.click() 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.

#ThanksForReading

Author: Amod Mahajan

My name is Amod Mahajan and I am an IT employee with 6+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went through so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning

Leave a Reply

Please wait...

Subscribe to new posts to become automation expert

Want to be notified when my new post is published? Get my posts in your inbox.
%d bloggers like this: