while True: elm = driver.find_element_by_class_name('next') if 'inactive' in elm.get_attribute('class'): break; elm.click()