DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Migrating from React Router v5 to v6: A Comprehensive Guide
  • Playwright: Filter Visible Elements With locator.filter({ visible: true })
  • React Callback Refs: What They Are and How to Use Them
  • Process Mining Key Elements

Trending

  • The Role of AI in Identity and Access Management for Organizations
  • Developers Beware: Slopsquatting and Vibe Coding Can Increase Risk of AI-Powered Attacks
  • Exploring Intercooler.js: Simplify AJAX With HTML Attributes
  • Implementing API Design First in .NET for Efficient Development, Testing, and CI/CD

Analysis of Selenium 4 Relative Locators

In the past, Relative Locators were called Friendly Locators. Check out the Selenium 4 Relative Locators in detail.

By 
Pallavi Singh user avatar
Pallavi Singh
·
Updated Feb. 20, 22 · Analysis
Likes (7)
Comment
Save
Tweet
Share
16.7K Views

Join the DZone community and get the full member experience.

Join For Free

Friendly Locators were previously known as Relative Locators. Relative Locators are used to track down a specific component or element in relation to another component's location. There are a total of five strategies and two arguments to choose from. We can use the Web Element component boundary or the by-finder border. A list of relative locators can be found below:

1) above(): Finds a component or set of components that are positioned above a valid component.

2) below(): Finds a component or set of components beneath a proper component.

3) near(): Finds a component (or a set of components) that are close to a fixed component.

4) toLeftOf(): Locates a component or components on the proper component's left side.

5) toRightOf(): Finds a component (or several components) on the right side of a valid component.

The current categories of Selenium's 8 Locators: id, className, CSS selector, linkText, name, partial link text, tagName, and XPath are connected via Relative Locators. One of the first tasks for an automation engineer is to figure out where to look for a component.

Import Relative Locator

Relative Locators has two import declarations. The first import declaration is static, but the second import declaration is not. The distinction influences the syntax of our Relative Locator.

We can use static in the import statement in one declaration and not in the other. Here's what our code would look like if we didn't use static in the import declaration:

Test package

The import declaration org.openqa.selenium.support.locators should be noted. RelativeLocator; This package was automatically loaded as a convenient solution in IntelliJ IDE. We can use the package to create a driver. findElement(RelativeLocator.withTagName(“”)). It's correct and will function normally. In any event, we can also begin our statement with TagName, as shown below:

If we remove RelativeLocator. before to withTagName("") and do not alter our import statement, an error stating "The technique withTagName(String) is undefined or not resolved for the type" will display.

We use static in the import statement before the bundle to figure out where the fault is coming from. Add a dot administrator (.) at the end, followed by (*) or withTagName(" "), as shown below:

web driver test package

Use of Relative Locator

In Selenium 4, the techniques for relative locators are over-used, and they can take relative WebElement or By locater as an input. The following code demonstrates how to utilize a relative locator:

Code for a relative locator

relative locator code

  • Xpath locates the Submit Button element.
  • The tag name input is used by Next Script to discover elements.
  • Above the submit button is the element input.
  • The message is finally typed into the Message Box.

Demo relative locators

Find List of Web Element Using Relative Locator

We can discover a list of web elements using relative locators in the same way that we can find a single element. With the use of relative locators, we are able to locate all of the social icons in our example:

finding social icons

Relative locators are a remarkable advancement in Selenium 4 that allows designers to access local web components with fewer lines of code. Because this is an Alpha release, the highlights may change in subsequent releases. Note that techniques like above, below, toLeftOf, toRightOf, and close in Selenium 4 don't function with covering components.

If you're using Selenium with Java for automated cross-program testing, you should check out Selenium 4 (Alpha). However, there are devices (both open-source and commercial) that offer benefits, such as Selenium 4 Relative Locator. Selenium 4 has a lot more features (including improved documentation) that make it worth the wait.

Element

Published at DZone with permission of Pallavi Singh. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Migrating from React Router v5 to v6: A Comprehensive Guide
  • Playwright: Filter Visible Elements With locator.filter({ visible: true })
  • React Callback Refs: What They Are and How to Use Them
  • Process Mining Key Elements

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!