• Selenium Video Tutorials

Selenium with Javascript Tutorial



Selenium Webdriver can be used to execute JavaScript commands to interact with the html of the elements appearing within a browser on a page with the help of the JavaScriptExecutor interface. Sometimes, the locators available in Selenium Webdriver fail to interact with the elements on a page, in those scenarios we can take the help of the JavaScriptExecutor methods.

Methods to Execute JavaScript Commands

Some of the methods available in JavaScriptExecutor interface are listed below −

  • executeScript() method − This method is used to execute JavaScript in a window or frame currently active. It is an anonymous function, supports various data types like WebElements, Long, String, Lists, and Boolean.

  • executeAsyncScript() method − It is a multi-threaded approach to execute individual JavaScript tasks on window or frame in currently active. It allows page parsing to continue, optimizing performance and providing great flexibility. This method enables the asynchronous JavaScript to be executed in an optimal way.

Steps to Run JavaScript Commands

The steps to be performed inorder to run JavaScript commands along with Selenium Webdriver using the JavaScriptExecutor interface are listed below −

  • Step 1 − Package import for JavaScriptExecutor.

  • Step 2 − Creating a reference variable JavaScriptExecutor interface.

  • Step 3 − Calling the JavaScriptExecutor methods.

Syntax

import org.openqa.selenium.JavascriptExecutor;
JavascriptExecutor j = (JavascriptExecutor) driver;
j.executeScript(script, args);

Example

Let us take an example of the below page, where we would launch an application with a URL: Selenium. Then, get the text: Text Box, and domain as www.tutorialspoint.com. Next we would enter the text Selenium in the input box beside the Full Name: label.

Selenium Javascript Tutorial 1

Then, we would click on the Checkbox button, after which we would be navigated to another page having a text as Check Box. Finally, we would refresh the browser and again obtain the text, URL and domain as Check Box, https://www.tutorialspoint.com/selenium/, and www.tutorialspoint.com respectively.

Selenium Javascript Tutorial 2

Syntax with JavaScriptExecutor −

// Initiate the Webdriver
WebDriver driver = new ChromeDriver();       

// Creating a reference variable JavaScriptExecutor interface.
JavascriptExecutor j = (JavascriptExecutor) driver;

// launching a URL
j.executeScript("window.location= 'https://www.tutorialspoint.com/selenium/practice/text-box.php'");


// getting current URL
String url =  j.executeScript("return document.URL;").toString();
System.out.println("Getting the current URL: " + url);

//identify text
WebElement t = driver.findElement(By.xpath("<value of xpath>"));

// get text
String text = (String) j.executeScript("return arguments[0].innerText", t);
System.out.println("Text is: " + text);

// getting current domain
String domain = j.executeScript("return document.domain;").toString();
System.out.println("Getting the current domain: " + domain);

// enter text in input box
WebElement e = driver.findElement(By.xpath("//*[@id='name']"));
j.executeScript("arguments[0].value='Selenium';", e);

// get text entered
String text1 = (String) j.executeScript("return arguments[0].value", e);
System.out.println("Entered text is: " + text1);

// perform click
WebElement b = driver.findElement(By.xpath("<value of xpath>"));
j.executeScript("arguments[0].click();", b);

// identify text
WebElement w = driver.findElement(By.xpath("<value of xpath>"));

// get text after click
String text2 = (String) j.executeScript("return arguments[0].innerText", w);
System.out.println("Text found after clicking: " + text2);

// refresh browser
j.executeScript("history.go(0)");

Code Implementation in JavaScriptsExeS.java

package org.example;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;

public class JavaScriptsExeS {
   public static void main(String[] args) throws InterruptedException {

      // Initiate the Webdriver
      WebDriver driver = new ChromeDriver();

      // adding implicit wait of 25 secs
      driver.manage().timeouts().implicitlyWait(25, TimeUnit.SECONDS);

      // Creating a reference variable JavaScriptExecutor interface.
      JavascriptExecutor j = (JavascriptExecutor) driver;

      // launching a URL
      j.executeScript("window.location= 'https://www.tutorialspoint.com/selenium/practice/text-box.php'");

      // getting current URL
      String url =  j.executeScript("return document.URL;").toString();
      System.out.println("Getting the current URL: " + url);

      //identify text
      WebElement t = driver.findElement(By.xpath("//*[@id='TextForm']/h1"));

      // get text
      String text = (String) j.executeScript("return arguments[0].innerText", t);
      System.out.println("Text is: " + text);

      // getting current domain
      String domain = j.executeScript("return document.domain;").toString();
      System.out.println("Getting the current domain: " + domain);

      // enter text in input box
      WebElement e = driver.findElement(By.xpath("//*[@id='fullname']"));
      j.executeScript("arguments[0].value='Selenium';", e);

      // get text entered
      String text1 = (String) j.executeScript("return arguments[0].value", e);
      System.out.println("Entered text is: " + text1);

      // perform click
      WebElement b = driver.findElement(By.xpath("//*[@id='navMenus']/li[2]/a"));
      j.executeScript("arguments[0].click();", b);

      //identify text
      WebElement w = driver.findElement(By.xpath("/html/body/main/div/div/div[2]/h1"));

      // get text after click
      String text2 = (String) j.executeScript("return arguments[0].innerText", w);
      System.out.println("Text found after clicking: " + text2);

      // refresh browser
      j.executeScript("history.go(0)");

      // getting current URL after browser refresh
      String url1 =  j.executeScript("return document.URL;").toString();
      System.out.println("Getting the current URL after browser refresh: " + url1);

      //identify text again after refresh
      WebElement y = driver.findElement(By.xpath("/html/body/main/div/div/div[2]/h1"));

      // get text after refresh
      String text3 = (String) j.executeScript("return arguments[0].innerText", y);
      System.out.println("Text found after refresh: " + text3);

      // getting current domain after browser refresh
      String domain1 = j.executeScript("return document.domain;").toString();
      System.out.println("Getting the current domain after browser refresh: " + domain1);

      // Quitting browser
      driver.quit();
   }
}

It will show the following output

Getting the current URL:
 https://www.tutorialspoint.com/selenium/practice/text-box.php
Text is: Text Box
Getting the current domain: www.tutorialspoint.com
Entered text is: Selenium
Text found after clicking: Check Box
Getting the current URL after browser refresh:
 https://www.tutorialspoint.com/selenium/practice/check-box.php
Text found after refresh: Check Box
Getting the current domain after browser refresh: www.tutorialspoint.com

Process finished with exit code 0

In the above example, we had launched a URL and obtained the current URL with the message in the console - Getting the current URL: https://www.tutorialspoint.com/selenium/. Then retrieved the text and domain with the message in the console - Text is: Text Box and Getting the current domain: www.tutorialspoint.com respectively.

Next, we entered the text Selenium in an input box and retrieved its value with the message in the console - Entered text is: Selenium. Then, we clicked the Login link and got the text after navigation with the message in console: Text found after clicking: Check Box.

Finally, we had refreshed the page and obtained current URL, text and domain of the page with the messages in the console - Getting the current URL after browser refresh: https://www.tutorialspoint.com/selenium/, Text found after refresh: Check Box, and Getting the current domain after browser refresh: www.tutorialspoint.com respectively.

Finally, the message Process finished with exit code 0 was received, signifying successful execution of the code

Advertisements