Prakash is a Data Scientist and Technical writer, with 8+ years of industry experience in software development and Testing. His primary experience includes Java, Selenium Testing, Python, Machine Learning, Cloud (AWS/Azure), and RDBMS.

Become a JMeter and Continuous Testing Pro

Start Learning
Slack

Test Your Website Performance NOW! |

arrowPlease enter a URL with http(s)
Oct 25 2021

Selenium Web Driver Commands: A Complete Tutorial

Selenium is an open source testing tool that automates the website testing process. It is not a single tool, but rather a collection that is made up of four parts: Selenium IDE, Selenium RC, Selenium WebDriver, and Selenium Grid. The last two are the most well-known. Selenium provides developers with a variety of features, including support for numerous browsers, parallel testing capabilities, and execution on many machines. It is one of the best solutions in the market in terms of performance and execution speed. This blog post will provide an overview of the top Selenium WebDriver commands you can use for test automation.

What is Selenium WebDriver?

Selenium Webdriver is an open-source API set for web application testing. It is used to automate web application testing in order to ensure that it functions properly. Selenium WebDriver accepts commands, transmits those commands to a browser, and communicates with programs. WebDriver is the most extensively used web application automation tool, and it works with all the major browsers: Firefox, Chrome, Safari and Internet Explorer. For script writing, testers can write test scripts in any supported programming language, like C#, Python, Perl, Java, Ruby, .NET and PHP.

Overview of Selenium WebDriver Commands

The techniques used to run Selenium test automation scripts are known as Selenium WebDriver commands. The Selenium Webdriver commands provide a number of options for interacting with the Webdriver and performing various tasks. These methods are accessed by calling "driver.methodName()" from a driver variable. Let’s go over the most widely used Selenium Webdriver commands and their syntax to help you to grasp them better.

 

Selenium WebDriver commands can be categorized as follows:

  1. Commands for the Browser
  2. Commands for Navigation
  3. Commands for Web Element
  4. Action Commands
  5. Result Commands   

1. Commands for Browser

Browser commands are those that can be used to execute operations on the browser itself, such as opening a browser, shutting down a browser, retrieving page source, and so on.

 

  • get() :

This command opens a new browser instance and navigates to the URL. The command accepts a single string type parameter, which is often the URL of the application being tested. Selenium IDE users might find that the command resembles the IDE open command.

 

Syntax:

driver.get(URL_PATH)

 

  • getTitle() :

This command retrieves the title of the webpage on which the user is currently working. If the webpage does not have a title, a null string is returned. There are no parameters required for this command, and it returns a trimmed string value.

 

Syntax:

String page_title = driver.getTitle();

 

  • getCurrentUrl():

This command retrieves the URL of the webpage that the user is presently viewing. There are no parameters required for this command, and it returns a string value.

 

Syntax:

String currentURL=driver.getCurrentUrl();

 

  • getPageSource():

This command returns the source code of the current web page loaded in the  browser, takes no parameters and returns a String value as a result.

 

Syntax:

String PageSource = driver.getPageSource()

 

  • close():

This function closes the WebDriver-controlled browser window that is currently open. If the current window is WebDriver's only active window, the browser is also terminated. Nothing is passed as a parameter, and the procedure returns void.

 

Syntax:

driver.close()

 

  • Quit():

There is a small difference between the quit() and Close() methods. The quit() method is used to end all open browser Instances, whereas the Close() method closes only the current browser instance. Both functions take no arguments and return void.

 

Syntax:

driver.quit()

 

2. Commands for Navigation

Navigation commands can be used by testers to move throughout the website. For example, open a web page URL, go to another web page by clicking any element, and use the browser's history to Back, Forward, or Refresh the web page. 
 

  • to(): This method is used to load a new web page in the existing browser window. The parameter string is recognised, yet it returns void.

 

Syntax: 

driver.navigate().to();

 

  • navigate() : This method opens a new browser window with a new web page. It accepts a String parameter as well as a void return type.

 

Syntax: 

driver.navigate().to(NewWebPageURL)

 

  • back() and forward() : To navigate back and forth on a Selenium WebDriver open window, utilise the forward() and backward() functions. The behaviour of these techniques is identical to that of a web browser's back and forward buttons.

 

Syntax: 

driver.navigate().forward();
driver.navigate().back();

 

  • findElement() : For Selenium WebDriver, any object on the website, such as a button, textbox, Listbox, radio buttons, headings, web tables, and so on, is a WebElement. In a nutshell, a WebElement is any HTML element. Before we can take any action on a WebElement, we must first obtain that WebElement. To acquire the required WebElement, utilize the findElement method..

 

  • refresh() : The current web page in the current browser window is refreshed or reloaded using this approach. It doesn't take anything and doesn't give it back.

 

Syntax: 

driver.navigate().refresh(); 

 

3. Commands for Web Element

WebElement is an HTML element that allows users to run automation tests with ease. Selenium Webdriver uses WebElements to enable well-organized web page interactions, such as identifying elements, getting attribute properties, asserting text in WebElement, and more. To interact with hidden elements in a web page, however, you must first unhide the hidden elements and then locate the element in the web page.

 

  • element.clear(): if the element is a text entry, its value will get cleared , and nothing is passed as a parameter to this procedure.

 

Syntax: 

element.clear();

 

  • element.sendKeys(): It allows the user to automatically type input into an editable field while running tests. These are web elements that can be recognized by locators such as element id, name, class name, and so on. This command receives CharSequence as an argument and returns nothing

 

Syntax:

element.sendKeys("text");

 

  • element.click(): This command allows you to interact with web elements such as text, links, and radio buttons, among others.

 

Syntax:

WebElement element = driver.findElement(By.linkText("Point 1"));  
element.click();

 

  • element.isDisplayed(): This command is used to determine whether or not an element is now visible. It takes no arguments and produces a boolean value (true/false).  

 

Syntax: 

WebElement element = driver.findElement(By.id("CityName"));  
boolean status = element.isDisplayed();

 

  • element.isEnabled():  This command determines whether or not the element is currently enabled. This function takes no parameters and produces a boolean value (true/false).

 

Syntax:

WebElement element = driver.findElement(By.id("UserID"));
boolean status = element.isEnabled();

 

  • element.isSelected(): Check to see if this element is selected. This function takes no input and returns a boolean value (true or false).  

 

Syntax:

	WebElement element = driver.findElement(By.id("married-Unmarried"));
boolean status = element.isSelected();

 

  • element.submit(): If the current element is a form or an element within a form, this command performs well/better than click(). It takes no parameters and returns nothing.

 

Syntax: 

WebElement element = driver.findElement(By.id("Button"));  
element.submit();  

 

  • element.getAttribute(): This command returns the value of the element's specified attribute. It takes a String as input and outputs a String value.

 

Syntax:

	WebElement element1 = driver.findElement(By.id("SubmitButton"));  
String attVal = element1.getAttribute("id");

 

4. Action Commands

The Actions class has a number of methods that return an action object, unless otherwise stated. In general, the methods of the actions class can be classified into two groups:

  1. Mouse Based Actions
  2. Keyboard Based Actions

 

Mouse Based Actions

  • click() : The method is used to perform a click operation at the current mouse position. This approach has no idea what it's clicking on or where it's clicking. It only makes a single click on the current place. It does not take any parameters and returns a self-reference.

 

Syntax: 

actions.click();

 

 

  • doubleClick() : This approach is used to double-click the mouse cursor where it is currently located. It takes no input parameters and returns an instance of the actions class. The moveToElement(element) method was used in the preceding syntax to move the mouse cursor to the element's location and double-click it. 

 

Syntax:

action.DoubleClick();

 

  • contextClick() : To do a right-click at the current mouse position, use this method. This technique is also known as the right-click technique.

 

Syntax:

action.contextClick();

 

  • clickAndHold(): This approach involves left-clicking on an element and holding it without releasing the mouse's left button, i.e. without releasing at the current mouse position. When doing drag-and-drop actions, it is often beneficial.

 

Syntax:

action.clickAndHold();

 

  • release(): This function is used to drop or release a mouse element. On a web element, it can let go of the left mouse button.

 

Syntax:

action.release();

 

  • dragAndDrop(WebElement,WebElement): This method is used to drop an element onto another element's position. The source element will be dragged and dropped in the dragAndDrop() function of the actions class.

 

Keyboard Based Actions

There are three possible keyboard actions to choose from: are keyDown,  keyUp, and sendKeys. 

 

  • keyDown(): To test the action of pressing and holding a key, use this approach. The keys that can be used are Shift, Ctrl and Alt. It takes one of three input parameters: Keys.SHIFT, Keys.CONTROL, or Keys.ALT. Selenium will throw an error as IllegalArgumentException if the passed key isn't one of those.

 

Syntax:  

keyDown(Input key);


 

  • keyUp() : This method is used to release a key that was pressed previously with the keyDown() method, meaning it's just opposite to keyDown. If you try to use this method on an unpressed key, it will result in undefined behaviour.

 

Syntax:

keyUp(Keys key):

 

The keyUp() and keyDown() functions are used to simulate pressing and releasing keys on a keyboard. Both can be used for a variety of things, such as converting text to uppercase or lowercase, copying text from one area and pasting it into another, scrolling up and down a web page, selecting several values, and so on. In Selenium test automation, this is one of the most commonly used Actions classes.

 

  • sendKeys(CharSequence): This method is used to enter alphanumeric and special character keys into WebElements like textareas and textboxes. The preceding method differs from WebElement's sendKeys(CharSequence) method. if the provided keys are null it will through IllegalArgumentException

 

Syntax:

sendKeys(CharSequence keysToSend) 

 

Conclusion

This article discussed a variety of selenium WebDriver commands that are often and excessively used by testers. Even correct syntax is explained with each command. All of these commands and methods for extracting data and sending it to web pages are based on the webpage's source code. 


We hope you found it informative and interesting! To learn more about Selenium, check out this webcast: How to Easily Record JMeter and Selenium Scripts with the BlazeMeter Chrome Extension.

   
arrowPlease enter a URL with http(s)

Interested in writing for our Blog?Send us a pitch!