There are several situations where users run into problems concerning the inability to click an element through the selenium WebDriver “click” command but can easily execute it with a JavaScript click. An example of this scenario in both Python and WebDriver JavaScript is provided below:
In Python,
element = driver.find_element_by_id("myid") driver.execute_script("arguments[0].click();", element) While in WebDriver JavaScript, var elm = $("#myid"); browser.executeScript("arguments[0].click();", elm.getWebElement());
This issue has raised questions concerning the difference between the JavaScript click and the WebDriver click. Many are also eager to know why it is possible to Click through JavaScript when it seems not to work in WebDriver click and thus brings thoughts as to the possibility of a risk attached.
JavaScript click () vs WebDriver click ()
For the WebDriver, when the click works, the simulation of real user experience on a browser is attempted. For clarification, let’s assume an element A represents a button that displays ‘Click me’, and another element B is categorized as a div element that is characteristically transparent with its dimensions. After you instruct the WebDriver to click A, the instruction will be transmitted, and the click will be simulated, so that element B is the first recipient of the click. This happens because element B covers element A. That is, any user who tries to click on element A would have to first go through element B. Element A may receive the click, or not, depending on the way element B handles the instruction. As the case may be, the actions of the WebDriver at this time are identical to a situation where a real user attempts to click on element A.
In the case of JavaScript click () to Click element A, it does not duplicate precisely what happens when a real user attempts to click element A like WebDriver does. Instead, JavaScript transmits the click instructions directly to element A without any interference by element B.