I am creating a program that uses selenium to extract data from a website. In this case I'm trying to get the length of the lesson. I have the following code on a webpage
<li class="">
<a href="/academy/lesson/intro-to-personality.html" data-virtual="3">
<span class="lesson__title">Intro to Personality</span></a>
<span class="lessonTime">4:16</span>
</li>
<li class="">
<a href="/academy/lesson/intro-to-real-estate.html" data-virtual="3">
<span class="lesson__title">Intro to Real Estate</span></a>
<span class="lessonTime">6:16</span>
</li>
<li class="is-viewing" test-id="course_nav_current_lesson">
<a href="/academy/lesson/freudian-defense-mechanisms.html" data-virtual="3">
Freudian Defense Mechanisms: Definition, Levels & Examples</a>
<span class="lessonTime">7:29</span>
<span class="icon-eye"></span>
</li>
The webpage contains a list of around 8-15 other lessons length's but I'm only interested in the one that the user is assigned. Since it won't visit the same webpage every time the xpath will be dynamic. The only two things that are consistent is the fact that the lesson that the user has been assigned is always at the bottom and the fact that it has a special class called is-viewing. I tried using this code to extract the data:
lessonlength = driver.find_element_by_class_name('lessonTime').get_attribute('innerHTML')
print(f'Lesson is {lessonlength} long')
This returns 4:16 since it's the first element with the same class as the one defined. Is there a way to get the last class found (since it's always at the end).
Try this one:
result = driver.find_element_by_css_selector(".is-viewing>.lessonTime").text
is-viewing - is unique class and you need its child class named lessonTime
Also, add wait for it to be shown.
You can find all elements into a list and then loop through the list:
lessonlengthList = driver.find_elements_by_class_name('lessonTime')
for l in lessonlengthList:
print(f'Lesson is {l.text} long')
or get the last element:
lessonlengthList = driver.find_elements_by_class_name('lessonTime')
print(f'Lesson is {lessonlengthList[-1].get_attribute('innerHTML')} long')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With