Python Selenium:遍历元素组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27006698/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 01:18:15  来源:igfitidea点击:

Selenium: Iterating through groups of elements

pythonhtmlseleniumbeautifulsouphtml-parsing

提问by AutomaticStatic

I've done this with BeautifulSoup but it's a bit cumbersome, and I'm trying to figure out if I can do it directly with Selenium.

我已经用 BeautifulSoup 做到了这一点,但它有点麻烦,我想弄清楚我是否可以直接用 Selenium 做到这一点。

Let's say I have the following HTML, which repeats multiple times in the page source with identical elements but different contents:

假设我有以下 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>

I need to build a dictionary where the entry for each person looks like:

我需要构建一个字典,其中每个人的条目如下所示:

dict = {'name' : 'John Smith', 'company' : 'SalesForce'}

I can easily get Selenium to produce a list of the contents of each top level element by doing:

通过执行以下操作,我可以轻松地让 Selenium 生成每个顶级元素的内容列表:

driver.find_elements_by_class_name('person')

But then I can't iterate through the list because the above method doesn't narrow the scope/source to just the contents of that element.

但是后来我无法遍历列表,因为上述方法并没有将范围/源缩小到该元素的内容。

If I try to do something like this:

如果我尝试做这样的事情:

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

I just get the same name over and over again.

我只是一遍又一遍地得到相同的名字。

I need to do this group by group because in my case, iterating through the whole page and appending each tag individually won't work (there's infinite scrolling, so it would be really inefficient).

我需要按组执行此操作,因为在我的情况下,遍历整个页面并单独附加每个标签是行不通的(无限滚动,因此效率非常低)。

Does anyone know whether it's possible to do this directly in Selenium, and if so how?

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

采纳答案by alecxe

Use find_elements_by_class_name()to get all blocks and find_element_by_xpath()to get titleand companyfor each person:

使用find_elements_by_class_name()让所有块,find_element_by_xpath()以获得titlecompany每个人:

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