Issue
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.
Solution
//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.
Answered By - Arundeep Chohan
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.