Python Forum
How to find element in a deeply nested html structure
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to find element in a deeply nested html structure
#1
Hi, I have a deeply nested element (the 11.7 value underlined) as shown below. May I know how can I find this element?


<div class="col-lg-6 ">
<div class="form-group ">
<label>Size</label>

<span data-val controltovalidate="MainPlaceHolder_Size " data-val-focusOnError="t " data-val-errormessage="(Required field*)" id="MainPlaceHolderFormCtrl_Size " data-val="true " data-val-evaluationfunction="RequiredFieldValidatorEvaluateIsValid " data-val-initialvalue="" style="color:Red;visibility:hidden;">(Required field*)</span>
<input name="MainPlaceHolderContentPlaceHolder" type="text " id="MainPlaceHolder_Size " value="N.A " class="form-control " style="display:none;" />
<input name="MainPlaceHolderContentPlaceHolderSize " type="text " id="MainPlaceHolder_ContentPlaceHolderSize " class="form-control " value="11.7 " />
</div>
</div>
</div>

I have tried finding element by ID,class name,XPath but nothing works..
Reply
#2
Please show URL, need to see full context of webpage.
Reply
#3
(Oct-04-2023, 10:20 AM)Larz60+ Wrote: Please show URL, need to see full context of webpage.

Sry I cant share the website as it requires a login access but I can share a snippet of the code section I extracted from Chrome developer tools.
[Image: Capture.jpg]
I have successfully logged in on Python but cant seemed to extract the element containing the value 11.7 as underlined in red as shown in the snippet above. I tried finding element by id, class, and even input name but to no avail..
Reply
#4
Please, at least include the snippit as text that can be copied (need to test our suggestions)
It's too much to type from scratch as there are so many threads that we need to look at.
Reply
#5
(Oct-06-2023, 08:26 AM)Larz60+ Wrote: Please, at least include the snippit as text that can be copied (need to test our suggestions)
It's too much to type from scratch as there are so many threads that we need to look at.

Here is the text version of the snippet:
<div class="col-lg-6 ">
<div class="form-group ">
<label>Size* </label>
<span data-val-controltovalidate="MainPlaceHolderSize “ data-val-focusOnError="t " data-val-errormessage="(Required field*)" id="MainPlaceHolderFormCtrl_Size " data-val="true " data-val-evaluationfunction="RequiredFieldValidatorEvaluateIsValid " data-val-initialvalue="" style="color:Red;visibility:hidden;">(Required field*)</span>
<input name="ctl00$ctl00$MainPlaceHolderContentPlaceHolder" type="text " id="MainPlaceHolder_SIze" value="N.A " class="form-control " style="display:none;" />
<input name="ctl00$ctl00$MainPlaceHolderSize " type="text " id="MainPlaceHolder_ContentPlaceHolderSize " class="form-control " value="11.7 " />
</div>
</div>



Thank you very much in advance!
Reply
#6
You don't need all of this code, but there's some additional to help you understand how to parse similar html in the future:
from bs4 import BeautifulSoup

data = '''<div class="col-lg-6 ">
<div class="form-group ">
<label>Size* </label>
<span data-val-controltovalidate="MainPlaceHolderSize “ data-val-focusOnError="t " data-val-errormessage="(Required field*)" id="MainPlaceHolderFormCtrl_Size " data-val="true " data-val-evaluationfunction="RequiredFieldValidatorEvaluateIsValid " data-val-initialvalue="" style="color:Red;visibility:hidden;">(Required field*)</span>
<input name="ctl00$ctl00$MainPlaceHolderContentPlaceHolder" type="text " id="MainPlaceHolder_SIze" value="N.A " class="form-control " style="display:none;" />
<input name="ctl00$ctl00$MainPlaceHolderSize " type="text " id="MainPlaceHolder_ContentPlaceHolderSize " class="form-control " value="11.7 " />
</div>
</div>'''

def find_value(data):
    soup = BeautifulSoup(data, "html.parser")
    itags = soup.find_all("input")
    value = itags[1].get('value')
    print(f"value: {value}")

find_value(data)


# you don't need this one, but may help you to understand breakdown
def show_elements(data):
    soup = BeautifulSoup(data, "html.parser")
    print(f"\nlist of all tags:\n{[tag.name for tag in soup.find_all()]}")
    itags = soup.find_all("input")
    print(f"\nattributes of 2nd input statement:\n{list(itags[1].attrs)}")

show_elements(data)
Output:
value: 11.7 list of all tags: ['div', 'div', 'label', 'span', 'input', 'input'] attributes of 2nd input statement: ['name', 'type', 'id', 'class', 'value']
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Selenium suddenly fails to find element Pavel_47 3 6,335 Sep-04-2022, 11:06 AM
Last Post: Pavel_47
  HTML multi select HTML listbox with Flask/Python rfeyer 0 4,652 Mar-14-2021, 12:23 PM
Last Post: rfeyer
  Find element using xpath engli 3 3,970 May-20-2020, 08:56 AM
Last Post: Larz60+
  find element...click() dont work windows11 6 3,179 Apr-23-2020, 11:13 PM
Last Post: law
  Find Dynamic Input Element Class Problem wazacko 0 1,615 Apr-03-2020, 11:46 AM
Last Post: wazacko
  bypassing find element click() windows11 1 1,899 Apr-02-2020, 11:55 AM
Last Post: Larz60+
  How find link element zinho 3 3,128 Mar-31-2020, 12:29 PM
Last Post: snippsat
  Python3 + BeautifulSoup4 + lxml (HTML -> CSV) - How to loop to next HTML/new CSV Row BrandonKastning 0 2,377 Mar-22-2020, 06:10 AM
Last Post: BrandonKastning
  How to find which frame/iframe my element belongs to? smaria 1 2,073 Nov-18-2019, 09:21 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020