Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Selenium:遍历元素组_Python_Html_Selenium_Beautifulsoup_Html Parsing - Fatal编程技术网

Python Selenium:遍历元素组

Python Selenium:遍历元素组,python,html,selenium,beautifulsoup,html-parsing,Python,Html,Selenium,Beautifulsoup,Html Parsing,我已经用BeautifulSoup做过了,但是它有点麻烦,我正在试图弄清楚我是否可以直接用Selenium来做 假设我有以下HTML,它在页面源代码中重复多次,元素相同,但内容不同: <div class="person"> <div class="title"> <a href="http://www.url.com/johnsmith/">John Smith</a> </div> <d

我已经用BeautifulSoup做过了,但是它有点麻烦,我正在试图弄清楚我是否可以直接用Selenium来做

假设我有以下HTML,它在页面源代码中重复多次,元素相同,但内容不同:

<div class="person">
    <div class="title">
        <a href="http://www.url.com/johnsmith/">John Smith</a>
    </div>
    <div class="company">
        <a href="http://www.url.com/company/">SalesForce</a>
    </div>
</div>
通过执行以下操作,我可以轻松地让Selenium生成每个顶级元素的内容列表:

driver.find_elements_by_class_name('person')
但是,我不能遍历列表,因为上面的方法没有将范围/源缩小到只包含该元素的内容

如果我尝试这样做:

people = driver.find_elements_by_class_name('person')
for person in people:
    print person.find_element_by_xpath['//div[@class="title"]//a').text
我只是一次又一次地得到同一个名字

我需要一组一组地做这项工作,因为在我的例子中,遍历整个页面并单独添加每个标记是不起作用的(有无限的滚动,所以效率很低)

是否有人知道是否可以直接在Selenium中执行此操作,如果可以,如何执行此操作?

用于获取所有块,并获取每个人的
标题和
公司

persons = []
for person in driver.find_elements_by_class_name('person'):
    title = person.find_element_by_xpath('.//div[@class="title"]/a').text
    company = person.find_element_by_xpath('.//div[@class="company"]/a').text

    persons.append({'title': title, 'company': company})

这很有道理,但它不起作用。请参阅我的OP中更新的示例代码,我相信它与您发布的代码相同。它一次又一次地返回相同的名称(我从列表中的第一个对象猜测)。这似乎并没有缩小范围…@AutomaticStatic发布后,我简短地更新了答案。出现错误(在循环中使用了
driver
而不是
person
)。请再查一下。谢谢。我正在做你写的事情(除了用打印语句检查它返回的内容),但它仍然一次又一次地返回相同的名称。是//而不是//表示它是一个孩子吗?抱歉,如果这是个愚蠢的问题。我逐渐熟悉xpath符号。@AutomaticStatic是的,这里的关键是点,我们说的是在元素范围内搜索的引擎。
persons = []
for person in driver.find_elements_by_class_name('person'):
    title = person.find_element_by_xpath('.//div[@class="title"]/a').text
    company = person.find_element_by_xpath('.//div[@class="company"]/a').text

    persons.append({'title': title, 'company': company})