Example of a list:
<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>
I am trying to get count of "Citations" from the list. The problem is, if there are no citations, the list element will be omitted.
My idea was to get info from p and span tags and save data in two lists. Then iterate through the span tag list, and if the "Citations" string is found, return text at that index from p tag list, like this:
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]
But it doesn't work, what I am missing here?
Thanks!
It looks like instead of collecting all elements with find_elements
you are only collecting 1 by using find_element
here:
metrics_labels = self.wd.find_element_by_css_selector('span.c-article-metrics-bar__label')
and here:
metrics_counts = self.wd.find_element_by_css_selector('p.c-article-metrics-bar__count')
To fix it, try using 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]
I hope this works, good luck! If it doesn't help, please let us know the error you are getting.