All about JavaScriptExecutor in Selenium

As per the selenium official link on Selenium RC, core selenium is basically a javascript program (set of functions) which interprets and executes selenium commands using browser’s built in javascript interpreter.

Sometimes we observe web controls doesn’t react well to selenium commands, there we can directly use / execute javascript commands using webdriver,

To execute javascript using selenium, we have JavascriptExecutor interface.

Note: Almost we can execute any javascript commands in selenium, but not the javaScript functions.

Refer below, for how to identify HTML elements by executing JavaScript in selenium
How to Identify an element (javaScript locators) 


How to handle browser element actionsLet’s see some more detail of how we can
Below code example shows:
How to create an alert and to get page title using selenium/javaScript


So in above program, we created a driver instance and casting to javascriptexecutor instance
(we can create an instance JavascriptExecutor interface, as FirefoxDriver implements JavascriptExecutor interface), then write commands / statments as parameters for the executescript method.

let’s see some more implementations on using javaScript in selenium:

  • Load URL on browser

  • To know if the page load done or not:

  • Highlight an element on browser:

  • How to get the current URL

  • Get the domain name

Content posted is based on learning or working experience,
Share the post with others if find useful,
Any queries?, leave comments or discuss on our, Thanks!

2 Responses

  1. January 6, 2017

    […] Now we know how to identify html elements using only selenium or execute javascript in selenium, […]

Leave a Reply

Your email address will not be published. Required fields are marked *