如何使用带有 Python 的 Selenium 从列表中获取特定元素
How to get a specific element from a list using Selenium with Python
列表示例:
<div class="c-article-metrics-bar__wrapper u-clear-both">
<ul class="c-article-metrics-bar u-list-inline">
<li class="c-article-metrics-bar__item">
<p class="c-article-metrics-bar__count">"277k "
<span class="c-article-metrics-bar__label">Accesses</span>
</p>
</li>
<li class="c-article-metrics-bar__item">
<p class="c-article-metrics-bar__count">"6 "
<span class="c-article-metrics-bar__label">Citations</span>
</p>
</li>
<li class="c-article-metrics-bar__item">
<p class="c-article-metrics-bar__count">"594 "
<span class="c-article-metrics-bar__label">Altmetric</span>
</p>
</li>
</ul>
</div>
我正在尝试从列表中获取 "Citations" 的计数。问题是,如果没有引用,列表元素将被省略。
我的想法是从 p 和 span 标签获取信息并将数据保存在两个列表中。然后遍历 span 标签列表,如果找到 "Citations" 字符串,则从 p 标签列表中该索引处的 return 文本,如下所示:
metrics_labels = self.wd.find_element_by_css_selector('span.c-article-metrics-bar__label')
labels = [label.text for label in metrics_labels]
metrics_counts = self.wd.find_element_by_css_selector('p.c-article-metrics-bar__count')
counts = [count.text for count in metrics_counts]
for i in range(len(labels) - 1):
if labels[i] == "Citations":
return counts[i]
但是它不起作用,我在这里缺少什么?
谢谢!
看起来你不是用 find_elements
收集所有元素,而是在这里使用 find_element
只收集 1 个:
metrics_labels = self.wd.find_element_by_css_selector('span.c-article-metrics-bar__label')
这里:
metrics_counts = self.wd.find_element_by_css_selector('p.c-article-metrics-bar__count')
要修复它,请尝试使用 find_elements_by_css_selector
:
metrics_labels = self.wd.find_elements_by_css_selector('span.c-article-metrics-bar__label')
labels = [label.text for label in metrics_labels]
metrics_counts = self.wd.find_elements_by_css_selector('p.c-article-metrics-bar__count')
counts = [count.text for count in metrics_counts]
希望这能奏效,祝你好运!如果没有帮助,请告诉我们您遇到的错误。
列表示例:
<div class="c-article-metrics-bar__wrapper u-clear-both">
<ul class="c-article-metrics-bar u-list-inline">
<li class="c-article-metrics-bar__item">
<p class="c-article-metrics-bar__count">"277k "
<span class="c-article-metrics-bar__label">Accesses</span>
</p>
</li>
<li class="c-article-metrics-bar__item">
<p class="c-article-metrics-bar__count">"6 "
<span class="c-article-metrics-bar__label">Citations</span>
</p>
</li>
<li class="c-article-metrics-bar__item">
<p class="c-article-metrics-bar__count">"594 "
<span class="c-article-metrics-bar__label">Altmetric</span>
</p>
</li>
</ul>
</div>
我正在尝试从列表中获取 "Citations" 的计数。问题是,如果没有引用,列表元素将被省略。
我的想法是从 p 和 span 标签获取信息并将数据保存在两个列表中。然后遍历 span 标签列表,如果找到 "Citations" 字符串,则从 p 标签列表中该索引处的 return 文本,如下所示:
metrics_labels = self.wd.find_element_by_css_selector('span.c-article-metrics-bar__label')
labels = [label.text for label in metrics_labels]
metrics_counts = self.wd.find_element_by_css_selector('p.c-article-metrics-bar__count')
counts = [count.text for count in metrics_counts]
for i in range(len(labels) - 1):
if labels[i] == "Citations":
return counts[i]
但是它不起作用,我在这里缺少什么?
谢谢!
看起来你不是用 find_elements
收集所有元素,而是在这里使用 find_element
只收集 1 个:
metrics_labels = self.wd.find_element_by_css_selector('span.c-article-metrics-bar__label')
这里:
metrics_counts = self.wd.find_element_by_css_selector('p.c-article-metrics-bar__count')
要修复它,请尝试使用 find_elements_by_css_selector
:
metrics_labels = self.wd.find_elements_by_css_selector('span.c-article-metrics-bar__label')
labels = [label.text for label in metrics_labels]
metrics_counts = self.wd.find_elements_by_css_selector('p.c-article-metrics-bar__count')
counts = [count.text for count in metrics_counts]
希望这能奏效,祝你好运!如果没有帮助,请告诉我们您遇到的错误。