Selenium WebDriver- Commands

As we have discussed earlier in the IDE section, Selenium commands are the set of commands that are used to run our Selenium tests.

In Selenium WebDriver, we have an entirely different set of commands for performing different operations. Since we are using Selenium WebDriver with Java, commands are simply methods written in Java language.

Note: A java method is a collection of statements that are grouped together to perform a specific operation.

Before getting into the details of commands provided by Selenium WebDriver, we insist you to go through the Java OOPs (Object-Oriented Programming) concepts in java programming language. You can also refer to our Java OOPs concepts section provided in the Java Tutorial.

Now, the question arises is that how can we access methods provided by WebDriver.

Till now, we have successfully created our first test script in Selenium WebDriver. Therefore, one possible way to view the methods provided by WebDriver is to open the Eclipse IDE loaded with Selenium Webdriver jar files, create a driver object for WebDriver and press the dot key. It will show you all of the possible methods provided by WebDriver.

Selenium WebDriver Commands

Let us consider an example form the suggestions displayed by Eclipse to understand the syntax of the methods provided by WebDriver.

Selenium WebDriver Commands

Method Name

To access any method of any class, we need to create an object of class and then all the public methods will appear for the object.


A parameter is an argument which is passed to a method to perform some specific operation.

Return type

Methods can return a value or returning nothing (void). If the void is mentioned after the method, it means, the method is returning no value. If it is returning a value, then it must display the type of the value for e.g. getTitle(): String.

Now, we will discuss the various commands provided by WebDriver. The commands provided by Selenium WebDriver can be broadly classified in following categories:

  1. Browser Commands
  2. Navigation Commands
  3. WebElement Commands

Given below are some of the most commonly used Selenium commands in WebDriver:

1. Fetching a web page

There are two methods to fetch a web page:

  • Using Get method
  • Using Navigate method

2. Locating forms and sending user inputs

driver.findElement("lst-ib")).sendKeys("rookienerd tutorials");

3. Clearing User inputs

The clear() method is used to clear the user inputs from the text box.


4. Fetching data over any web element

Sometimes we need to fetch the text written over a web element for performing some assertions and debugging. We use getText() method to fetch data written over any web element.


5. Performing Click event

The click() method is used to perform click operation on any web element.


6. Navigating backward in browser history


7. Navigating forward in browser history


8. Refresh/ Reload a web page


9. Closing Browser


10. Closing Browser and other all other windows associated with the driver


11. Moving between Windows


13. Moving between Frames


14. Drag and Drop

Drag and Drop operation is performed using the Action class.

WebElement element = driver.findElement("source"));
WebElement target = driver.findElement("target"));

(new Actions(driver)).dragAndDrop(element, target).perform();

Let us consider a sample test script which will cover most of the commonly used WebDriver commands.

For our test purpose, we are using a dummy web page under the URL:

The default interface of the web page looks like this:

Selenium WebDriver Commands

You can also use this dummy web page for your Selenium Testing practice.

First you need to download the browser driver for the browser on which you are willing to automate your test scenarios. We have already discussed execution of Selenium test scripts on different browsers in the previous sections of this tutorial.

For this test, we are using Firefox Gecko driver to automate our test scenarios on Firefox browser.

Below is the sample test script with embedded comments.

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.DesiredCapabilities;

public class Second {

	public static void main(String[] args) {
		  // System Property for Gecko Driver 
	System.setProperty("webdriver.gecko.driver","D:\\GeckoDriver\\geckodriver.exe" );
		 // Initialize Gecko Driver using Desired Capabilities Class
		DesiredCapabilities capabilities = DesiredCapabilities.firefox();
		WebDriver driver= new FirefoxDriver(capabilities);
		// Launch Website
	    // Fetch the text "This is sample text." and print it on console
	    // Use the class name of the div to locate it and then fetch text using getText() method
	 String sampleText = driver.findElement(By.className("col-md-12")).getText();
	      // Use the linkText locator method to find the link and perform click using click() method
	 driver.findElement(By.linkText("This is a link")).click();
	      // Click on the textbox and send value
	 	// Clear the text written in the textbox
	 	// Click on the Submit button using click() command
		// Locate the radio button by id and check it using click() function
		// Locate the checkbox by cssSelector and check it using click() function
		// Use Select class for selecting value from dropdown
	Select dropdown = new Select(driver.findElement("testingDropdown")));
	dropdown.selectByVisibleText("Automation Testing");
		// Close the Browser

Related Tutorial
Follow Us #
Contents +