10. JavaScript Executer(1)
10. JavaScript Executer(1)
JavaScript is the programming language of HTML and the Web. JavaScript used to program the behavior of web
page.
JavaScriptExecutor is an interface of Selenium. It is used to change appearance of HTML coded webpage on a
client end. Usually we design webpages using JavaScript. JavaScript written in console of a Browser. JavaScript is a
programming language which is used –
to handle elements on entire document object model (HTML document)
Click on Element
to enter the text in TextBox
to select values from dropdown menu
to handle alert
to handle window (scrolling, Minimising, Maximizing, Closing ..)
There are two classes in JavaScript which are required to automate webpages.
1) Document Class –
This class refers to entire HTML DOM. We can perform different operations like click, enter text, select
from dropdown, change visibility of webElement, change colour of webElement etc. using this class.
JavaScriptExecuter --
JavaScriptExecutor is an interface that helps us to execute Java Script through Selenium WebDriver.
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. You can use JavaScriptExecutor to perform a desired
operation on a web element. Selenium supports javaScriptExecutor. There is no need for an extra plugin or add-on. You
just need to import org.openqa.selenium.JavascriptExecutor in the script as to use JavaScriptExecutor.
JavaScriptExecutor has following two methods to run JavaScript on current window or on selected window.
1. ExecutesAsynchScript(Script, Argument) –
With Asynchronous script, your page renders more quickly. Instead of forcing users to wait for a script to
download before the page renders. This function will execute an asynchronous piece of JavaScript in the context
of the currently selected frame or window in Selenium. The JavaScript so executed is single-threaded with a various
callback function which runs synchronously.
2. ExecuteScript(Script, Argument) –
This method executes JavaScript in the context of the currently selected frame or window in Selenium. The
script used in this method runs in the body of an anonymous function (a function without a name). We can also
pass complicated arguments to it.
Boolean
Long
String
List
WebElement
NOTE : To use JavaScript Executer we need to type caste driver instance into JavaScript executer.
System.setProperty("webdriver.chrome.driver","F:\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.facbook.com");
System.setProperty("webdriver.chrome.driver","F:\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.facbook.com");
JavascriptExecutor js = (JavascriptExecutor) driver ;
js.executeScript("window.scrollBy(0,100)"); // scrolling down by 100
Thread.sleep(1000);
js.executeScript("window.scrollBy(0,100)"); // scrolling down by 100
Thread.sleep(1000);
js.executeScript("window.scrollBy(0,-200)"); // scrolling up by -200
}}