Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

How To Get Current URL Using JavaScript In Selenium Java?

Posted on 02/19/2025 By admin

Selenium WebDriver with Java already provides a method named getCurrentUrl() to get the currently loaded URL in a browser and I don’t see any scenario in which this method has not worked as expected. But sometimes in interviews, some tricky questions are asked. “How to get current URL of browser without using getCurrentUrl() method?” is one of them.

Did you know that I have started a YouTube channel as well and I need your support to make it successful. Please do watch content then comment, like, share, and obviously subscribe.

We can use the JavaScript command here to get the current URL of a browser. Since JavaScript is enabled for all browsers nowadays to make web pages dynamic we can easily execute JavaScript commands.

You can run the JavaScript command in a browser. Perform the below steps:-

  1. Launch browser. Here I have launched the Chrome browser.
  2. Launch any URL. I launched Google URL.
  3. Open dev tool.
  4. Click on the “console” tab.
  5. In the console, type “document.URL” command and hit enter. You will give the current URL.

To run JavaScript command, Selenium WebDriver provides a method called “executeScript()” which is declared in JavascriptExecutor interface.

package com.mse.codes;

import org.openqa.selenium.chrome.ChromeDriver;

import io.github.bonigarcia.wdm.WebDriverManager;

public class GetCurrentURLUsingJavascript {
        
        public static void main(String[] args) {
                
                // Launch browser
                WebDriverManager.chromedriver().setup();
                ChromeDriver driver = new ChromeDriver();
                
                // Load URL
                driver.get("https://www.google.com/");
                
                // JavaScript command to get URL
                String cURL = (String) driver.executeScript("return document.URL");
                System.out.println("Current URL is : "+cURL);
                
                driver.quit();
        }

}

Current URL is : https://www.google.com/

Please subscribe to my YouTube channel Retarget Common to learn from my video tutorials.

Below are important end to end tutorials for Testers:-

Selenium Tutorials

Rest Assured Tutorials

Postman Tutorials

TestNG Tutorials

If you have any doubt, feel free to comment below.If you like my posts, please like, comment, share and subscribe.#ThanksForReading

#HappyLearning

Uncategorized

Post navigation

Previous Post: API Testing Tutorial Part 12 – Tools For Manual & Automation API Testing
Next Post: REST Assured Tutorial 73 – How to ignore node/s for JSON comparison in JSONassert

Related Posts

March 4, 2019 – Make Selenium Easy Uncategorized
ResultOfFirstTest – Make Selenium Easy Uncategorized
IFrame – Make Selenium Easy Uncategorized
Advanced Concept of Selenium – Design Patterns in Selenium WebDriver – There is Not Only Page Object Model Design Pattern Uncategorized
Frequently Asked Java Program 19: Java Program to Reverse Position of Words in a String Uncategorized
SelectCLass – Make Selenium Easy Uncategorized

Recent Posts

  • Getting Started with Selenium 4: What Is New and How to Upgrade from Selenium 3
  • Manual Testing
  • Baby Steps To Become Efficient Selenium-Java Automation Tester
  • Features of Selenium 4.0.0 Release – Java Binding
  • Part 1: Handling Drop-down Created Using SELECT Tag In Selenium

Recent Comments

No comments to show.

Archives

  • April 2026
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • August 2023
  • November 2022
  • September 2022
  • August 2022
  • July 2022
  • May 2022
  • March 2022
  • October 2021
  • April 2021
  • March 2021
  • January 2021
  • December 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • May 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • January 2018

Categories

  • Getting Started
  • Uncategorized

Copyright © 2026 Make Selenium Easy.

Powered by PressBook Masonry Dark