Find the element inside the element using @FindBy Annotation.

For instance,

WebElement parentEle = driver.findElement(By.id("xyz")); WebElement childEle = parentEle.findElement(By.id("abc")); childEle.click(); 

In the above example, we find childEle with in parentEle. How can we achieve this using the @FindBy annotation (along with PageFactory)

+6
source share
4 answers

First of all, why do you need to find a child from a parent if the child has a UNIQUE id? The main purpose of the identifier is to provide the flexibility of searching for an element using a unique selector.

If the child is actually nested in another, find it using xpath. I often use this.

 @FindBy(how = How.XPATH, using = "//something/something") private WebElement TestElement; 

Or with id

 @FindBy(how = How.ID, using = "abc") private WebElement TestElement; 
+1
source
 @FindBy (id = "abc") private WebElement TestElement; 

On your factory page, try this and call this method from your test method.

 public WebElement Child_Of_ParentElement(WebElement TestElement, String Child_Id) { return TestElement.findElement(By.id(Child Id)); } 
0
source

This can be achieved in two stages:

 //identify parent element with @FindBy annotation 1) @FindBy(className="locator") List<WebElement> parent; //loop through each parent to get child(<li> in your case) of each parent 2) for(WebElement list_items:Parent){ list_items.findElement(By.xpath("child locator")); 

I used this approach and was able to achieve the desired results. I hope this will explain your request.

0
source

As pointed out in other answers, I would use xpath, although you would have to rewrite the root path for each child if you did not do a string constant or something else, but that might get confusing.

 // Child 1 @FindBy(xPath = "//[@id='xyz']/[@id='abc']"; private WebElement childAbcOfParentXyz; // Child 2 @FindBy(xPath = "//[@id='xyz']/[@id='def']"; private WebElement childDefOfParentXyz; 
0
source

All Articles