23 Most Useful Selenium WebDriver Commands That You Must Know

In this Selenium WebDriver tutorial, I will show you the most commonly used Selenium WebDriver commands with simple examples so that you can learn them easily and use them effectively as per the need.

So here we go.

Top and Useful Selenium webdriver commands

 

What You Will Learn?

In order to perform any action on the browser or its elements like Button, Textbox, and Listbox, etc, the first thing that we have to do is to create the object of the Selenium Webdriver for the browser that you are using and the syntax to create the driver object is as follows.

For Chrome Browser:

WebDriver driver = new ChromeDriver();

For Firefox Browser:

WebDriver driver = new FirefoxDriver();

Selenium Webdriver Commands

We can categorize Selenium WebDriver commands in the following categories.

  • Browser commands
  • Navigation commands
  • WebElement commands
  • Action commands

Selenium WebDriver Browser Commands

Browser commands are those commands which can be performed on the browser itself to perform operation like opening a browser, closing a browser and getting page source and, etc. So here we go to see the Selenium WebDriver commands with examples.

1) get()How to Open a URL in Selenium WebDriver?

The get() method is used to open a URL in the current browser.

Syntax:

driver.get(WebAppURL);

Example


2) getCurrentUrl()
– How to read the URL of the Webpage in Selenium WebDriver?

The getCurrentUrl() method returns the current URL opened in the browser. It accepts nothing as an input parameter but returns a String.

Syntax:

driver.getCurrentUrl();

Example

This method is usually used to check if you have navigated to the correct page. We can use an assert statement to check the actual vs expected URL.

Syntax:

Assert.assertEquals(expectedURL, driver.getCurrentUrl());

Example


3) getTitle()
– How to get the title of the Webpage in Selenium WebDriver?

The getTitle() method returns the title of the current webpage. A null value is returned if the webpage has no title. It accepts nothing as an input parameter but returns a String.

Syntax:

driver.getTitle();

Example


4) getPageSource()
– How to read the page source of the WebPage in Selenium WebDriver?

The getPageSource() method is used to retrieve the source code of the current page. It accepts nothing as an input parameter but returns a String.

Syntax:

driver.getPageSource();

Example


5) close()
– How to close the Browser in Selenium WebDriver?

This method close() method is used to close only the current window the WebDriver is currently controlling. It neither accepts nor returns anything.

Syntax:

driver.close();

Example


6) quit()
– How to close all the Browser’s Window in Selenium WebDriver?

Unlike the close() method, the quit() method closes all windows opened by the WebDriver current session. It neither accepts nor returns anything.

Syntax:

driver.quit();

Example

Selenium WebDriver Navigation Commands

7) navigate() How to Navigate to URL on the Existing WebPage Opened by Selenium WebDriver?

The navigate() method open a new web page in the current browser window opened by Selenium WebDriver. It accepts a String parameter and returns nothing.

Syntax:

driver.navigate().to(NewWebPageURL);

Example


8) forward() and back()
 How to browse Forward and backward in Selenium WebDriver?

The forward() and backward() methods are used to move back and forth on an open window of Selenium WebDriver.The behavior of these methods is exactly as you are clicking the back and forward button of any web browser.

Syntax:

driver.forward();

Example

Selenium WebDriver WebElement commands

9) findElement() How to find an element/object in Selenium WebDriver?

Every object on the webpage like Button, Textbox, ListBox, Radio buttons, Headings, WebTables and etc are WebElement for Selenium WebDriver. In short, any HTML element is a WebElement.

Before performing any action on any WebElement first of all we have to get that WebElement.The findElement can be used to get the required WebElement.

Example:

WebElement element = driver.findElement(By.name(“UserName”));

To know more about getting/locating a WebElement please refer to my post All about 8 Locators in Selenium Webdriver with Examples.

9) findElements() How to find a list of the same type of Web elements in Selenium WebDriver?

This method returns a list of web elements matching with the same property.

Example:

List<WebElement> elements = driver.findElement(By.xpath(“//books”));

I will discuss this method in another post.

11) clear() How to clear the value of a TextBox in Selenium WebDriver?

This method is used to clear the value present in the text box. It does not accept any parameter and returns nothing as the return type is void.

Syntax:

element.clear();

Example

12) click() How to click on a Button/Link/WebEement in Selenium WebDriver?

This method is used to click on any WebElement. It does not accept any parameter and returns nothing as its return type is void.

Syntax:

element.click();

Example

13) submit() How to Submit a form in Selenium WebDriver?

This method can be used to submit a form by pressing the button.

Syntax:

element.submit();

Example

Note: If you see the type=Submit attribute in the buttons HTML Tag it’s better to use the submit method rather than as it works better.

For example, if you see the properties of the Google Search button you will see the following HTML tag attributes and you can easily notice it has an attribute “type=submit”

If you perform a click operation on the Google Search button it probably won’t work and you might get the following exception message in your Eclipse Console.

Exception in thread “main” org.openqa.selenium.ElementNotInteractableException: element not interactable

However, if you use the submit method it will display the search result based on the provided keywords in the search textbox.

14) sendKeys()How to set value in a TextBox in Selenium WebDriver?

This method can be used to set values in any text field element. It accepts String value as a parameter and returns nothing.

Syntax:

element.sendKeys(“textValue”);

Example

You can also set some value in a textbox and press the required keyboard key like sending the ENTER key together with it.

15) getText() How to get text value of an element in Selenium WebDriver?

This getText method returns the visible text of any web element. It accepts nothing as a parameter but returns a String value.

Syntax:

element.getText();

Example

16) isDisplayed() How to check whether an element is being displayed in Selenium WebDriver?

This method checks whether an element is currently being displayed on the page. It accepts nothing as a parameter but returns a boolean value that is true or false.

Syntax:

element.isDisplayed();

Example

Note: If the web element is present on the page but the property of the element is set to hidden in the DOM, in such case, it will return false value because the element is present in the DOM but not visible for the user.

17) isEnabled() How to check whether an element is  enabled in Selenium WebDriver?

This method lets you know whether an element is currently enabled or not on the page. If the element is enabled it returns true. It accepts nothing as an input parameter but returns a boolean value.

Syntax:

element.isEnabled();

Example

18) isSelected() How to check whether an element like CheckBox, Radio button is selected in Selenium WebDriver?

This method checks whether an element is currently selected or not. If the element is selected it returns true. It accepts nothing as an input parameter but returns a boolean value.

Syntax:

element.isSelected();

Example

19) getTagName() How to get Tag Name of an element in Selenium WebDriver?

This method returns the tag name of the current element. It accepts nothing as a parameter and returns a String value.

Syntax:

element.getTagName();

Example

20) getCssValue () – How to get the color and background color of a web element in Selenium WebDriver by getting cssValue?

This method is used to get the color of the web element and its background color. It accepts two types of parameters and returns an RGB color as a String value.

Color Parameter types:

  • color
  • background-color

Syntax:

element.getCssValue(colorParameter);

Example of getting element Color as well as Background color

OutPut

Color of login button: rgba(255, 255, 255, 1)
Background color of login button: rgba(24, 119, 242, 1)

21) getAttribute() How to get the desired attribute value of an element in Selenium WebDriver?

This method returns the attribute current element. It accepts String value as a parameter and returns a String.

Note: Attributes are properties of the element like id,name, class and etc

Syntax:

element.getAttribute(“id”);

Example: The following example will return the value of name attribute

22) getSize() How to get height and width of an element in Selenium WebDriver?

This getSize() method fetches the width and height of the web element. It accepts nothing as an input parameter but returns the Dimension object.

Syntax:

element.getSize();

Example: The following example shows how to get the height and width of an element.

23) getLocation() How to get the Location of an element in Selenium WebDriver?

This getLocation method locates the location of the element on the page. It accepts nothing as a parameter but returns the Point object.

Syntax:

element.getLocation();

Example: The following example shows how to get the X and Y Coordinate of an element.

Conclusion

We have learned various useful Selenium Webdriver commands.Hope now you could understand all these commands and you may be able to use them easily.

Recommended Posts

Leave a Reply