JavaScriptExecutor in selenium:

 

What is JavaScript and why do we use it in selenium?

  • Java Script is an object-oriented programming language commonly used to create interactive effects within the web browser to Handle WebElements within Web Page.
  • It is used by Selenium WebDriver to do handle WebElements.
  • We use Java in our test case and we apply almost all the features but in some features we can not use Java, so let us copy scripting the wanted.

 

What is JavaScriptExecutor?

  • The JavaScriptExecutor is an interface that provides mechanisms to execute javascript through the Selenium Driver. To run JavaScript in the context of the currently selected frame or window, it provides “executescript” and “executeAsyncScript” methods.
  • The basic advantage of JavaScriptExecutor is that Selenium provides a way to run Java Script in webdriver.
  • Sometimes the locator can not work, in that case the JavaScriptExecutor will help interact with web elements on a particular webpage.
  • The reason behind this is; Even Selenium webdriver convert the bindings of the language internally to the equivalent of javascript and inject them into the related browser.
  • Javascript extractor is also very useful for identifying hidden elements and interacting with them on the webpage.

 

Package to import:

 

Syntax:

 

Example  of JavaScriptExecutor:

 

// How to generate Alert Pop window in selenium ?

 

// How to get innertext of the entire webpage in Selenium? 

 

// How to navigate to different page using Javascript?

 

// How to enter value into textbox:

 

// How to click a button:

 

// How to refresh a window:

 

// How to get the text of a particular web element:

 

// How to get the title of the WebPage:

 

// How to scroll vertically for certain pixels:

 

// How to scroll till the bottom of the web page:

 

// How to scroll to a particular element:

 

// How to navigate back to page:

 

// How to navigate to forward page:

 

 Example of JavaScriptExecutor in Selenium: