Styrow.dev
Question 56 of 64

What is JavaScriptExecutor and its use in selenium WebDriver?

Question

What is JavaScriptExecutor and its use in selenium WebDriver?

Answer

JavaScriptExecutor is an Interface that helps to execute JavaScript through Selenium Webdriver. JavaScriptExecutor provides two methods ""executescript"" & ""executeAsyncScript"" to run javascript on the selected window or current page

In Selenium Webdriver, locators like XPath, CSS, etc. are used to identify and perform operations on a web page. In case, these locators do not work you can use JavaScriptExecutor.

Syntax:

JavascriptExecutor js = (JavascriptExecutor) driver;

js.executeScript(Script,Arguments);

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.