Finding Child Elements in WebDriver using findElements

Finding Child Elements in WebDriver using findElements is like a cake walk for many but i am sure at very first instance it would have taken long time to reach to the solution and might be Selenium WebDriver Javadocs would have helped you.

So here is the scenario

1- First Search a string in google search
2- Print all the result coming as search result.

So here are two approach to achieve this

First approach :

Finding generic Xpath and fetching all element in a list using findElements method and then iterating in list and printing all the search result using getText() method…

So lets see how to achieve this generic xpath

1- Open
2- Search “I want child Element”
3- Right click on first search result and select inspect element. Screen something like this would be rendered
Right Click and Inspect Element4- Now right click on greyed out html code and Select Copy Xpath and paste it somewhere in notepad or it you want then in any text editor.
Like xpath for first element that i get is : //*[@id="rso"]/div[2]/div[1]/div/h3/a
5- Again go to second search result text and follow the step 3 and 4 and fetch the second xpath and xpath for second search result is : //*[@id="rso"]/div[2]/div[2]/div/h3/a
6- In the same fashion xpath for third search result would be : //*[@id="rso"]/div[2]/div[3]/div/h3/a

7- Find the similarity in xpath and also find html tag where some index/pattern is changing with each search result.
8- So what we got similar in all xpath <here i am going to divide the complete xpath in static and dynamic element>
So static parts are
and dynamic html tag is div[1], div[2], div[3] in between both static parts i.e. a) and b)
9- So remove the index of div that is varying each time.
So our xpath would be : //*[@id='rso']/div[2]/div/div/h3/a
Note: here we need to replace double quote.
10- So now we have a generic xpath and now we can write the code that could print all the search result one after another
so here is the code that would print all the search result

Result in console

Second Approach:  

This approach is going to take help of Selenium WebDriver Javadocs of WebElement . In This approach we would first take the all the parents elements first in list and would iterate in each element of list and would try to find the child on the basis of generic xpath for child

But before going ahead please read this part of Javadocs for FindElements

Find all elements within the current context using the given mechanism. When using xpath be aware that webdriver follows standard conventions: a search prefixed with “//” will search the entire document, not just the children of this current node. Use “.//” to limit your search to the children of this WebElement. This method is affected by the ‘implicit wait’ times in force at the time of execution. When implicitly waiting, this method will return as soon as there are more than 0 items in the found collection, or will return an empty list if the timeout is reached.
So lets see how to achieve this one:
1- Search the string in google search textbox
2- Press F12, developer tool should get rendered, Now go to magnify icon in developer tool and inspect the sub container in complete search result page that contains all the search result. Our search would end once we get result as belowFindElements
3- Further drill down and find another container that is going to have all the search result text..and like this
4- If we would further drill down then we would come to know that class=’rc’ is coming with each search result container.
5- So take it as pivot element as parent and fetch it in List using finElements(By.className("rc"))
6- Iterate in list and now figure out the generic html tag that has been used for search result text and concatenate finElement method with each element of each iteration.
so here are two way
a) Xpath : In this scenario h3 is tag that is being used for search result text
so xpath would be : //h3 <since we are inside smallest container>
Now here is one tricky thing that we need to note is to use “.” just before xpath inside to let the control know that only h3 with recent iteration element is being in use other wise it will take the first element each time  and would print first search result for all iteration. so to avoid this scenario code would look like this

b) if we are going to use any of other locator method then code would be like this
element.findElement(By.tagName("h3")).getText(); here element is iterated element
So lets see one code that would print all the text but here it will go first to parent and then it would go to child element..

One comment

Leave a Reply

Your email address will not be published. Required fields are marked *