Selenium C# – JavaScript NavigateToURL

Pankaj KumarPankaj Kumar
2 min read

Selenium C# – JavaScript NavigateToURL

Introduction

JavaScriptExecutor in Selenium is a handy tool that gives you extra control over how you interact with web pages. One cool thing you can do is navigate to a new URL directly using JavaScript. This method is a great alternative to the usual navigation methods, especially when you're dealing with complex test scenarios. In this guide, we'll walk you through how to navigate to a URL using JavaScriptExecutor in Selenium with C#.

Code Implementation

Below is a C# Selenium script that opens a browser, navigates to a demo website, and then navigates to another website (Amazon) using JavaScriptExecutor.

Example: JavaScript NavigateToURL with Selenium in C#.

using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Interactions;
using OpenQA.Selenium.Support.UI;
using SeleniumExtras.WaitHelpers;
using System.Globalization;

namespace SACSSAuto.QA.SeleniumPractice
{
    [TestClass]
    public class Selenium_75
    {
        /****
         *  Sample Code Definition :-  Selenium - JavaScript NavigateToURL
         *  Developed  By :- Pankaj Kumar
         *  Date of Creation :- 31-Jan-2025
         *  Project :- SACSSAUTO - C# [Web / Mobile / API]     *  
         */
        [TestMethod]
        public void Selenium_Test()
        {
            Console.WriteLine("Selenium - JavaScript NavigateToURL");
            // Open Chrome Browser
            IWebDriver driver = new ChromeDriver();
            // Maximize the Browser Window
            driver.Manage().Window.Maximize();
            // Open the URL
            driver.Navigate().GoToUrl("https://practice.solveautomation.in");
            // Wait for 5 seconds
            Thread.Sleep(5000);
            // Click on Element By using JavaScript
            IJavaScriptExecutor js = (IJavaScriptExecutor)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
            Thread.Sleep(5000);         
            // Quit the driver
            driver.Quit();
        }
    }
}

Steps Explained

1. Opening Initial URL

The browser opens the specified initial URL and waits for it to load.

2. Navigating via JavaScript

JavaScriptExecutor is used to navigate to a different URL by executing a script.

3. Wait and Exit

After navigating to the new URL, the script waits briefly before closing the browser.

Expected Outcome

  • Opens the practice website.

  • Navigates to Amazon using JavaScript.

  • Closes the browser session.

Conclusion

JavaScriptExecutor is a handy tool for web automation. In this example, we used it to jump straight to a new URL, which is great for skipping tricky navigation steps or adding custom logic during testing.

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