Selenium Special locators
ByAll
The ByAll locator is a Selenium WebDriver locator that can be used to locate web elements using multiple locator strategies.
Looking for the element using locators given is start from left to right.
The ByAll locator will stop searching further as soon as the element is found in the first matching locator.
For instance, as per below example, if element is not found using “name”, then search using “id” only. If element is found using “id”, it won’t search using xpath to save the execution time.
WebElement username= driver.findElement(new ByAll(By.name("email"),By.id("email"),By.xpath("//input[@id='email']")));
Element
<input class="s-input" id="email" type="email" size="30" maxlength="100" name="email">
Here are some of the advantages of using the ByAll locator:
Can be used to locate elements that are not uniquely identifiable by a single locator strategy.
Can be used to improve the robustness of your tests by making them less reliant on a single locator strategy.
Can be used to make your code more readable and maintainable by reducing the need to use multiple locator strategies in the same test.
Here are some of the disadvantages of using the ByAll locator:
Can be slower than using a single locator strategy, as the browser has to evaluate all of the locator strategies in order to find the element.
Can be more difficult to debug, as it can be difficult to determine which locator strategy is not working correctly.
Can be less flexible than using multiple locator strategies in the same test, as you cannot use different locator strategies for different elements in the same test.
ByIdOrName
locator strategy that allows you to find an element by either its ID or its name attribute.
WebElement password=driver.findElement(new ByIdOrName("password"));
password.sendKeys("password");
Element
<input class="flex--item s-input" type="password" autocomplete="off" name="password" id="password">
ByChain
Complete parent and child should be used.
WebElement submitButton= driver.findElement(new ByChained(By.cssSelector("button.flex--item.s-btn.s-btn__primary#submit-button"), By.id("submit-button")));
Above one is just an example, it won’t run since the locators are just dummy values.
Refer- https://www.youtube.com/watch?v=-SbjeEBcfSg&list=WL&index=20&t=2s&ab_channel=NaveenAutomationLabs