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);For example, the command to load javaTpoint's official webpage may be expressed as:
webDriver.get(path);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();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();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();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();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()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();
}
}Output






