Selenium WebDriver Browser and Navigation Commands

Browser commands are the starting point of your Selenium Webdriver script. These commands used to launch browser, maximize it, open URL to be tested and other navigation commands.

1. Browser Commands

i. Set Path of browser/driver executable:
  • This would be the first line of your webdriver script. You have to download the browser executable file for the browser you are using and set path of the driver executbale to system property.
  • Like for firefox you have to download geckodriver.exe and place in your project. Similarly for other browsers you have to download their browser/driver executables.
  • Download Driver Executables link -
  • Below is an example for Firefox.

// Set Path of driver executable
String driver_executable_path = "./src/com/techlistic/utils/geckodriver.exe";

System.setProperty("webdriver.gecko.driver", driver_executable_path);

ii. Launch Broswer:

// Launch Browser - Creating a Firefox instance
WebDriver driver = new FirefoxDriver();

Code for other browsers :

// For Chrome
WebDriver driver = new ChromeDriver();

// For Safari
WebDriver driver = new SafariDriver();

// For Opera
WebDriver driver = new OperaDriver();

// For IE
WebDriver driver = new InternetExplorerDriver();

iii. Open URL
  • This command is used to open URL. You can provide a string which contains a url.
// Open URL 

// Or 
String url = "";


iv. Get Title
  • This command is used to obtain title of the page. It returns a string.
// Get Title

// Code implementation example
String pageTitle = driver.getTitle();
// Validate Page Title
if(pageTitle == "Techlistic - Home") {
 System.out.println("Test Passed.");
else {
 System.out.println("Test Failed.");

v. Get Current URL
  • This command is used to get url of current page. It returns a string.
// Get Current URL

//Code implementation example
String pageURL = driver.getCurrentUrl();

// Validate Current Page URL
if(pageURL == "") 
 System.out.println("Test Passed.");
 System.out.println("Test Failed.");

vi. Get Page Source
  • This command is used to get the source code of the web page. It returns a string.
// Get Page Source

// Or
String sourceCode = driver.getPageSource();

vii. Close Browser
  • This command is used to close the current browser window. It is generally used at the end of script.
viii. Quit Browser
  • This command is used to close all the browser windows opened by Webdriver. It is used at end of the script
// Close Current Browser Window

// Close All Browser Windows

    ix. Maximize Browser

    x. Make Browser Full Screen

    xi. Minimize Browser

    xii. Set Size of Browser

    // Maximize Browser
    // Make Browser Window Full Screen
    // Minimize the browser
    driver.manage().window().setPosition(new Point(0, -1000));
    // Set Size of browser
    driver.manage().window().setSize(new Dimension(1920, 1080));

    Program - Browser Commands
    You can refer the following example program for browser commands.

    package com.techlistic.selenium;
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;
    public class A_01_BrowserCommands_Test {
     public static void main(String[] args) {
      // Expected Title
      String expectedTitle = "Techlistic";
      // Set Path of the Chrome driver
      System.setProperty("", "./src/com/techlistic/utils/chromedriver.exe");
      // Launch Firefox browser
      WebDriver driver = new ChromeDriver();
      // Open URL of Website
      // Maximize Window
      // Get Title of current Page
      String actualTitle = driver.getTitle();
      System.out.println("Title fetched: "+ actualTitle);
      // Validate/Compare Page Title
      if(expectedTitle.equals(actualTitle)) {
       System.out.println("Test Case Passed.");
      else {
       System.out.println("Test Case Failed.");
      // Close Browser

    2. Browser Navigation Commands

    These commands are used to move forward and back using browser history.

     * Browser Navigation Commands     *
    // Navigate Forward
    // Navigate Back
    // Refresh Page
    // Naviagte directly to some URL (which might be external or internal in the website)

    Selenium Demo Websites  << Previous      ||      Next >>  Find Element & Locators

    Follow Us

    Quora Space | Facebook Page | Telegram Channel
    Feel free to ask queries or share your thoughts in comments or email us.


    1. Excellent. Content Simplified in its best way. Please post on pending areas in the index.

    2. very helpful to practise these Assignments


    Post a Comment

    Popular posts from this blog

    10 Best Demo Websites for Automation Testing Practice

    Automate 'User Registration' of E-commerce Website using Selenium

    Automate Browser Actions on using Selenium

    Automate E-Commerce Website using Selenium Webdriver