In this post we will see another frequently asked interview question:-
We can perform click on a web element of a web page in two ways:-
- Using WebDriver click – element.click()
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:-
- The element must be visible.
- 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.
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.
If you like above post and feel it is worth to share to reach it to maximum people, please do.