Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

How to load a URL in browser without using get() or navigate() method in Selenium

Posted on 03/16/2025 By admin

Hello Folks,

In last post, we have seen in details how to open a URL in selenium webdriver.

Do you know that we can open a URL without using any browser as well?  It is frequently asked interview question as well. Let’s learn it.

Let’s perform some steps first:

  1. Open a browser.
  2. Press F12.
  3. Switch to Console tab.
  4. Type ” window.location=’https://www.redbus.in’ “(exclude double quotes) and hit Enter key.
  5. You will notice that redbus website is loaded.

This is the way of loading URL without using any methods like get() or navigate(). Above statement is called as javaScript command. We will see javaScript concepts later. In this post we will just learn how to execute a javaScript command through selenium.

Complete JAVA program:


package MakeSeleniumEasy;

import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class LoadURLWithoutGetNavigate {
public static void main(String[] args) {

System.out.println("Execution Starts");
// Setting chrome driver property and opening chrome browser
System.setProperty("webdriver.chrome.driver", "./exefiles/chromedriver.exe");
WebDriver driver= new ChromeDriver();
System.out.println("Browser opened.");
// We need to downcast WebDriver reference varaible to use JavascriptExecutor methods
JavascriptExecutor js= (JavascriptExecutor) driver;
String url = "https://www.google.com";
String script = "window.location = \'"+url+"\'";
js.executeScript(script);
System.out.println(driver.getCurrentUrl());
driver.quit();
}
}

 

Notes:

  1. It also holds the execution of next statement till website is loaded completely.
  2. It also throws page load time exception after 300 seconds.
  3. It also keeps current browser session history and you can navigate back and forward.

Java Program:


package MakeSeleniumEasy;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class LoadURLWithoutGetNavigate {
public static void main(String[] args) {

System.out.println("Execution Starts");
// Setting chrome driver property and opening chrome browser
System.setProperty("webdriver.chrome.driver", "./exefiles/chromedriver.exe");
WebDriver driver= new ChromeDriver();
System.out.println("Browser opened.");
// We need to downcast WebDriver reference varaible to use JavascriptExecutor methods
JavascriptExecutor js= (JavascriptExecutor) driver;
//String url = "http://flipkart.com";
String script1 = "window.location = \'http://flipkart.com'";
js.executeScript(script1);
//WebElement firstName= driver.findElement(By.xpath("//input[@title='Search for Products, Brands and More']"));
System.out.println(driver.getCurrentUrl());
String script2 = "window.location = \'http://facebook.com'";
js.executeScript(script2);
System.out.println(driver.getCurrentUrl());
driver.navigate().back();
System.out.println(driver.getCurrentUrl());
driver.navigate().forward();
System.out.println(driver.getCurrentUrl());

driver.quit();
}
}

Output: Execution Starts Browser opened. https://www.flipkart.com/ https://www.facebook.com/ https://www.flipkart.com/

https://www.facebook.com/

Uncategorized

Post navigation

Previous Post: selenium interview question
Next Post: JsonIgnoreProperties

Related Posts

TestNG Tutorials 21: Why Don’t We Require a Main Method In TestNG class For Execution Of Methods? Uncategorized
JavaScript Way To Handle Calendar In Selenium | Make Selenium Easy Uncategorized
TestNG Tutorials 23: @Test Annotation – Don’t Confuse TestNG With Duplicate Priorities Uncategorized
Git Tutorial 23 – Git Stash Apply – How To Solve Merge Conflict Uncategorized
LatestRun – Make Selenium Easy Uncategorized
Method 2: getCssValue() : What, When and How to use? 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