python-3.x - 如何在 python 中使用 selenium 在变量中添加具有相同类的所有元素
问题描述
content = driver.find_element_by_class_name('topics-sec-block')
container = content.find_elements_by_xpath('//div[@class="col-sm-7 topics-sec-item-cont"]')
代码如下:
for i in range(0, 40):
title = []
url = []
heading=container[i].find_element_by_xpath('//div[@class="col-sm-7 topics-sec-item-cont"]/a/h2').text
link = container[i].find_element_by_xpath('//div[@class="col-sm-7 topics-sec-item-cont"]/a')
title.append(heading)
url.append(link.get_attribute('href'))
print(title)
print(url)
它给了我 40 行,但所有行的标题和 url 都相同(其中一些在下面给出):
['Stuck in Mexico: Central American asylum seekers in limbo']
['https://www.aljazeera.com/news/2020/03/stuck-mexico-central-american-asylum-seekers-limbo-200305103910955.html']
['Stuck in Mexico: Central American asylum seekers in limbo']
['https://www.aljazeera.com/news/2020/03/stuck-mexico-central-american-asylum-seekers-limbo-200305103910955.html']
解决方案
推荐阅读
- git - 仅在 git log 中显示标记的提交
- python - 根据另一个数据框的列名和索引值填充数据框
- python - 如何使用 Python3 获取当前日期的 EOMONTH(月末)
- android - 从 webview 打印 pdf.js 生成的文档
- javascript - 如何处理流星呼叫内或呼叫后的事件?
- macos - 如何让 /usr/bin/mail 在 Mac 10.14 上通过 gmail 发送邮件?
- html - 通过查询插入的时间在几分钟后显示更多数字
- java - 如何使用 apache camel 从 elasticseach 获取所有条目?
- codeigniter - 发送带有个性化正文的邮件
- pdf - 为什么我的自定义 URI 在浏览器栏中有效,但在 PDF 链接中无效?