Selenium 4 Beginner Tutorial 2 | Web Element Locators | Relative Locators | Step by Step
Automation Step by Step Automation Step by Step
507K subscribers
32,738 views
0

 Published On Oct 21, 2021

All FREE courses - https://automationstepbystep.com/
00:00 Introduction
00:59 Web Elements
02:03 How to locate web elements
07:35 Locating single and multiple elements
11:03 Elements Selector Strategies
12:45 Tips on using selectors
15:13 Selenium 4 Relative Locators
20:11 Conclusion

Selenium Quiz - https://automationstepbystep.com/quiz...

Web Elements:
What are web elements
How to locate web elements in Selenium

Locating single & multiple elements
Locate element using any of its unique properties
WebElement searchBox = driver.findElement(By.name("q"));

Locate multiple elements
List<WebElement> options = driver.findElements(By.name("Optionwithcheck[]"));

for(WebElement element : options) {
System.out.println(element.getText());
}

Elements Selector Strategies
ID : using ID property of the element
Name : using Name property of the element
Link Text : using the visible text value of an anchor element
Partial Link Text : using the partial value of visible text of an anchor element
Class name : using Class name property of the element
Tag name : using Tag name property of the element
CSS Selectors : using a CSS selector with available properties
XPath : using XPath expression with available properties

Tips on using Selectors

ID : use ID if available, unique and consistent

CSS Selectors : If unique ID’s not available

XPath : If CSS Selectors can’t be used

keep your locators as compact and readable as possible
traversing the DOM structure is an expensive operation,
the more you can narrow the scope of your search, the better

Selenium 4 - Relative Locators

The findElement method accepts a new method with(By) which returns a RelativeLocator
above
below
toLeftOf
toRightOf
near

WebElement loginButton = driver.findElement(By.id("btnLogin"));
WebElement passwordTextBox = driver.findElement(RelativeLocator.with(By.tagName("input")).above(loginButton));

#Selenium4Tutorials
Stories by Raghav - https://automationstepbystep.com/stor...
My Udemy Courses - https://automationstepbystep.com/udem...

Every LIKE & SUBSCRIPTION gives me great motivation to keep working for you

You can support my mission for education by sharing this knowledge and helping as many people as you can

If my work has helped you, consider helping any animal near you, in any way you can.

Never Stop Learning
Raghav

show more

Share/Embed