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

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/

My first very small post. I hope you must have learnt new concept.

If you like my posts, please like, comment, share and subscribe. If you have any feedback or suggestions, feel free to contact me.

Thank you!!

#HappyLearning

 

Author: Amod Mahajan

My name is Amod Mahajan and I am an IT employee with 4+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went throw so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning