In this blog post let us understand what is ByChained class.
Table of Contents
Introduction to ByChained class in selenium
In Selenium API ByChained is a predefined class which is part of package org.openqa.selenium.support.pagefactory
The ByChained class has a constructor which takes variable arguments of type By instance so that you can pass multiple instances of By
Working of ByChained object
Using ByChained object we can find the elements that are matched by the first argument, then their descendant using the subsequent parameters.
HTML Code to automate
<html> <body> <div id="div-01"> <input type="text" id="user-name-txt-field"/> </div> <div id="div-02"> <input type="text" id="user-name-txt-field"/> </div> </body> </html>
We need to write a code to locate user-name-txt-field which is inside div whose id is div-01.
Program demonstrating without ByChained class
Without using ByChained class we would have achieved like
WebElement div01Elmnt = driver.findElement(By.id("div-01")); WebElement userNameTxtElmnt = div01Elmnt.findElement(By.id("user-name-txt-field")); userNameTxtElmnt.sendKeys("Naveen");
Program demonstrating by using ByChained class
The same thing can be achieved using ByChained class by writing more efficient code and elegant code as shown below:
WebElement userNameTxtElmnt = driver.findElements(new ByChained(By.id("div-01"),By.id("user-name-txt-field") )); userNameTxtElmnt.sendKeys("Naveen");
About the course
NPN Training is counted among the top ten selenium training centers in Bangalore for providing the best Selenium training in Bangalore. Our selenium course content and syllabus is designed according to the student’s requirement to accomplish everyone’s career goal.