Soo ive been trying to make a script that does stuff automatically for me on a website, the website have multiple labels that looks like this:
<label id="v3">This is v3</label>
<label id="v4">This is v5</label>
<label id="v6">This is v6</label>
the problem is that sometime this website will show 20 labels and other times only 13
i know for each label the number increase like this -> id="v1" then the next have id="v2" and i need to click the label with highest id (v1-20)
if i knew what would be the highest id i could use this:
driver.find_element_by_xpath("//label[@id='v20']").click()
but this is not an option as it will randomly be between v1 and v20 and i never know what the higest id will be only that it will never be higher than v20 and the lowest number will not always be 1
Do any one here know how i could use python/selenium to find the highest id present on the website and then click it?
CodePudding user response:
for i in range(20,0,-1):
try:
driver.find_element(By.XPATH,f"//label[@id='v{i}']").click()
break
except:
continue
You can also loop backwards from 20 and if you click on one than exit the loop. You may want to exit if it errors as well.