SELENIUM BASICS:


SELENIUM WEBDRIVER:


TESTNG:


REPORTS:


BUILD & CONTINUOUS INTEGRATION TOOLS:


CROSS BROWSER TESTING:


MOBILE TESTING:


AUTOMATION FRAMEWORKS:


API Testing:


INTERVIEW QUESTIONS:


SAMPLE RESUME:

What Are Selenium Relative Locators And How To Use Them

In this Selenium 4 series, we will dive into Selenium Relative locators. 

What are Relative Locators in Selenium 4?

Relative Locators in Selenium formerly known as Friendly Locators. 

In test automation, the automation tester our main task while writing test scripts for web applications is to locate web elements. We already know the existing Selenium locators (id, name, className, linkText, partialLinkText, tagName, cssSelector, xpath). You can learn about Selenium locators here. The point to be noted here is sometimes we may not find elements using these locators and to overcome that we use JavaScriptExecutor. You can learn about JavaScriptExecutor in Selenium here.

In Selenium 4.0, a new locator added to the locator’s list i.e., Friendly locators and later it is renamed as Relative locators.

It helps us to locate the web elements by its position by concerning other web elements such as above, below, toLeftOf, toRightOf, and near.

In simple words, relative locators allow us to locate the web elements based on their position with respect to other web elements.

There are five locators newly added in Selenium 4:

above()

It is to locate a web element just above the specified element

below()

It is to locate a web element just below the specified element

toLeftOf()

It is to locate a web element present on the left of a specified element

toRightOf()

It is to locate a web element present on the right of a specified element

near()

It is to locate a web element at approx. 50 pixels away from a specified element. The distance can be passed as an argument to an overloaded method.

Note: Method “withTagName()” is added which returns an instance of RelativeLocator. Above relative locators support this method “withTagName()”

Practical Example Selenium Relative Locators 

Software Testing Material Home Page

STEP 1: Import RelativeLocator ‘withTagName’

import static org.openqa.selenium.support.locators.RelativeLocator.withTagName;

STEP 2: Launch a web browser and navigate to https://www.softwaretestingmaterial.com/

STEP 3: Locate the web element ‘Your Name’ text field

STEP 4: Locate the web element ‘Your Email’ text field

STEP 5: Locate ‘Your Email’ text field which is to the left of ‘Your Name’ text field and enter the text

STEP 6: Locate ‘Your Name’ text field which is to the right of ‘Your Email’ text field and enter the text

STEP 7: Locate the web element ‘Selenium Tutorial’ and open it

STEP 8: Locate the web element ‘Java Tutorial’ and open it

STEP 9: Close the browser to end the program

Related Posts:

Get our latest blog posts delivered to your inbox

Subscribe and get popular blog posts about software testing industry.

Rajkumar

Leave a Comment

Share via
Copy link
Powered by Social Snap