Advertisement

TrimwebsolutionsTrimwebsolutions

Problem and Solutions

Skip clicking on an element in the While loop, when opening a popup with selenium. Problem with While loop in python

By M.K. Verma · 30 April 2022

Skip clicking on an element in the While loop, when opening a popup with selenium. Problem with While loop in python

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