InterviewSolution
| 1. |
What is JavaScriptExecutor and in which cases JavaScriptExecutor will help in Selenium automation? |
|
Answer» In selenium mostly we click on an element using click() METHOD. Many times web controls don’t react against selenium commands and we face issues with the Click() statement. To overcome such a situation, in selenium we use the JavaScriptExecutor interface. JavaScriptExecutor executes Javascript using Selenium driver and provides “executescript” & “executeAsyncScript” methods, to run JavaScript in the context of the currently selected frame or window. To execute JavaScript within the browser using the Selenium WebDriver script, writing a separate script is not required. Just use a predefined interface NAMED ‘Java Script Executor’ and need to import the JavascriptExecutor package in the script. Package: import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.JavascriptExecutor;Syntax: JavascriptExecutor js = (JavascriptExecutor) driver; js.executeScript(Script,Arguments);where Script is the JavaScript to be executed. Arguments are the arguments to the script and are optional and can be empty. JavascriptExecutor returns either Boolean, or LONG, or String, or List, or WebElement, or null. Let us discuss some scenarios we could handle using JavascriptExecutor :
|
|