Skip to content Skip to sidebar Skip to footer

Using Python + Selenium To Click On Next Page With While Loop

I'm trying to move through different pages on a website called iens. I'm using selenium + python to click on 'volgende' (which means 'next' in dutch), but I want my program to keep

Solution 1:

Because of automatic scrolling each time you switch to next page, webdriver tries to click Next button, but some other element receives click. You can use this solution:

while True:
    try:
        click_icon = WebDriverWait(driver, 5, 0.25).until(EC.visibility_of_element_located([By.LINK_TEXT, 'Volgende']))
        click_icon.click()
        WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.CSS_SELECTOR, 'main:not([style*="margin-top"])')))
    except:
        break

Another simple solution (only if your goal is to reach last page) is to get last page without clicking Next all the time:

driver.find_elements_by_xpath('//div[@class="pagination"]/ul/li/a')[-2].click()

Post a Comment for "Using Python + Selenium To Click On Next Page With While Loop"