Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Practice Problems
  • Python
  • C
  • C++
  • Java
  • Courses
  • Machine Learning
  • DevOps
  • Web Development
  • System Design
  • Aptitude
  • Projects
Open In App
Next Article:
Selenium WebDriver Commands
Next article icon

Selenium WebDriver - Browser Commands

Last Updated : 24 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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().

Browser Commands

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:

get(String url) command

Explanation:

  1. This command will start the loading of a new web page in the web browser that is currently open.
  2. It takes one argument in the form of a string which contains the URL of the page desired to be retrieved.
  3. This method is identical to the driver.navigate().to() which works for the same purpose.
  4. 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:

getTitle() Command

Explanation:

  1. This command obtains the title of the active webpage which will be same as displayed in the browser.
  2. It does not take any arguments and it return the title of the specified URL as a string.
  3. 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:

getCurrentUrl() Command

Explanation:

  1. This technique obtains the web address of the present page.
  2. No arguments are accepted and it return the URL of the webpage as a string.
  3. This technique is similar to clicking within the search field of the browser and copying the link from the search area.
  4. 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:

getPageSource() Command

Explanation:

  1. This technique returns the entirety of the page source code of the existing URL.
  2. Taking in no parameters, it returns the page source as a string.
  3. 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.
  4. 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:

close() Command

Explanation:

  1. This approach shuts down the present window or tab of the browser.
  2. No parameters are accepted and nothing is returned.
  3. The said method is equal to clicking on the X of the active tabs or pressing CTRL + W on the keyboard.
  4. 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:

quit() Command

Explanation:

  1. This method quits the WebDriver session, closing all open browser windows and tabs associated with that session.
  2. It does not accept any parameters and does not return anything.
  3. 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.
  4. It can be used to end a WebDriver session and release browser resources.

Implementation

Interaction with the GeeksForGeeks Website using Brower Commands:

  1. It starts by initializing the ChromeDriver and navigating to the GFG Home page.
  2. After waiting for 2 seconds, get the Title of the current URL.
  3. 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.
  4. After waiting for 2 seconds, it will get the URL of the redirected link.
  5. 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.
  6. 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:

Browser Commands

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.


Next Article
Selenium WebDriver Commands
author
the_ravisingh
Improve
Article Tags :
  • Geeks Premier League
  • Software Testing
  • selenium
  • Geeks Premier League 2023

Similar Reads

  • Selenium WebDriver Commands
    Selenium WebDriver is a powerful tool for automating the web browser to perform certain tasks. Selenium supports multiple browsers (such as Chrome, Firefox, Edge, Safari, etc.) and multiple programming languages (such as Java, Python, C#, etc.) so, it is very easy to use and automate tasks on a brow
    7 min read
  • Selenium WebDriver - Navigation Commands
    Selenium WebDriver is quite a popular open-source framework used for automating web browsers. It helps the developers to automate web-based operations and interactions by creating code in Java, Python, C#, and others. When automating tests, Selenium WebDriver offers a set of navigation commands that
    6 min read
  • Selenium WebDriver - WebElement Commands
    Selenium is an open-source program that automates web browsers. Selenium Webdriver is mainly used to execute the scripts according to the browser we are using. Prerequisites1. Install the Selenium Python Package.Use the following command to install the Selenium Python package: pip install selenium 2
    6 min read
  • Selenium WebDriver-Handling Alerts
    Selenium is one of the most popular and powerful tools for automated web applications. Selenium is widely used for automating user interactions like filling out forms, navigating to a website clicking on a button, etc. While we are dealing with automating user interactions one of the most common sce
    5 min read
  • Features of Selenium WebDriver
    Selenium is a powerful tool for controlling web browser through program. It is functional for all browsers, works on all major OS and its scripts are written in various languages i.e Python, Java, C# etc, we will be working with Python. This article revolves around Major Features of Selenium WebDriv
    2 min read
  • Architecture of Selenium WebDriver
    Selenium WebDriver is a powerful tool for automating web browsers. Its architecture comprises various key components, including the Selenium Client Library, WebDriver API, Browser Drivers, and the Browser itself. The Selenium Client Library provides language-specific bindings for interacting with We
    7 min read
  • Limitations of Selenium Webdriver
    Selenium is a powerful tool for controlling web browser through program. It is functional for all browsers, works on all major OS and its scripts are written in various languages i.e Python, Java, C#, etc but it has some disadvantages and limitations such as it doesn't support Windows or Desktop app
    2 min read
  • How to Install Selenium WebDriver on MacOS?
    In this article, we will learn how to install Selenium WebDriver in Python on macOS. Selenium WebDriver is a web framework that permits you to execute cross-browser tests. This tool is used for automating web-based application testing to verify that it performs expectedly. Installation Procedure: Fo
    2 min read
  • Selenium- WebDriver Vs RC Vs IDE
    Selenium is a famous system for Automatic internet browsers, utilized widely for web application testing. Inside the Selenium structure, two significant parts have advanced throughout the long term: Selenium WebDriver and Selenium RC (Controller). Both fill a similar need for Automatic internet brow
    4 min read
  • Selenium WebDriver-Installation
    Selenium WebDriver is a powerful tool for automating web applications for testing purposes. It allows developers and testers to write automated tests in various programming languages like Java, Python, C#, etc. Also, it supports different browsers like Firefox, Chrome, Edge, etc. for testing. Approa
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences