Getting the return value of Javascript code in Selenium.

We can get the return value of Javascript code with Selenium WebDriver using the executeScript method. This method executes JavaScript commands in the browser and can return values back to our Java code.

To work with JavaScript in Selenium, we need to cast our WebDriver instance to JavascriptExecutor and import the necessary package. The JavaScript command is passed as a string argument to the executeScript method.

Syntax

JavascriptExecutor js = (JavascriptExecutor) driver;
Object result = js.executeScript("return document.getElementById('elementId').value");

Key Points

When working with JavaScript return values in Selenium:

  • Use the return keyword in your JavaScript code to send values back
  • Cast the WebDriver to JavascriptExecutor interface
  • The executeScript method returns an Object that needs to be cast to the appropriate type
  • Import org.openqa.selenium.JavascriptExecutor in your Java class

Example: Getting Input Field Value

Let's create an example that enters text into a search field and then retrieves that value using JavaScript:

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

public class JavascriptValue {
    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        String url = "https://www.tutorialspoint.com/tutor_connect/index.php";
        driver.get(url);
        driver.manage().timeouts().implicitlyWait(12, TimeUnit.SECONDS);
        
        // Identify element and enter text
        WebElement searchBox = driver.findElement(By.id("txtSearchText"));
        searchBox.sendKeys("Selenium WebDriver");
        
        // JavaScript executor to return value
        JavascriptExecutor js = (JavascriptExecutor) driver;
        String retrievedValue = (String) js.executeScript(
            "return document.getElementById('txtSearchText').value"
        );
        
        System.out.println("Value retrieved: " + retrievedValue);
        driver.quit();
    }
}

Output

Value retrieved: Selenium WebDriver

Common JavaScript Return Examples

Here are some useful JavaScript snippets you can execute with Selenium:

// Get page title
String title = (String) js.executeScript("return document.title");

// Get current URL
String url = (String) js.executeScript("return window.location.href");

// Get element text content
String text = (String) js.executeScript("return document.getElementById('myId').textContent");

// Check if element is visible
Boolean isVisible = (Boolean) js.executeScript("return document.getElementById('myId').offsetHeight > 0");

// Get page scroll position
Long scrollY = (Long) js.executeScript("return window.pageYOffset");

Return Value Types

JavaScript Type Java Type Example
String String Element text, URLs, titles
Number Long Scroll positions, element counts
Boolean Boolean Visibility checks, comparisons
null/undefined null Missing elements

Conclusion

Using executeScript with return values allows you to extract data from web pages using JavaScript. Always cast the WebDriver to JavascriptExecutor and handle the returned Object appropriately based on the expected data type.

Updated on: 2026-03-15T23:19:00+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements