Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in DevOps and Agile by (19.7k points)

The Story:

Here on StackOverflow, I've seen users reporting that they cannot click an element via selenium WebDriver "click" command and can work around it with a JavaScript click by executing a script.

Example in Python:

element = driver.find_element_by_id("myid");

driver.execute_script("arguments[0].click();", element);

Example in WebDriverJS/Protractor:

 var elm = $("#myid");

browser.executeScript("arguments[0].click();", elm.getWebElement());

The Question:

Why is clicking "via JavaScript" works when a regular WebDriver click does not? When exactly is this happening and what is the downside of this workaround (if any)?

. 

1 Answer

0 votes
by (62.9k points)

  • Webdriver utilizes a browser’s native support for mapping the DOM element to WebElement object using id/xpath etc.

  • The JavascriptExecutor.executeScript executes an external script in the context of the currently selected browser window. (similar to an augmented browsing tool like a grease monkey, if you ever used), and in case the script returns any DOM element it is converted into WebElement object.

  • One can also say, the click simulated by WebDriver on a browser is similar to what actual user does as compared to one invoked using javascript.

  • In reality, with WebDriver not, all the events can be automated perfectly with all the web browsers, in fact with different versions of the same Web browser also. (i.e. different version of IE, FF, etc behave differently). Nevertheless, WebDriver is the near best tool available for this.

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...