Selenium Java - Navigate to URL using JavaScriptExecutor

Pankaj KumarPankaj Kumar
2 min read

Selenium Java - Navigate to URL using JavaScriptExecutor

Introduction

In this blog, we'll dive into how to use JavaScriptExecutor in Selenium WebDriver with Java to navigate to a new URL. This is especially handy when you want precise JavaScript control over browser navigation or when you're testing actions in web apps that rely on JavaScript.

Navigating to a URL using JavaScriptExecutor

Below is a complete Selenium code example demonstrating how to use JavaScriptExecutor to navigate to a new page.

java

package sa.Demo.SeleniumPractice;

import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;

import java.time.Duration;

public class Selenium_075 {
    /****
     *  Sample Code Definition :- Selenium - JavaScript NavigateToURL
     *  Developed  By :- Pankaj Kumar
     *  Date of Creation :- 15-June-2024
     *  Project :- SAJVSAUTO - Java [Web / Mobile / API]
     *  SA_BLOG_CODE :- SAJVSAUTO_075
     */
    @Test
    public void Selenium_075() throws InterruptedException {
        System.out.println("Selenium - JavaScript NavigateToURL");

        // Open Chrome Browser
        WebDriver driver=new ChromeDriver();
        // Maximize the Browser Window
        driver.manage().window().maximize();
        // Setup the implicit Wait
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30));
        // Open the URL
        driver.get("https://practice.solveautomation.in");
        // Wait for 5 Seconds
        Thread.sleep(5000);
        // Click on Element By using JavaScript
        JavascriptExecutor js = (JavascriptExecutor)driver;
        // Navigate to new Page i.e to generate access page (launch new URL)
        js.executeScript("window.location = 'https://www.amazon.in/'");
        // Wait for 5 Seconds to observe the action (optional)
        Thread.sleep(5000);
        // Quit the Driver
        driver.quit();
    }
}

Explanation

  • Browser Setup: Sets up ChromeDriver with maximized window and implicit wait.

  • Initial Page Load: Opens the first URL using driver.get().

  • JavaScriptExecutor: Utilizes JavaScript to navigate to a new URL.

    • The script window.location = 'URL' is executed to load the specified page.
  • Thread.sleep(): Introduced between actions to observe the transitions (optional for testing clarity).

  • Driver Quit: Ensures proper cleanup by closing the browser session at the end.

Conclusion

Using JavaScriptExecutor to navigate URLs is a handy trick for simulating direct JavaScript interactions with the browser. This method can help automate JavaScript-based workflows, making your automated scripts more thorough and effective.

0
Subscribe to my newsletter

Read articles from Pankaj Kumar directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Pankaj Kumar
Pankaj Kumar