How to choose last element from class using Selenium Python

Question:

I need to parse prices from

enter image description here

I want to take the maximum possible period. But when I use

enter image description here

driver.find_elements(By.CLASS_NAME, 'btn-group')[-1].click()

I get 15 years.

How I can fix it?

Can I additionally upload all these elements to the list and select the last one from them?

Asked By: I_B_O

||

Answers:

Looks like you need to put some delay before the

driver.find_elements(By.CLASS_NAME, 'btn-group')

command.
It seems that you are grabbing the elements before all of them are loaded properly.
So, I think

time.sleep(2)
driver.find_elements(By.CLASS_NAME, 'btn-group')[-1].click()

should work better

Answered By: Prophet

Try to click on last element using xpath expression

driver.find_elements(By.XPATH, '//*[@class="btn-group"]//a')[-1].click()
Answered By: F.Hoque