Selenium WebDriver - Browser Commands
Last Updated :
24 Apr, 2025
Selenium WebDriver is an extremely useful tool for automating web applications and browser interactions. Through its collection of browser commands, developers and testers can control web browsers in a programmatic fashion. Browser commands that enable tasks such as web scraping, simplifying everyday operations on the web, and testing web applications. As Selenium WebDriver is compatible with a range of programming languages, it is highly accessible among developers of varying levels. The article focuses on discussing Selenium WebDriver Browser Commands in detail.
What are Browser Commands?
Selenium WebDriver provides predefined methods and functions that enable developers to interact directly with browsers.
- These commands provide exact control over the browser's actions, like loading specific pages, extracting details such as page titles and URLs, accessing page source code, and controlling browser windows.
- Browser commands provide an easy way to interact with web applications and to perform automation, and scraping data from web pages.
- The Browser Command provides methods: get(), getTitle(), getCurrentUrl(), getPageSource() , close() and quit().

Prerequisites
1. Java Development Kit (JDK): Install the JDK to write and execute Java code.
2. Integrated Development Environment (IDE): Choose an IDE like Eclipse, IntelliJ IDEA, or Visual Studio Code for writing and managing your Java projects.
3. Selenium WebDriver Java Client JAR Files: These files enable you to interact with the Selenium WebDriver API using Java.
4. WebDriver Executable: Download the WebDriver executable for your preferred browser (e.g., ChromeDriver, GeckoDriver) to automate browser actions.
5. Dependencies: Configure the required dependencies in your Java project to use Selenium WebDriver.
6. Import the required packages.
org.openqa.selenium.WebDriver
org.openqa.selenium.chrome.ChromeDriver
7. Set the system property for ChromeDriver (path to chromedriver executable).
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver.exe");
8. Create the instance of the ChormeDriver.
WebDriver driver = new ChromeDriver();
For detailed installation steps, refer to this article.
Brower Commands
1. get(String url) command
It loads a new web page in the current browser window and accepts a string parameter that specifies the URL of the web page to be loaded.
Method:
driver.get("https://www.geeksforgeeks.org/");
or
String URL = "https://www.geeksforgeeks.org/";
driver.get(URL);
Output:

Explanation:
- This command will start the loading of a new web page in the web browser that is currently open.
- It takes one argument in the form of a string which contains the URL of the page desired to be retrieved.
- This method is identical to the driver.navigate().to() which works for the same purpose.
- But, the driver.get() method waits until the page is completely finished loading before returning control back to the script whereas driver.navigate().the to() does not wait for the page to load and terminates quickly.
2. getTitle() Command
It gets the title of the current web page displayed in the browser. It does not accept any parameters. It returns the title of the specified URL as a string.
Method:
String pageTitle = driver.getTitle();
System.out.println(pageTitle);//it will print the title of the specified URL.
Output:

Explanation:
- This command obtains the title of the active webpage which will be same as displayed in the browser.
- It does not take any arguments and it return the title of the specified URL as a string.
- It can be utilized to access the title of the URL, which is be convenient for the purpose of confirmation in automated tests and for logging as well as reporting.
3. getCurrentUrl() Command
It gets the URL of the current web page shown in the browser. It does not accept any parameters and returns the URL as a string.
Method:
String currentURL = driver.getCurrentUrl();
Output:

Explanation:
- This technique obtains the web address of the present page.
- No arguments are accepted and it return the URL of the webpage as a string.
- This technique is similar to clicking within the search field of the browser and copying the link from the search area.
- It can be utilized to validate the present URL, which is very expedient for the function of corroboration in automated tests and for logging as well as reporting.
4. getPageSource() Command
It gets the entire page source of the current web page loaded in the browser. It does not accept any parameters but it do returns the page source as a string.
Method:
String pageSource = driver.getPageSource();
Output:

Explanation:
- This technique returns the entirety of the page source code of the existing URL.
- Taking in no parameters, it returns the page source as a string.
- This method corresponds to clicking anywhere on the current tab or browser window then picking 'View page source' or hitting CTRL + U on the keyboard.
- It's mostly used for extracting data (web scraping), diagnosing issues, and verifying page content.
5. close() Command
It closes the current browser window or tab. Also, this command does not accept any types of parameters. It also does not return anything.
Method:
driver.close();
Output:

Explanation:
- This approach shuts down the present window or tab of the browser.
- No parameters are accepted and nothing is returned.
- The said method is equal to clicking on the X of the active tabs or pressing CTRL + W on the keyboard.
- It is available to be utilized for closing specific browser windows or tabs while operating with several tabs in a single WebDriver session.
6. quit() Command
It closes all the browser windows and tabs for a particular WebDriver session. This command does not accept any parameters and not return anything.
Method:
driver.quit();
Output:

Explanation:
- This method quits the WebDriver session, closing all open browser windows and tabs associated with that session.
- It does not accept any parameters and does not return anything.
- This method is equivalent to clicking on the cross mark present in the top rightmost corner or pressing the ALT + F4 key on the keyboard.
- It can be used to end a WebDriver session and release browser resources.
Implementation
Interaction with the GeeksForGeeks Website using Brower Commands:
- It starts by initializing the ChromeDriver and navigating to the GFG Home page.
- After waiting for 2 seconds, get the Title of the current URL.
- The program then waits for another 2 seconds, it clicks on the "Data Structures" link on the GeeksforGeeks website, which redirects to another web page.
- After waiting for 2 seconds, it will get the URL of the redirected link.
- The program waits for another 2 seconds and closes the current tab or window only, using the driver.close() command and display "Browser closed" in the console.
- Lastly, the program quits the Webdriver session and closes all the open tabs in the browser.
Below is the Java program to implement the above approach:
Java
// Java program to implement Browser
// Commands
package Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class BrowerCommands {
public static void main(String[] args)
throws InterruptedException
{
// Set the system property to specify the path of
// the ChromeDriver executable.
System.setProperty(
"webdriver.chrome.driver",
"D:/chromedriver-win64/chromedriver-win64/chromedriver.exe");
// Create an instance of ChromeDriver (launch the
// Chrome browser).
WebDriver driver = new ChromeDriver();
// Maximize the browser window.
driver.manage().window().maximize();
// Define the URL to visit.
String URL = "https://www.geeksforgeeks.org/";
// Waiting for 2 seconds
Thread.sleep(2000);
// Open the specified URL in the browser.
driver.get(URL);
System.out.println("Visited GeeksForGeeks");
// Waiting for 2 seconds
Thread.sleep(2000);
// Getting the Title of the URL
String pageTitle = driver.getTitle();
System.out.println(pageTitle);
// Waiting for 2 seconds
Thread.sleep(2000);
// Getting current URL
// Clicking on a link that redirects to another web
// page
driver.findElement(By.linkText("Data Structures"))
.click();
System.out.println(
"Clicked on Data Structures link");
// Waiting for 2 seconds
Thread.sleep(2000);
// Getting the current URL after the click
String currentURL = driver.getCurrentUrl();
System.out.println(currentURL);
// Waiting for 2 seconds
Thread.sleep(2000);
// close the current tab
driver.close();
System.out.println("Browser closed");
// Close the all the tabs or windows of the browser.
System.out.println("Closed all tabs");
driver.quit();
}
}
Output:

Conclusion
Browser commands available in Selenium WebDriver, including get(), getTitle(), getCurrentUrl(), getPageSource(), close(), and quit() are important for retrieving the information and also control the browser behaviour. Browser commands provide easy way to interact with web application and to perform automation, and scraping data on the web pages.
Similar Reads
Non-linear Components
In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Class Diagram | Unified Modeling Language (UML)
A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Spring Boot Tutorial
Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Backpropagation in Neural Network
Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read
Polymorphism in Java
Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca
7 min read
AVL Tree Data Structure
An AVL tree defined as a self-balancing Binary Search Tree (BST) where the difference between heights of left and right subtrees for any node cannot be more than one. The absolute difference between the heights of the left subtree and the right subtree for any node is known as the balance factor of
4 min read
What is Vacuum Circuit Breaker?
A vacuum circuit breaker is a type of breaker that utilizes a vacuum as the medium to extinguish electrical arcs. Within this circuit breaker, there is a vacuum interrupter that houses the stationary and mobile contacts in a permanently sealed enclosure. When the contacts are separated in a high vac
13 min read
3-Phase Inverter
An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power
13 min read
What is a Neural Network?
Neural networks are machine learning models that mimic the complex functions of the human brain. These models consist of interconnected nodes or neurons that process data, learn patterns, and enable tasks such as pattern recognition and decision-making.In this article, we will explore the fundamenta
14 min read
Use Case Diagram - Unified Modeling Language (UML)
A Use Case Diagram in Unified Modeling Language (UML) is a visual representation that illustrates the interactions between users (actors) and a system. It captures the functional requirements of a system, showing how different users engage with various use cases, or specific functionalities, within
9 min read