Mikel
Mikel

Reputation: 103

Loop through divs inside div in Selenium/Python

I have this HTML:

<div class="container">
    <div class="name">James</div>
    <div>Rodriguez</div>
    <div class="image">
        <div><img src="https://example.com/1.jpg"></div>
    </div>
</div>
<div class="container">
    <div class="name">Harry</div>
    <div>Kane</div>
    <div class="image">
        <div><img src="https://example.com/2.jpg"></div>
    </div>
</div>

How do I loop through all containers and get name, surname (second div) and image URL (img src)? So far I came up with this:

items = []

containers = driver.find_elements_by_xpath('//div[@class="container"]')

for items in containers:
    name = items.find_element_by_xpath('//div[@class="name"]')
    print(name.text)

This should give two names. However, I'm getting 'James' twice as output, and no 'Harry'.

Thanks!

Upvotes: 8

Views: 16417

Answers (2)

Andersson
Andersson

Reputation: 52675

Try below solution to get required values

for item in containers:
    name = item.find_element_by_xpath('.//div[@class="name"]')
    surname = name.find_element_by_xpath('./following-sibling::div')
    image = surname.find_element_by_xpath('./following::img')
    print(name.text, surname.text, image.get_attribute('src'))

Upvotes: 9

Guy
Guy

Reputation: 50899

When using // you are starting the search from the root node (<html>). Use . before the xpath to start the search from the element location

for items in containers:
    name = items.find_element_by_xpath('.//div[@class="name"]')
    print(name.text)

Upvotes: 8

Related Questions