Home  >  Blog  >   Selenium

Selenium WebDriver Commands List

Rating: 5
  
 
11023
  1. Share:
Selenium Articles

Selenium WebDriver Commands

Selenium Webdriver is one of the most widely used open-source website automation tools. There are various sets of commands available in Selenium Webdriver to perform different operations. In this article, we’ll try to discuss the most commonly used Selenium Webdriver commands along with their syntax for your better understanding.

If you want to become a Selenium Certified Specialist, then visit Mindmajix - A Global online training platform: “Selenium Certification Course”. This course will help you to achieve excellence in this domain.

There are various methods available in the Webdriver interface. Using the instance variable driver we can access those methods through driver.methodName(). To view the methods offered by Webdriver, just open the Eclipse IDE loaded with Selenium Webdriver jar files, create a driver object for WebDriver, and press the dot key. 

Blog post image

Selenium Interview Questions

To understand the syntax of the methods, consider an example

Syntax of the methods

Method Name: First, we need to create an object of a class to access any method and then all public methods will be visible for the object.

Parameter: An argument that is passed to a method to perform a particular 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. 

 

 

 List of Selenium WebDriver Commands

Let us first know the list of the basic Selenium Webdriver commands used in Selenium followed by a detailed explanation of each.

The commands offered by Selenium Webdriver are broadly classified into the following categories:

1. Browser Commands

  • Get command 
  • Get Title command
  • Get Current URL command
  • Get Page Source Command
  • Close command
  • Quit command

2. Navigation Commands

  • Navigate to command
  • Back command
  • Forward command
  • Refresh commands

3. WebElement Commands

  • Clear command
  • Click command
  • GetText command
  • IsSelected command
  • IsEnabled command
  • IsDisplayed command
  • getSize command
  • GetCssValue command
  • getLocation command
  • Submit command
  • getAttribute command
  • gefTagName command
  • SendKeys command

MindMajix Youtube Channel

Also Read:  Ultimate Selenium RC Tutorial

Details of Selenium Webdriver commands List with Examples:

1) Browser Commands

The following are the most commonly used browser commands in selenium Webdriver:

  • Get command 

This command loads a new URL in the current browser window. It accepts a String as a parameter and returns void. The command takes a single string type parameter that is usually a URL of the application under test.

For the Selenium IDE users, the command may look very much like the open command.

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

For loading a new web page, the respective web command can be written as

driver.get(URL);      

// Or can also be written as

String URL = "URL";  
driver.get(URL);  

Example:  The command to load the official website can be written as below:

driver.get("https://www.mindmajix.com")  
  • Get Title command

This command is used to retrieve the title of the existing web page the user is working on. It accepts no parameter and returns a String. A null string is returned if the web page has no title. 

The respective command can be written as the following to fetch the title of the current web page:
driver.getTitle(); 

Or can also be written as

String Title = driver.getTitle();  
  • Get Current URL command

This command fetches the URL of the current web page. It doesn’t require any parameter and returns a String value.

Selenium Tutorial

The respective command to retrieve the string denoting the current URL can be written as:

driver.getCurrentUrl();

//Or can be writen as

String CurrentUrl = driver.getCurrentUrl();   
  • Get Page Source Command

This command is used to return the source code of the web page loaded on the current browser. It requires no parameter and returns a string value.

To get the page source of the page, it is written as:

driver.getPageSource();  

Or 

String PageSource = driver.getPageSource();  

The command can be used with various string operations like contains() to ascertain the presence of the specified string value

boolean result = driver.getPageSource().contains("String to find");
  • Close command

This command is used to terminate the current browser window at the current time.  If the current window is the only window operating by WebDriver, it terminates the browser as well. It accepts no parameters and returns void.

It can be written as:

driver.close();  
  • Quit command

This command is used to terminate all windows operating by WebDriver. It terminates all tabs as well as the browser itself. It accepts nothing as a parameter and returns void.

The respective command is written as:

driver.quit();  

 ------ Related Article: Selenium Commands With Examples  ------ 

2) Navigation commands

The navigation commands in WebDriver enable the browser to move forwards or backward in the browser’s history.

The following are the most commonly used Browser Navigation commands for Selenium WebDriver:

  • Navigate to command

For loading a new web page in the current browser window, this command is used. The string is accepted as a parameter and returns void.

To navigate/load a new web page, this command is written as:

driver.navigate().to("www.mindmajix.com");  
  • Back command

This command allows you to click on the back button in the current browser window. It neither accepts anything nor returns anything.

It takes you backward by one page on the browser’s history and written as:

driver.navigate().back(); 
  • Forward command

This command lets the web browser to click on the forward button in the current browser window. It neither accepts anything nor returns anything.

It takes you forward by one page on the browser's history and can be written as:

driver.navigate().forward(); 
  • Refresh commands

For refreshing/reloading the current web page in the existing browser window, this command is used. It neither accepts anything nor returns anything.

The respective command can be written as:

driver.navigate().refresh();  

 ------ Related Article: How to use WebDriver Backed Selenium  ------ 

3) WebElement commands 

Web element refers to an HTML element, which contains a start tag, an end tag and the content in between. We have various WebElement commands in Webdriver.

WebElement element = driver.findElement(By.id("UserName"));  
UserName is the value of the id attribute that is used as a unique identification for the desired web element.

The below mentioned are the most commonly used WebElement commands for Selenium WebDriver.

  • Clear command

This command has no effect on other elements. 

Command -  element.clear();

Text entry elements are INPUT  and TEXTAREA  elements. It can be written as a

WebElement element = driver.findElement(By.id("UserName"));  
element.clear();  

//Or can be written as          

driver.findElement(By.id("UserName")).clear(); 
  • Click command
Command: element.click(); 

This command helps you to interact with web elements like text elements, links, radio boxes and many more. It can be written as

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

//Or can be written as          

driver.findElement(By.linkText("javaTpoint")).click();  
  • GetText command

This command retrieves the visible innerText of the element. This accepts nothing as a parameter but returns a String value.

Command: element.getText();  

It returns an innerText of the element, including sub-elements, without any leading or trailing whitespace. It is written as:

WebElement element = driver.findElement(By.xpath("anyLink"));  
String linkText = element.getText();  
  • IsSelected command

This is used to determine whether or not the element is selected or not. It accepts nothing as a parameter but returns a boolean value(true/false).

Command: element.isSelected();  

This operation only applies to input elements such as Select Options,  Checkboxes, and Radio Buttons. Returns True if the element is currently selected or checked, otherwise false. It is written as 

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

//Or can be written as    

boolean staus = driver.findElement(By.id("Sex-Male")).isSelected();  
  • IsEnabled command

This command helps to determine if the current element is Enabled or not? It accepts nothing as a parameter but returns a boolean value(true/false).

Command: element.isEnabled();  

It can be written as

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

//Or can be written as      

boolean staus = driver.findElement(By.id("UserName")).isEnabled();  
  • IsDisplayed command

This command used to determine whether the element is currently being displayed or not. It accepts nothing as a parameter but returns a boolean value(true/false).

Command: element.isDisplayed();  

It is written as

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

//Or can be written as  

boolean staus = driver.findElement(By.id("UserName")).isDisplayed();
  • getSize command

This command is used for fetching the width and height of the rendered element. It accepts nothing as a parameter but returns the Dimension object.

Command: element.getSize();  

It returns the element size on the page and  is written as:

WebElement element = driver.findElement(By.id("SubmitButton"));  
Dimension dimensions = element.getSize();  
System.out.println("Height :" + dimensions.height + "Width : "+ dimensions.width);
  • GetCssValue command

This Command fetches the CSS property value of the given element. It accepts nothing as a parameter and returns a String value.

Command: element.getCssValue();  
  • getLocation command

This command is used for locating the element location on the page. It accepts nothing as a parameter but returns the Point object.

Command: element.getLocation();  

It is written as

WebElement element = driver.findElement(By.id("SubmitButton"));  
Point point = element.getLocation();  
System.out.println("X cordinate : " + point.x + "Y cordinate: " + point.y);
  • Submit command

This command works well/better than the click() if the current element is a form or an element within a form. It accepts nothing as a parameter and returns nothing.

Command: element.submit();  

If this causes the current page to change, then this method will wait until the new page is loaded. It is written as

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

//Or can be written as  

driver.findElement(By.id("SubmitButton")).submit();  
  • getAttribute command

This command is used to get the value of the given attribute of the element. It accepts the String as a parameter and returns a String value.

Command: element.getAttribute();  

It is written as

WebElement element = driver.findElement(By.id("SubmitButton"));  
String attValue = element.getAttribute("id"); //This will return "SubmitButton"?
  • gefTagName command

This command is used to get the tag name of the element. accepts nothing as a parameter and returns a String value.

Command: element.getTagName();  

It can be written as

WebElement element = driver.findElement(By.id("SubmitButton"));  
String tagName = element.getTagName();  

//Or can be written as 

 String tagName = driver.findElement(By.id("SubmitButton")).getTagName();  
  • SendKeys command

This command simulates typing into an element and accepts CharSequence as a parameter and returns nothing.

Command: element.sendKeys("text");  

It works fine with text entry elements like TEXTAREA and INPUT elements. The respective command is written as 

WebElement element = driver.findElement(By.id("UserName"));  
element.sendKeys("JavaTpoint");  ?

//Or can be written as  

driver.findElement(By.id("UserName")).sendKeys("JavaTpoint");  

Conclusion:

In this post, we tried our best to explain the various Selenium WebDriver commands. We hope they are helpful for you and let you work easily with Selenium.

Join our newsletter
inbox

Stay updated with our newsletter, packed with Tutorials, Interview Questions, How-to's, Tips & Tricks, Latest Trends & Updates, and more ➤ Straight to your inbox!

Course Schedule
NameDates
Selenium Training Mar 30 to Apr 14View Details
Selenium Training Apr 02 to Apr 17View Details
Selenium Training Apr 06 to Apr 21View Details
Selenium Training Apr 09 to Apr 24View Details
Last updated: 04 Apr 2023
About Author

 

Madhuri is a Senior Content Creator at MindMajix. She has written about a range of different topics on various technologies, which include, Splunk, Tensorflow, Selenium, and CEH. She spends most of her time researching on technology, and startups. Connect with her via LinkedIn and Twitter .

read more
Recommended Courses

1 / 15