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!
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'))
                        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)
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With