Table of contents
1.
Introduction
2.
Web Browser Commands
2.1.
Get Commands
2.2.
Get Title Command
2.3.
Get the Current URL command
2.4.
Get the page source command
2.5.
Close command
2.6.
Quit Command
2.6.1.
Program
2.6.2.
Output
3.
FAQs
4.
Conclusion
Last Updated: Mar 27, 2024

Browser Commands

Author Aman Thakur
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Selenium executes commands such as opening a URL, clicking a button, typing in a textbox, turning off the browser, and closing the browser. To automate the software programme, we may use the web driver to perform a variety of command activities. To handle browser instructions, Selenium will need to create a web driver instance.

Let us see the few Selenium WebDriver Browser commands in use.

Web Browser Commands

WebDriver's most basic browser actions are opening a browser, conducting a few activities, and then closing the browser. Let us see some methods in it.

Get Commands

Method:

get(String URL):void;

This function in WebDriver creates a new browser window and loads a new web page into it.

It takes a String as an argument and returns a void as a result.

The appropriate command to load a new web page is as follows:

String URL = "URL";
webDriver.get(URL);
You can also try this code with Online Java Compiler
Run Code

For example, the command to load javaTpoint's official webpage may be expressed as:

webDriver.get(path);
You can also try this code with Online Java Compiler
Run Code

Get Title Command

Method:

getTitle(): String
This function in WebDriver retrieves the current web page's title. It returns a String and takes no parameters. 

To get the current page's title, use the following command:

String Title = webDriver.getTitle();
You can also try this code with Online Java Compiler
Run Code

Get the Current URL command

Method:
getCurrentUrl(): String

This function in WebDriver gets the string that represents the current web page's current URL. It takes no parameters and returns a String value as a result.

The command to retrieve the string that represents the current URL is as follows:

String CurrentUrl = webDriver.getCurrentUrl();
You can also try this code with Online Java Compiler
Run Code

Get the page source command

Method:

getPageSource(): String

This function in WebDriver retrieves the source code of the currently loaded web page in the current browser. It takes no parameters and returns a String value as a result.

To obtain the source code for the current web page, use the following command:

String PageSource = webDriver.getPageSource();
You can also try this code with Online Java Compiler
Run Code

Close command

Method:

close():void

This function closes the WebDriver-controlled browser window that is currently open. The browser is also terminated if the current window is WebDriver's sole active window. Nothing is passed as an argument, and the procedure returns void.

The command to close the browser window may be written as follows:

webDriver.close();
You can also try this code with Online Java Compiler
Run Code

Quit Command

Method:

quit():void

This technique closes all WebDriver-controlled windows. It closes all open tabs as well as the browser. Nothing is accepted as an argument, and it returns void.

The command to close all windows may be written as follows:

webDriver.quit()
You can also try this code with Online Java Compiler
Run Code

Program

package BrowserCommands;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class BrowserCommands {
   public static void main(String[] args) {  
       System.setProperty("webdriver.chrome.driver", "../Dependencies/chromedriver");
     
       WebDriver webDriver= new ChromeDriver();


       String URL = ("https://www.google.co.in/"); 
       webDriver.get(URL);


       String title = webDriver.getTitle();
       int titleLength = webDriver.getTitle().length();
  
       System.out.println("Title of the page is : " + title);
       System.out.println("Length of the title is : "+ titleLength);


       String actualUrl = webDriver.getCurrentUrl();


       if (actualUrl.equals("https://www.google.co.in/")){
           System.out.println("Verification Successful - The correct Url is opened.");
       }
       else{
           System.out.println("Verification Failed - An incorrect Url is opened.");
       }


       // Storing Page Source in String variable 
       String pageSource = webDriver.getPageSource();


       // Storing Page Source length in Int variable 
       int pageSourceLength = pageSource.length();


       // Printing length of the Page Source on console 
       System.out.println("Total length of the Pgae Source is : " + pageSourceLength);


       //Closing browser 
       webDriver.close();
   }
}
You can also try this code with Online Java Compiler
Run Code

Output

FAQs

1. What is the difference between a click and submit in Selenium?
Ans: The click() method is only relevant to submit buttons in a form. The submit() method will wait for the page to load, but the click() function will only wait if there is an explicit wait condition. The submit() function cannot be used on a form with a submit of type button.

2. What's the fastest locator in Selenium?
Ans: The most recommended and fastest approach to identify needed WebElements on the page is to use Selenium's ID locator. Each element in the DOM has its own ID Selenium locator. Because each element on the page has its own ID, it is thought to be the quickest and safest way to locate them.

3. What is Page factory in Page object model?
Ans: Page Factory is a Selenium WebDriver class that implements the Page Object Model. The Page Factory idea is used to segregate the Page Object Repository from the Test Methods. You may use it to either initialise or directly instantiate Page Objects.

4. What is the parent class of WebDriver?
Ans: SearchContext is the most significant Selenium interface, which is extended by another interface called WebDriver.The RemoteWebDriver class implements all of the SearchContext and WebDriver interfaces' abstract functions.

Conclusion

If you have reached till here that means, you really enjoyed this article, This article covers the important webDriver Browser commands which are commonly used in under selenium, code snippets and their use cases. And a program which automated the java program.

Recommended Readings: 

Nmap commands

There are few articles that you might be interested in, such as installation of selenium Interview QuestionsDifference between RTL vs Enzyme . Do upvote our blog to help other ninjas grow, and head over to our practice platform Coding Ninjas Studio to practise top problems, attempt mock tests, read interview experiences, and much more.

Happy Learning !

Live masterclass