Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to recieve inner HTMl of a child node in selenium (python)?

I’m trying to iterate through multiple nodes and recieve various child nodes from the parent nodes. Asuming that I’ve something like the following structure:

<div class="wrapper">
    <div class="item">
        <div class="item-footer">
            <div class="item-type">Some data in here</div>
        </div>
    </div>
    <!-- More items listed here -->
</div>

I’m able to recieve all child nodes of the wrapper container by using the following.

wrapper = driver.find_element(By.XPATH, '/html/body/div')
items = wrapper.find_elements(By.XPATH, './/*')

Anyways I couldn’t figure out how I can now recieve the inner HTML of the container containing the informations about the item type. I’ve tried this, but this didn’t work.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

for item in items:
    item_type = item.item.find_element(By.XPATH, './/div/div').get_attribute('innerHTML')
    print(item_type)

This results in the following error:

NoSuchElementException: Message: Unable to locate element:

Does anybody knows how I can do that? I highly appreciate any kind of help, sheers!

>Solution :

In case all the elements their content you want to get are div with class attribute value item-type located inside divs with class attribute value item-footer you can simply do the following:

elements =  driver.find_element(By.XPATH, '//div[@class="item-footer"]//div[@class="item-type"]')
for element in elements:
    data = element.get_attribute('innerHTML')
    print(data)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading