How to access elements within a web element in Selenium?

Viewed 159

I have a list of web elements that I defined as follows:

sellersList = browser.find_elements_by_class_name('gig-card-layout')

Each web element looks like this:

<div class="gig-card-layout">
    <div>
        <div class="gig-wrapper card" data-gig-id="gig_id" data-impression-collected="true">
            <a href="ref_link" target="_blank" class="media">...</a>
        <div class="seller-info text-body-2">...</div>
        <h3 class="text-display-7>...</h3>
        <footer>
            <a href="ref_link" target="_blank" class="price" title="$50">...</a>
        </footer>
        </div>
    </div>
</div>

I would like to access the price text located in the footer of each web element using a for loop.

How could I do that?

1 Answers

You can get the price elements directly using the below snippet.

# get all price elements
priceElems = driver.find_elements_by_css_selector(".gig-card-layout  footer a")

# iterate through all price elements and print the price
for priceElem in priceElems:
    print(priceElem.get_attribute('title'))

If you want to use the sellersList and iterate through the list then you can do the below

for seller in sellersList:
    print(seller.find_element_by_xpath(".//footer/a").get_attribute('title'))
Related