Reading JavaScript variables using Selenium WebDriver Last Updated : 12 Sep, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report In web automation testing, interacting with JavaScript variables on a webpage can be essential for validating specific conditions. Selenium WebDriver provides a way to execute JavaScript directly on the browser using the `executeScript()` method. This feature allows us to read JavaScript variables and perform various actions based on their values. In this article, we will explore how to read JavaScript variables using Selenium WebDriver.PrerequisitesJava Development Kit (JDK) installed on your system.Eclipse IDE or any Java-compatible IDE.Chrome Browser.Basic knowledge of Java and Selenium WebDriver.Selenium WebDriver and Browser Driver installed.How to read JavaScript variables using Selenium WebDriverStep 1. Set up the Selenium WebDriverThe first step is to set up the WebDriver and navigate to the target webpage. Java // Import necessary classes import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; public class ReadJavaScriptVariables { public static void main(String[] args) { // Set the path to the ChromeDriver System.setProperty("webdriver.chrome.driver", "path/to/chromedriver"); // Initialize WebDriver WebDriver driver = new ChromeDriver(); // Navigate to the web page driver.get("https://www.geeksforgeeks.org"); } } Step 2. Use JavascriptExecutor to Retrieve JavaScript Variable Java import org.openqa.selenium.JavascriptExecutor; // Use JavascriptExecutor to retrieve the JavaScript variable value JavascriptExecutor js = (JavascriptExecutor) driver; // Execute JavaScript to retrieve the variable String jsVariable = (String) js.executeScript("return document.title;"); Step 3. Capture and Display the ValueWe have to capture and display it in the console: Java // Print the value of the JavaScript variable System.out.println("JavaScript variable value: " + jsVariable); Step 4. Close Browser Java driver.quit(); Complete Code Java import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; public class ReadJavaScriptVariables { public static void main(String[] args) { // Step 1: Set the path to the ChromeDriver System.setProperty("webdriver.chrome.driver", "path/to/chromedriver"); // Initialize WebDriver WebDriver driver = new ChromeDriver(); // Navigate to the web page driver.get("https://geeksforgeeks.org"); // Step 2: Use JavascriptExecutor to retrieve the JavaScript variable value JavascriptExecutor js = (JavascriptExecutor) driver; String jsVariable = (String) js.executeScript("return window.myJavaScriptVariable;"); // Step 3: Print the value of the JavaScript variable System.out.println("JavaScript variable value: " + jsVariable); // Step 4: Close the browser driver.quit(); } } NOTE: Make changes to the path to chrome driver.Output:Console outputConclusionReading JavaScript variables in Selenium WebDriver can be extremely useful for interacting with dynamic data that is managed by JavaScript on the front end. By using the `JavascriptExecutor` interface, we can execute any JavaScript code and retrieve the necessary values into your WebDriver scripts. JavaScript Executor Visit Course Comment More infoAdvertise with us Next Article Find Web Elements using Selenium WebDriver R rd9437 Follow Improve Article Tags : Software Testing TestNG Similar Reads Find Web Elements using Selenium WebDriver We can identify web elements in the web page using the following two tools: Developer Tools FireBug and FirePath Developer Tools - Right-click on the web page. Navigate to inspect element to find the developer's tool. Note: There are some websites where the right-click is disabled. eg. IRCTC, bankin 4 min read Selenium WebDriver Handling Radio Buttons Using Java A platform-independent language that is used to build various applications is known as Java. Java can also be used to automate the web drivers. There are various automation tools available, out of which Selenium is the most common one. We can automate the opening of the website, clicking of push but 4 min read How to Select Value from DropDown using Java Selenium Webdriver? In web Automation testing, the common task is Selecting the Dropdowns. Selenium Webdriver mainly used for Interacting to the Web-Elements. Selecting the Web-Elements from the dropdowns is a needed for the interaction to the browser.In these guide we will going through the process of selecting the dr 2 min read Selenium WebDriver-Handling Alerts Selenium is one of the most popular and powerful tools for automated web applications. Selenium is widely used for automating user interactions like filling out forms, navigating to a website clicking on a button, etc. While we are dealing with automating user interactions one of the most common sce 5 min read Selenium Webdriver Handling Checkbox using Java A set of tools and libraries that allow the user to automate interactions with web browsers is known as Selenium. It is too diverse tool that can handle every operation of a webpage starting from opening the webpage to closing of webpage. In this article, we will see the various steps to handle the 6 min read How can I verify Error Message on a webpage using Selenium Webdriver? Selenium WebDriver is an essential tool for automating web application testing, allowing testers to ensure that applications function correctly and efficiently. One critical aspect of this testing is verifying error messages, which are crucial for user feedback, especially when invalid inputs are pr 4 min read Like