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

 

6 thoughts on “How to load a URL in browser without using get() or navigate() method

      1. Amod Sir, am not getting any timeout in console. am just getting below mentioned set of lines :

        browser opened
        about:blank
        about:blank
        about:blank
        about:blank
        1500884784173 Marionette INFO New connections will no longer be accepted

Leave a Reply