In this tutorial, we will learn about very important and most frequently used Selenium WebDriver commands for performing different operations in selenium tests.

Since we are using Selenium WebDriver with Java, commands are also called methods that are written in Java language.

To access any Selenium WebDriver methods, First, we need to create a driver object using a WebDriver reference variable and then all the public methods will appear for the object. Thus, we can call Selenium WebDriver methods using an object reference variable.

Let's see a glance at list of all the possible methods provided by WebDriver in the below screenshot.
List of all WebDriver methods/commands
To view all the above methods shown in the figure,  just open the Eclipse IDE loaded with Selenium Webdriver jar files, create a FirefoxDriver object for WebDriver and press a dot key. It will show all of the possible methods provided by WebDriver. The following source code is given below:
    package webDriverCommands; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class WebDriverMethods { public static void main(String[] args) { // Create a driver object using WebDriver reference variable. WebDriver driver=new FirefoxDriver(); driver. } }

Classification of Selenium WebDriver Commands


Selenium WebDriver commands can be classified as Get commands, Navigation commands, WebElement commands, Action commands, and Result commands.
Let's see first Get command in detail.

Selenium WebDriver: Get Commands

Get commands are the most commonly used commands to perform the basic operations of WebDriver such as opening a browser, performing a few tasks, and then closing the browser. There are the following get methods provided by Selenium WebDriver. They are as follows:

1. Get Command

Method:
    get(String arg) : void
➲ The get() method takes a String URL as a parameter and returns nothing.
➲ This method opens the specified URL in the current browser window. The URL must be in the form of http://www.google.com. If the http is not included then it will throw a message "Cannot navigate to invalid URL".

Syntax:
         driver.get(http://www.google.com);
WebDriver Get method
For example:
    driver.get("http://www.google.com"); //Or It can be also written as String URL = "http://www.scientecheasy.com"; driver.get(URL);

2. Get Current URL Command

Method:
    getCurrentUrl() : String
➲ The getCurrentURL() method takes nothing as a parameter and returns the URL of the page currently loaded in the browser.

➲ This method is used to get the String URL of the current web page loaded in the opened browser.
Syntax:
           driver.getCurrentUrl();

For example:
    driver.getCurrentUrl(); //The above code can be also written as String currentUrl = driver.getCurrentUrl(); // Since the return type of getCurrentUrl() method is String. So, we will store it using variable currentUrl with String data type.

3. Get Title Command

Method:
    getTitle() : String
➲ The getTitle() method takes nothing as a parameter and returns the page title of the currently loaded web page. If the web page has no title, it will return a null String.
➲ This method is used to get the title of the current web page.
Syntax:
         driver.getTitle();

For example:
    driver.getTitle(); // The above code can also be written as String title = driver.getTitle(); // Return type of getTitle is String.

4. Get Page Source Command

Method:
    getPageSource() : String
➲ The getPageSource() method accepts nothing as a parameter and returns the source code of the current web page.

➲ This method is used to get the page source code of the currently loaded web page.
Syntax:
        driver.getPageSource();

For example:
    driver.getPageSource(); // Or String pageSource = driver.getPageSource(); // Since return type of the this method is String. Therefore, the output must be stored in a variable with datatype String.

5. Get Text Command

Method:
    getText() : String
➲ The getText() method accepts nothing as a parameter and returns a string value.
➲ This method is used to retrieve the inner text of the specified element.
Syntax:
       driver.findElement(By.locatorName("Specified Element")).getText();

For example:
    String text=driver.findElement(By.id("India")).getText(); // id is name of locator. India is a specified element. System.out.println(text); // The above code can also be written as WebElement element=driver.findElement(By.id("India")); String text=element.getText(); // Return type of getText method is String. System.out.println("Inner Text: " +text);

6. Get Tag Name Command

Method:
    getTagName() : String
➲ The getTagName() method takes nothing as a parameter and returns a String value.
➲ This method is used to get the tagName of the WebElement returned by the findElement(By) method.
Syntax:
         element.getTagName(); // Here. element is a variable whose return type is WebElement.

For example:
    String tagName=driver.findElement(By.id("DownloadButton")).getTagName(); // or WebElement element=driver.findElement(By.id("DownloadButton")); String tagName=element.getTagName(); System.out.println(tagName);

7. Get CSS Value Command

Method:
    getCssValue() : String
➲ The getCssValue() method accepts nothing as a parameter and returns a String value.
➲ This method is used to fetch the value of the CSS property of the given web element when it is invoked.
Syntax:
         element.getCssValue(); 

8. Get Attribute Command

Method:
    getAttribute(String Name) : String
➲ The getAttribute() method takes the String as a parameter and returns a String value.
➲ It is used to fetch or get the value of the attribute of the WebElement.
Syntax:
         element.getAttribute();

For example:
    WebElement element = driver.findElement(By.id("DownloadButton")); String attributeValue = element.getAttribute("id"); //This will return "DownloadButton" System.out.println(attributeValue);

9. Get Size Command

Method:
    getSize() : int
 The getSize() method does not accept any parameter but returns the size of the element on the page. 
 This method is used to fetch the width and height of the rendered element.
Syntax:
         element.getSize();

For example:
    WebElement element = driver.findElement(By.id("DownloadButton")); Dimension elementSize = element.getSize(); System.out.println(“Height :” + elementSize.height + ”Width : "+ elementSize.width);

10. Close Command

Method:
    close() : void
➲ The close method takes nothing as a parameter and returns nothing.
➲ This method is used to close only the browser window that web driver is currently controlling.
Syntax:
         driver.close();

11. Quit Command

Method:
    quit() : void
➲ The quit() method accepts nothing as a parameter and returns nothing.
➲ This method is used to close all windows opened by WebDriver.
Syntax:
         driver.quit();

Selenium WebDriver Methods Example Programs


Let us take an example program based on get(), getTitle(), and close() methods.

Scenario to Automate:
1. Launch the Firefox browser.
2. Visit the home page of Google.
3. Wait to be loaded the web page fully.
4. Get the title of home page and print the title on the console.
5. Close browser.

Follow all the steps in the following program source code to understand the program clearly. So, let's start coding.
Program source code 1:
    package seleniumProject; import java.util.concurrent.TimeUnit; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class GetCommandExample { // To execute any program we need main main() method. So we will continue from the main method. public static void main(String[] args) { // Create a driver object using WebDriver reference. WebDriver driver=new FirefoxDriver(); // FirefoxDriver class with no parameters. So, Default Firefox will be launche by Java program. Default Firefox profile is similar to launching the firefox in save mode. // Declaration of variable URL with data type String and store the URL of the webpage. String URL="https://www.google.co.in"; // Now call the get() method to open the URL of the web page in the existing browser. driver.get(URL); // It takes String URL as a parameter and returns nothing. // Wait For Page To Load driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS); // Print a message to display on the console that the website is opened successfully. System.out.println("Website is opened successfully"); // Call getTitle() method using reference variable to get the title of the web page. String getTitle=driver.getTitle(); // Now print title of web page on the console. System.out.println("Title of home page: " +getTitle); // Now call the close() method using reference variable 'driver' to close the browser. driver.close(); } }
    Output: Website is opened successfully Title of home page: Google
Explanation:
1. When the statement WebDriver driver=new FirefoxDriver(); is executed, it will launch the Firefox web browser.

2. When the statement driver.get(URL); will be executed, it will open the web page with a specified URL.
3. After visiting the home page, the driver will wait for 60 seconds on the current web page.

4. When the statement driver.getTitle() method will be executed, the driver will fetch the title of Google home page and then it will store title in a variable getTitle. After it, it will print the title on the console.
5. driver.close() method will close the existing browser.

How to verify title in Selenium WebDriver using Java?


In this section, we will verify the title of the web page in Selenium WebDriver using the String concept of Java.

Scenario to Automate:
1. Launch the web browser.
2. Open the URL https://www.facebook.com in the current browser.
3. Fetching the home page title.
4. Verify the actual tile of the home page with the expected title.
5. Print the home page title on the console.
6. Close the browser.

Let's understand the following source code related to above scenario.
Program source code 2:
    package seleniumProject; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class VerifyingTitle { public static void main(String[] args) { // Create WebDriver reference. WebDriver driver; // Till now the driver object is not created. // Create an driver object using WebDriver reference variable. driver=new FirefoxDriver(); // Now driver object is created. String URL="https://www.facebook.com"; driver.get(URL); // It will navigate the Facebook home page in the existing web browser. // Call get() method to get the actual title of home page. String actualTitle=driver.getTitle(); String expectedTitle="Facebook – log in or sign up"; // Use equalsIgnoreCase() method to verify actual title with expected title. if(actualTitle.equalsIgnoreCase(expectedTitle)) { System.out.println("Verification is success" ); //verifying the home page title. System.out.println("Home page title is:" + actualTitle ); } else { System.out.println("Verification is failed, Matched not found"); } driver.close(); } }
    Output: Verification is success Home page title is:Facebook – log in or sign up
Explanation:
1. In the preceding example program, we have created a driver object in two steps. First, we have declared WebDriver reference variable and then created a driver object of Firefox class using reference variable of WebDriver.

2. equalsIgnorecase() method performs the compersion between two strings by ignoring the case difference. i.e, it only checks the content characters.

If you any doubt in understanding it, you can go to below link to learn more in detail. How to create Object in Java with ExampleJava String Compare | Example & Memory Explanation Let's one more practice a program based on getCurrentUrl() method and getPageSource() method.
Scenario to Automate:
1. First, launch the browser.
2. Open the Google home page.
3. Wait for the web page to be load.
4. Fetch the current URL of current web page and print it on console.
5. Fetch page source of current web page.
6. Close the browser at last.

Let's see the following source code to automate the above scenario.
Program source code 3:
    package seleniumProject; import java.util.concurrent.TimeUnit; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class GetCommandExample2 { public static void main(String[] args) { // Create a WebDriver reference and assign a driver object with it. WebDriver driver; driver=new FirefoxDriver(); // Declaration of variable URL with data type String and store the URL of the webpage. String URL="https://www.google.co.in"; // Call the get() method to open the URL of the web page in the existing browser. driver.get(URL); // Wait For Page To Load driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); // Call getCurrentUrl() method using reference variable to get the current url of the web page. String getCurrentUrl=driver.getCurrentUrl(); // Print current URL of web page on the console. System.out.println("Current URL of Home page: " +getCurrentUrl); // Call getPageSource() method to get page source of web page. String getPageSource=driver.getPageSource(); // Now call the close() method using reference variable 'driver' to close the browser. driver.close(); } }
    Output: Current URL of Home page: https://www.google.co.in/
Final words
Hope that this tutorial has covered almost all important points related to Selenium WebDriver commands list with practical example programs. I hope that you will have understood this topic and enjoyed the practice. In the next tutorial, we will learn navigation commands in selenium.
Thanks for reading!Next ➤ Navigation Commands in Selenium ⏪ Prev
Next ⏩