you should approach catching the exception and, Instead of the try-else
wrapping the while
iteration, do it the other way round.
while i <= limit:
try:
row = wait.until(EC.visibility_of_element_located((By.XPATH, f"(//div[@data-visualcompletion='ignore-dynamic' and not (@role) and not (@class)])[{i}]")))
add = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Add Friend']"))).click()
driver.execute_script("arguments[0].scrollIntoView(true);", row, add)
time.sleep(delay)
except ElementClickInterceptedException:
print("A button cannot be pressed. You go to the next button ...")
close_popup = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Ok']"))).click()
i = i + 1
* Be the first to Make Comment