Selenium WebDriver Commands

In this post, we will learn some of the basic selenium commands for performing operations like opening a URL, clicking on buttons, writing in textbox, closing the browser etc. For your practice, a dummy webpage with different types of web elements is available. Now, let's see the basic commands of Selenium WebDriver.

Opening a URL

Using Get method-

The driver.get() method is used to navigate to a web page by passing the string URL as parameter. Syntax-


Using Navigate method-

The driver.navigate().to() method does the task of opening a web page like driver.get() method. Syntax-


Clicking on WebElements

The click() method in Selenium is used to perform the click operation on web elements. In our previous tutorial Locators in Selenium WebDriver, we studied about locating the webElements in Selenium. The click() method is applied on the webElements identified, to perform the click operation.

//Clicking an element directly

//Or by first creating a WebElement and then applying click() operation
WebElement submitButton = driver.findElement("button2"));;

Writing in a Textbox

The sendKeys() method can be used for writing in a textbox or any element of text input type.

//Creating a textbox webElement
WebElement element = driver.findElement("q"));

//Using sendKeys to write in the textbox

Clearing text in a Textbox

The clear() method can be used to clear the text written in a textbox or any web element of text input type.

//Clearing the text written in text fields

Fetching text written over any web element

In automation, many a times we need to fetch the text written over a web element for performing some assertions or debugging. For this, we have getText() method in selenium webDriver.

//Fetching the text written over web elements

Navigating backwards in a browser

Selenium provides navigate().back() command to move backwards in the browser's history.

//Navigating backwards in browser

Navigating forward in a browser

Selenium provides navigate().forward() command to move forward in a browser.

//Navigating forward in browser

Refreshing the browser

There are multiple ways to refresh a page in Selenium WebDriver-

  • Using driver.navigate().refresh() command
  • Using sendKeys(Keys.F5) on any textbox on the webpage
  • Using driver.get("URL") with current URL
  • Using driver.navigate().to("URL") with current URL
//Refreshing browser using navigate().refresh()

//By pressing F5 key on any textbox element

//By opening the current URL using get() method

//By opening the current URL using navigate() method

Closing browser

Selenium provides two commands to close browsers close() and quite(). The driver.close() command is used to close the browser having focus. Whereas, the driver.quite command is used to close all the browser instances open.

//To close the current browser instance

//To close all the open browser instances

Sample Script

You can use the sample script below to automate the dummy webpage. The comments mentioned throughout the script will guide you through whole automation process. Also, note that the Thread.sleep() used in the sample script is to pause the automation in between events. This Thread.sleep() is not required and is only included to provide you some time to see the automation events.
Download this java file here (right click on '' and click on 'save link as...' to save the sample selenium test script).

package myTestPackage;

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.Test;

public class seleniumBasicCommands {
public static void main(String Args[]) throws InterruptedException{
	//Create Firefox driver's instance
	WebDriver driver = new FirefoxDriver();
	//Set implicit wait of 10 seconds
	//This is required for managing waits in selenium webdriver
	driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
	//Launch sampleSiteForSelenium
	//Fetch the text "This is sample text!" and print it on console
	//Use the id of the div to locate it and then fecth text using getText() method
	String sampleText = driver.findElement("idOfDiv")).getText();
	//Waiting for 3 seconds just for user to efficiently check automation
	//Its not mandatory though
	//Using linkText locator to find the link and then using click() to click on it
	driver.findElement(By.linkText("This is a link")).click();
	//Finding textbox using id locator and then using send keys to write in it
	driver.findElement("fname")).sendKeys("Kuldeep Rana");
	//Clear the text written in the textbox
	//Clicking on button using click() command
	//Find radio button by name and check it using click() function
	//Find checkbox by cssSelector and check it using click() function
	//Using Select class for for selecting value from dropdown
	Select dropdown = new Select(driver.findElement("testingDropdown")));
	dropdown.selectByVisibleText("Database Testing");
	//Close the browser