JavaScript Executor functions for Selenium

In this article we will learn about different action types we can perform using JavaScript Executor. Sometime we are not able to accomplish the task using traditional methods of Selenium, to handle those cases Javascript can come in handy with the solutions.

JavaScript Executor functions for Selenium

JavaScriptExecutor is an interface that helps to execute JavaScript through Selenium WebDriver.

It provides executeScript and executeAsyncScript methods to run Javascript on selected frames or windows.



If you are appearing for the written test in Nagarro, you might find a lot of questions on the syntax of Javascript Executor in your paper.

Click Operation using JavaScript Executor

JavaScriptExecutor js = (JavaScriptExecutor)driver;
js.executeScript(" arguments[0].click();", button);

Explanation - We have to typecast driver each time we use javascript executor.

Scroll into a particular element using JavaScript Executor

js.executeScript("arguments[0].scrollIntoView()", radio.get(0));

Scroll into the bottom of the web page using JavaScript Executor

js.executeScript("window.scrollTo(0, document.body.scrollHeight")


If you are not able to perform any action using the library of selenium, you might consider using JavaScriptExecutor and if it is also not working, I would recommend to use Actions class to perform click operation.

Comments

Popular posts from this blog

Azure Tutorials Series - Azure Networking

Coforge Interview Questions | Automation Testing profile

Testing in CI/CD