We use cookies to analyze site traffic and show personalized ads. You can accept all cookies or decline personalized advertising.
Learn more in our Privacy Policy.
Advanced locator strategies for complex or dynamic pages.
Sometimes simple locators (single By.id, By.cssSelector, etc.) are not enough, especially on complex pages with nested structures or changing attributes. Selenium offers two helper locator classes to combine locators:
ByChained – chain multiple locators (parent → child → grandchild…).ByAll – match elements that satisfy any of several locators (OR logic).These are in org.openqa.selenium.support.pagefactory (or org.openqa.selenium.support in newer versions).
new ByChained(By.id("form-1"), By.tagName("button"))
What it does
By.id("form-1").By.tagName("button").id="form-1", then find a button inside it".When to use it
new ByAll(By.id("btn-submit-1"), By.className("cancel-btn"))
What it does
id="btn-submit-1" or any element with class="cancel-btn".When to use it
findElements and filtering/inspection in code.Practice Area: Two Forms with Buttons (ByChained & ByAll Demo)
On this practice page, you'll see two simple forms and practice buttons. Use your Selenium scripts to interact with these actual elements!
Login-style form
Important attributes:
id="form-1"id="btn-submit-1"form-1 → buttonSecondary form
Important attributes:
id="form-2"class="cancel-btn"btn-submit-1 OR cancel-btnClick these buttons to see which elements the locators would target:
driver.findElement(new ByChained(By.id("form-1"), By.tagName("button"))) to locate the Submit button in Form 1.driver.findElements(new ByAll(By.id("btn-submit-1"), By.className("cancel-btn"))) to locate both the Submit and Cancel buttons.sendKeys() and clicking the buttons using click().12345678910111213141516171819202122By chainedLocator = new ByChained(
By.id("form-1"),
By.tagName("button")
);
WebElement submitBtn = driver.findElement(chainedLocator);
System.out.println("Found button in Form 1: " + submitBtn.getText());
submitBtn.click();
By allLocator = new ByAll(
By.id("btn-submit-1"),
By.className("cancel-btn")
);
List<WebElement> buttons = driver.findElements(allLocator);
System.out.println("Found " + buttons.size() + " buttons matching either criteria.");
for (WebElement button : buttons) {
System.out.println("Button text: " + button.getText());
}
driver.quit();id="form-1".button elements.findElement to get the button inside Form 1."Submit"), useful for logging and verification.id="btn-submit-1", orclass="cancel-btn".findElements to get all elements matching either locator.Use ByChained when you want to limit your search to a specific container (form, card, modal). It reduces the chance of hitting the wrong element in a large page.
Use ByAll as a fallback strategy when an element might be identified in more than one way (ID in some builds, class or name in others).
Stick with simple locators first (id, cssSelector, XPath). Reach for ByChained and ByAll only when you truly need extra flexibility or robustness.