Loop through divs inside div in Selenium/Python
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)