Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 214
Mikel Avatar asked Feb 11 '18 19:02

Mikel


2 Answers

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'))
like image 121
Andersson Avatar answered Sep 29 '22 23:09

Andersson


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)
like image 45
Guy Avatar answered Sep 29 '22 22:09

Guy