Selenium WebDriver Commands, Methods List

In this tutorial, we will learn about the 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 that object.

Thus, we can call Selenium WebDriver methods using an object reference variable. Let’s see a glance at the list of all possible methods provided by WebDriver in the below screenshot.

List of Selenium WebDriver 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
  • Result commands

Let’s understand the first get commands 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 that are as follows:

  • get()
  • getCurrentUrl()
  • getTitle()
  • getPageSource()
  • getText()
  • getTagName()
  • getCssValue()
  • getAttribute(String Name)
  • getSize()
  • close()
  • quit()

Let’s understand all get commands one by one with examples.

1. Get Command

Method:

get(String arg) : void

a) The get() method takes a string URL as a parameter and returns nothing.

b) This method opens the specified URL in the current browser window. URL must be in the form of https://www.google.com. If the HTTPS is not included then it will throw a message “Cannot navigate to invalid URL”.

The general syntax to use get command is as follows:

Syntax:
     driver.get(https://www.google.com);

WebDriver Get command

For example:

driver.get("https://www.google.com"); 

// Or, it can be also written as 
  String URL = "https://www.scientecheasy.com"; 
  driver.get(URL);


2. Get Current URL Command

Method:

 getCurrentUrl() : String

a) The getCurrentURL() method takes nothing as a parameter and returns URL of the web page currently loaded in the browser.

b) This method is used to get 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, we will store it using variable currentUrl with String data type.

3. Get Title Command

Method:

getTitle() : String

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

b) 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

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

b) 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, the output must be stored in a variable with datatype String.


5. Get Text Command

Method:

getText() : String

a) The getText() method accepts nothing as a parameter and returns a string value.

b) 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

a) The getTagName() method takes nothing as a parameter and returns a String value.

b) 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

a) The getCssValue() method accepts nothing as a parameter and returns a String value.

b) 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

Methods:

getAttribute(String Name) : String

a) The getAttribute() method takes the String as a parameter and returns a String value.

b) 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

a) The getSize() method does not accept any parameter but returns the size of the element on the page.

b) 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

a) The close method takes nothing as a parameter and returns nothing.

b) 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

a) The quit() method accepts nothing as a parameter and returns nothing.

b) 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 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 code to understand the program clearly. So, let’s start coding.

Program 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 home page title.
4. Verify actual tile of the home page with the expected title.
5. Print home page title on the console.
6. Close the browser.

Let’s understand the following program code related to the above scenario.

Program 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 getTitle() 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))  // Verifying the home page title.
   { 
     System.out.println("Verification is success" ); 
     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 about understanding it, you can go to this link to learn more in detail. How to create Object in Java with Example


Let’s take an example to practice based on getCurrentUrl() and getPageSource() methods.

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 the console.
5. Fetch page source of current web page.
6. Close the browser at last.

Let’s see the following program code to automate the above scenario.

Program 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/

Key Points for Selenium WebDriver Commands

Here, we have listed some key points on the basic Selenium WebDriver commands that are as follows:

  • get(url): Opens the specified URL in the web browser.
  • getTitle(): Returns the title of the current web page.
  • getCurrentUrl(): Retrieves the URL of the current web page.
  • getPageSource(): Returns the source code of the current web page.
  • close(): Closes the current browser window.
  • quit(): Closes all browser windows opened by the WebDriver.