Home > Mobile >  How to choose last element from class using Selenium Python
How to choose last element from class using Selenium Python

Time:08-25

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?

CodePudding user response:

Try to click on last element using xpath expression

driver.find_elements(By.XPATH, '//*[@]//a')[-1].click()

CodePudding user response:

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

  • Related