在Selenium / Python中循环遍历div内的div

时间:2018-02-11 19:15:21

标签: python loops selenium xpath screen-scraping

我有这个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>

如何遍历所有容器并获取名称,姓氏(第二个div)和图像URL(img src)?到目前为止,我想出了这个:

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)

这应该有两个名字。但是,我得到了詹姆斯&#39;两次输出,没有&#39; Harry&#39;

谢谢!

2 个答案:

答案 0 :(得分:3)

尝试以下解决方案以获取所需的值

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'))

答案 1 :(得分:2)

使用<html>时,您将从根节点(.)开始搜索。在xpath之前使用for items in containers: name = items.find_element_by_xpath('.//div[@class="name"]') print(name.text) 从元素位置开始搜索

100% width