Click here to Skip to main content
14,934,503 members
Articles / DevOps / Automation
Posted 20 Oct 2014


19 bookmarked

Selenium WebDriver Browser Commands

Rate me:
Please Sign up or sign in to vote.
4.96/5 (13 votes)
20 Oct 2014CPOL3 min read
Here I describe all the commands that are required for the Selenium to perform specific tasks.


In this part of my tip, we are going to discuss about various browser commands that we would be using in our day to day life in testing. Opening and closing a browser is the very first thing in Selenium, a tester would like to do. The commands refer to what Selenium has to do. Just like a master commands a slave in early days!! Sorry even today. OK!! Let's not get social now.
So here, we discuss regarding the commands we give to the Selenium in order to perform some specific task.

Below are different basic commands, which we will encounter while writing the scripts.

Get Commands

Get commands are used to collect various information about the page that we deal with. Here are some important "get" commands you must be familiar with:

  1. Get Command - " get() ": get() command is used to open a new browser window and it will find/fetch the page that you have given.
  2. Get Title Command - " getTitle() " : This command is used to get the title of the current page.
  3. Get Current URL Command - " getCurrentUrl() " : This command is used to get the current URL of the browser.
  4. Get Page Source Command - " getPageSource()" : This command is used to get the source code of the page.
  5. Get Text - " getText() ": This command is used to fetch the text of the element.

Navigate Commands

  1. Navigate To Command - " navigate().to() " : This command is like Get() command. It opens a new browser window and it will find/fetch the page that you have given.
  2. Navigate Refresh Command - " navigate().refresh() " : This command is used to refresh the current page.
  3. Navigate to back page - " navigate().back() " : This command is used to go back by one page on the browser's history.
  4. Navigate to forward page - " navigate().forward() " : Takes you forward by one page on the browser's history.
  5. Refresh page - " navigate().refresh() " : It refreshes the current page.

Other Useful Commands

  1. SwitchTo command - " driver.switchTo().window("windowName") " : This command is used to switch from one window to another.
  2. Switching from one iframe to another - " driver.switchTo().frame("frameName") " : This command is used to switch from a one iframe to another iframe.
  3. Handling Alerts - " driver.switchTo().alert() " : This command is used to handle alerts.
  4. Close Command - " close() ": This command closes the current window of the browser.
  5. Quit Command - " quit() " : This command is used to quit the browser and all the opened windows in the browser. These are the basic commands that are needed for writing simple script.

Now by using these commands, I will automate a browser by writing scripts so that we can have a clear idea of how to use these commands.

Test Scenario

  • Open a Firefox Browser.
  • Put an Implicit wait.
  • Navigate to the URL.
  • Maximize the window.
  • Store the Title Name.
  • Print the Title Name.
  • Store Title Length.
  • Print Title Length.
  • Store the URL Name.
  • Print the URL Name.
  • Store URL Length.
  • Print URL Length.
  • Print the name of Tab which is going to be clicked suppose [NEWS TAB].
  • Click on NEWS Tab.
  • Store the Text of an element of the new opened tab.
  • Print the Text Name of an element of the new opened tab.
  • Now Navigate to the previous page.
  • Print the Title of the previous page.
  • Now Navigate to the next page.
  • Print the Text of the next page.
  • Refresh the current page.
  • Store the Page Source in String variable.
  • Print the Page Source Name.
  • Store Page Source Length in Integer Variable.
  • Print Page Source Length.
  • Close the browser or page currently which is opened.

The code/snippet goes as below:

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Commands {

public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
WebDriver driver = new FirefoxDriver();

// Wait For Page To Load
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);

// Go to URL

// Storing Title name in String variable so that we can get the title name
String btitle = driver.getTitle();

// Printing the Title name in 2 ways
// 1st way
// 2nd way
System.out.println("Title is : " + btitle);

// Storing Title length in Int variable because we are dealing with Integers that's why int is used.
int bTitleLength = driver.getTitle().length();

// Printing Title l0ength
System.out.println("Length of the Title is : " + bTitleLength);
// Storing URL in String variable
String b1 = driver.getCurrentUrl();

// Printing URL on Console Window

System.out.println("URL is : " + b1);

// Storing URL length
int b2 = driver.getCurrentUrl().length();

// Printing URL length on console Window

System.out.println("URL length is : " + b2);


//Open the NEWS Page.
String b3 = driver.findElement(By.xpath("//*[@id='blq-nav-news']/a")).getText();

// Print the Tab name which is being clicked.
System.out.println("Name of the Tab which is being clicked is : " + b3);

// Click on the Tab

//Get the text where Xpath is pointing.
String b4 = driver.findElement(By.xpath("//*[@id='header']")).getText();

// Print the text of the current page
System.out.println("Text of current page is : " + b4);

//Navigate to previous Page.

String b5 = driver.getTitle();

// Print the text of the previous page
System.out.println("Text of the previous page is : " + b5);

//Navigate to Next page.
// Print the text of the NEXT page
String b6 = driver.getTitle();
System.out.println("Text of the NEXT page is : " + b6);

// driver.get(driver.getCurrentUrl());

// Storing Page Source in String variable.
String b7 = driver.getPageSource();

// Printing the Page Source
System.out.println("Page source : " + b7);

// Storing Page Source length in Int variable
int b8 = driver.getPageSource().length();

// Printing the Page Source length
System.out.println("Length of the page source is : " + b8);

//close the browser or page currently which is having the focus.

//Close all the windows.

Below is the output of the commands executed as seen in the console window.

Image 1
  • SwitchTo command
  • Switching from one iframe to another
  • Handling Alerts

The above three useful commands are not covered in this part of the tip, I will be penning these details in my upcoming article.

Thanks for reading. Hope this helps beginners in testing.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Sipra Ray
Tester / Quality Assurance
India India
I am Sipra Ray, ISTQB certified - Quality Analyst at Mindfire Solutions.
An extrovert by nature ,I like to interact with people and share with all what I learn.
I would love to share and answer queries and gain knowledge from forums like Code Project
Follow up with my blogs at:-

Comments and Discussions

Suggestionget() and navigate() Pin
SUDORP6-Feb-16 5:55
professionalSUDORP6-Feb-16 5:55 
GeneralGreat Article on Selenium-Thanks and keep up the great work! :) Pin
Member 1200670323-Sep-15 6:46
MemberMember 1200670323-Sep-15 6:46 
GeneralBravo.... Pin
Anda Cristea12-Aug-15 4:33
MemberAnda Cristea12-Aug-15 4:33 
QuestionIs this part of you blog? Pin
Nelek20-Oct-14 13:31
protectorNelek20-Oct-14 13:31 
AnswerRe: Is this part of you blog? Pin
Sipra Ray21-Oct-14 0:11
MemberSipra Ray21-Oct-14 0:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.