JavaScript

JavaScript can be executed in Selenium WebDriver using the JavascriptExecutor interface. This interface provides methods to execute JavaScript code within the context of the current browser window. This is particularly useful when interacting with elements or performing actions that cannot be easily achieved using the standard WebDriver methods.

Following are some common use cases for using JavaScript in Selenium:

Executing JavaScript Code

There can be a scenario where you need to make some changes on web page during runtime especially on element or data or run some script as prerequisite and this can be achieved by executing JavaScript code.

In the following example, we are enabling a radio button using JavaScript so that we can click on it.

Java
C#
Python
Javascript
Kotlin
Copy
WebDriver driver = new ChromeDriver();
driver.get("https://www.testautomationstudio.com/demo/set/");
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30));
driver.manage().window().maximize();
JavascriptExecutor je = (JavascriptExecutor) driver;
je.executeScript("document.getElementById('two').disabled = false;");
driver.findElement(By.id("two")).click();
driver.quit();
Copy
IWebDriver driver = new ChromeDriver();
driver.Url = "https://www.testautomationstudio.com/demo/set/";
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(30);
driver.Manage().Window.Maximize();
IJavaScriptExecutor je = (IJavaScriptExecutor)driver;
je.ExecuteScript("document.getElementById('two').disabled = false;");
driver.FindElement(By.Id("two")).Click();
driver.Quit();
Python code coming soon
Javascript code coming soon
Kotlin code coming soon

Clicking an Element

Sometimes it will be difficult to interact with the element using the standard selenium commands. In such situations, we can use JavaScript executer to interact with the element especially clicking on it.

In the following example, JavaScript Executor allows you to click an element using JavaScript code. The arguments[0] is a reference to the first element passed to the executeScript method, in this case, the element WebElement variable.

Java
C#
Python
Javascript
Kotlin
Copy
WebDriver driver = new ChromeDriver();
driver.get("https://www.testautomationstudio.com/demo/actions/");
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30));
driver.manage().window().maximize();
WebElement action2Elem = driver.findElement(By.id("action2"));
JavascriptExecutor je = (JavascriptExecutor) driver;
je.executeScript("arguments[0].click();", action2Elem);
driver.quit();
Copy
IWebDriver driver = new ChromeDriver();
driver.Url = "https://www.testautomationstudio.com/demo/actions/";
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(30);
driver.Manage().Window.Maximize();
IWebElement action2Elem = driver.FindElement(By.Id("action2"));
IJavaScriptExecutor je = (IJavaScriptExecutor)driver;
je.ExecuteScript("arguments[0].click();", action2Elem);
driver.Quit();
Python code coming soon
Javascript code coming soon
Kotlin code coming soon

Scrolling

JavaScript code can be used to scroll to view elements on screen which are at the bottom of the page. In the following example we are scrolling to view the last element in the page.

Java
C#
Python
Javascript
Kotlin
Copy
WebDriver driver = new ChromeDriver();
driver.get("https://www.testautomationstudio.com/demo/get/");
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30));
driver.manage().window().maximize();
WebElement newsLetterElem = driver.findElement(By.name("newsLetter"));
JavascriptExecutor je = (JavascriptExecutor) driver;
je.executeScript("arguments[0].scrollIntoView();", newsLetterElem);
driver.quit();
Copy
IWebDriver driver = new ChromeDriver();
driver.Url = "https://www.testautomationstudio.com/demo/get/";
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(30);
driver.Manage().Window.Maximize();
IWebElement newsLetterElem = driver.FindElement(By.Name("newsLetter"));
IJavaScriptExecutor je = (IJavaScriptExecutor)driver;
je.ExecuteScript("arguments[0].scrollIntoView();", newsLetterElem);
driver.Quit();
Python code coming soon
Javascript code coming soon
Kotlin code coming soon

Getting Value or Inner Text

You can use retrieve or return the value of a text box or an innerText of an element by using JavaScript executor. In the following example value of a textbox element is returned using javascriptexecutor. The arguments[0] is a reference to the first element passed to the executeScript method, in this case, the element WebElement variable.

Java
C#
Python
Javascript
Kotlin
Copy
WebDriver driver = new ChromeDriver();
driver.get("https://www.testautomationstudio.com/demo/validations/");
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30));
driver.manage().window().maximize();
WebElement todaysDateElem = driver.findElement(By.id("todaysDate2"));
JavascriptExecutor je = (JavascriptExecutor) driver;
String todaysDate = (String) je.executeScript("return arguments[0].value;", todaysDateElem);
System.out.println(todaysDate);
driver.quit();
Copy
IWebDriver driver = new ChromeDriver();
driver.Url = "https://www.testautomationstudio.com/demo/validations/";
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(30);
driver.Manage().Window.Maximize();
IWebElement todaysDateElem = driver.FindElement(By.Id("todaysDate2"));
IJavaScriptExecutor je = (IJavaScriptExecutor)driver;
String todaysDate = (String)je.ExecuteScript("return arguments[0].value;", todaysDateElem);
Console.WriteLine(todaysDate);
driver.Quit();
Python code coming soon
Javascript code coming soon
Kotlin code coming soon