Selenium:遍历元素组 [英] Selenium: Iterating through groups of elements

查看:11
本文介绍了Selenium:遍历元素组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经用 BeautifulSoup 完成了这件事,但它有点麻烦,我想知道我是否可以直接用 Selenium 来完成.

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

<div class="title"><a href="http://www.url.com/johnsmith/">约翰史密斯</a>

<div class="公司"><a href="http://www.url.com/company/">SalesForce</a>

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

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

我可以通过以下方式轻松地让 Selenium 生成每个顶级元素的内容列表:

driver.find_elements_by_class_name('person')

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

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

people = driver.find_elements_by_class_name('person')对于人在人:打印 person.find_element_by_xpath['//div[@class="title"]//a').text

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

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

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

解决方案

使用 find_elements_by_class_name() 获取所有块和 find_element_by_xpath() 为每个获取titlecompany人:

persons = []对于 driver.find_elements_by_class_name('person') 中的人:title = person.find_element_by_xpath('.//div[@class="title"]/a').textcompany = person.find_element_by_xpath('.//div[@class="company"]/a').textpeople.append({'title': title, 'company': company})

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.

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

<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:

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?

解决方案

Use find_elements_by_class_name() to get all blocks and find_element_by_xpath() to get title and company for each person:

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

这篇关于Selenium:遍历元素组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
前端开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆