How to get child nodes and their innertext using parent nodes xpath

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

How to get child nodes and their innertext using parent nodes xpath

mohit.khatri
This post was updated on .
I've xpath of parent node so from this, how can i get all child nodes and their inner text using webdriver?

HTML code is as below. I've id of div tag. Now i need all child nodes innertext.
<\div id = "xyz">
<\a href="...">link1
<\a href="...">link2
<\a href="...">link3
<\a href="...">link4
<\h1 class="xyz">
<\a href="...">linkA
<\/h1>
<\span class="abc">Text
<\/div>

I've used below code and it is working for me. Is it right way to get all child details or i need to modify the code?

List <WebElement> breadCrumb = driver.findElements(By.xpath("//*" + xPathValue + "/*"));
                                if (! breadCrumb.isEmpty()){
                                        breadCrumbValue = new String[breadCrumb.size()];
                                        for(int i = 0; i < breadCrumb.size(); i++){
                                                breadCrumbValue[i] = breadCrumb.get(i).getText();
                                        }
Reply | Threaded
Open this post in threaded view
|

Re: How to get child nodes and their innertext using parent nodes xpath

softwaretestingforum
Administrator
Can you modify it? Your html snippet is parsed as html
~ seleniumtests.com
Reply | Threaded
Open this post in threaded view
|

Re: How to get child nodes and their innertext using parent nodes xpath

mohit.khatri
I've modified the code snippet. Kindly have a look on that!
Reply | Threaded
Open this post in threaded view
|

Re: How to get child nodes and their innertext using parent nodes xpath

softwaretestingforum
Administrator
Looks good to me.
~ seleniumtests.com