问题描述
我遇到了一个难以理解的问题.我有一个带有ul列表的HTML,其中包含一堆li项目,在这些li项目中,它们包含我想在Python上使用Selenium抓取的元素.网站组成如下
I'm having an issue that I'm struggling to understand. I have a HTML with a ul list with a bunch of li items, and within those li items they have elements that I would like to scrape using Selenium on Python. The website makeup is as follows
<ul id="results">
<li>
<div class="name">John</div>
<div class="age">23</div>
</li>
<li>
<div class="name">Bob</div>
<div class="age">39</div>
</li>
..... #more li
</ul>
因此,这似乎是一个非常简单的问题,我们只将li元素保存为变量,然后遍历每个列表以保存信息.问题是,无论我做什么,我的结果总是一遍又一遍地返回第一个列表项.它将循环遍历正确数量的元素,但始终返回第一个.因此,如果我执行以下操作
So this seems like a pretty simple problem where we just save the li elements as a variable, and iterate through each list to save the information. The problem is that no matter what I do, my results always gives back the first list item over and over again. It will loop through the correct number of elements, but always refer back to the first one. So if I do the following
results = driver.find_elements_by_xpath("""//*[@id="results"]/li""")
for result in results:
name = result.find_element_by_xpath("""//*[@class="name"]""").text
print(name)
现在,在这种情况下,如果有10个li元素,则名称"John"将只打印10次,而不是根据迭代列表进行更新.
Now if there is 10 li elements in this particular case, the name "John" will just print out 10 times rather than updating based on the iterated list.
推荐答案
您用于第二次搜索的XPath不正确.它必须以.
开头.否则,它将从顶部开始搜索.这就是为什么它总是找到第一个项目的原因.参见下面的示例.
Your XPath for the 2nd search is incorrect. It must begin with .
. Otherwise, it will start searching from the top. That's the reason why it always find the first item. See my example below.
results = driver.find_elements_by_xpath('//*[@id="results"]/li')
for result in results:
name = result.find_element_by_xpath('.//*[@class="name"]').text
print(name)
这篇关于遍历元素在Selenium上的Python上得到重复的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!