LambdaTest Community

Find answers, support, and inspiration from other users

Questions
LambdaTest Community
Answered

How can I execute JavaScript in selenium?

Lately i have been trying to execute JavaScript in selenium but dont know how to do it properly, can anyone please tell me how to do it.

Up Vote Down Vote 0 Votes
Posted 4 months ago

Answers


By using predefined interface named “Java script executor”, we can execute javascript in selenium. Additionally we need to follow below procedure.

Step 1 : Import the package

import org.openqa.selenium.JavascriptExecutor;

Step 2 : Follow the below syntax

JavascriptExecutor js = (JavascriptExecutor) driver;

js.executeScript(Script,Arguments);

Explanation of Syntax :

Script – The JavaScript to execute

Arguments –The arguments to the script(Optional). May be empty.

Returns –One of Boolean, Long, String, List, WebElement, or null.

Up Vote Down Vote 0 Votes
Posted 3 months ago