Home > Back-end >  Selenium get only child elements from current parent
Selenium get only child elements from current parent

Time:02-10

I'm trying to get data from an HTML like this:

<div>
  <h4 id='id1'>...</h4>
  <ul>
    <li></li>
    <li></li>
  </ul>
</div>
<div>
  <h4 id='id2'>...</h4>
  <ul> ... </ul>
</div>

The goal is to get the <li> values from every <h4>. To get this I've tried something like this:

divs = driver.find_elements_by_xpath("//div//h4[starts-with(@id,'id_')]")
for h4 in divs:
    title = h4.text
    # Get <li> from each div
    for value in h4._parent.find_elements_by_tag_name('li'): #<-- It gives me all <li> in the page
        # TODO ...

Here I'm trying to get all <h4> tags and then go to the parent (the <div>) and find the <li> tags existing only in that parent. But I retrieve all <li> tags.

I've searched over the internet and I've found a couple of question in StackOverflow like Get child element using xpath selenium python or selenium find child's child elements where it says to set the context, so I've tried this:

for value in h4._parent.find_elements_by_xpath('.//li'):
                                                 ^

But it gives me the same numbers of elements.

So, I'm misunderstanding something?

Thanks in advance.

CodePudding user response:

//div[./h4[starts-with(@id,'id')]]//li

To get all li's of all div that contains an element h4 with a certain id starting try this.

//div[./h4] basically means div with h4 element inside 1 layer deep.

CodePudding user response:

Arendeep question is ok and it worked for me, but also I've noticed the problem I was having.

The element _parent seems to be the web page, not the parent element. And this is why the method find_element was getting all <li> tags from the page.

I can use accepted answer or also:

parent = h4.find_element_by_xpath('..')
for value in parent.find_elements_by_tag_name('li'):
    # TODO

Where the xpath ('..') return the parent element.

This way gives me only child from the current element (maybe is an answer more accurate) but accepted answer also works on my scenario where I want all <li> tags dependant from the <h4>.

By the way I haven't found docs about _parent.

  • Related