How can I make Selenium click on the “Next” button until it is no longer possible?

前端 未结 1 976
梦如初夏
梦如初夏 2021-01-14 12:01

I would like to write a code that would make Python scrape some data on a page, then click on the \"next\" button at the bottom of the page, scrape some data on the second p

1条回答
  •  清酒与你
    2021-01-14 12:59

    I would make an endless while True loop and break it once there is TimeoutException thrown - this would mean there are no pages to go left:

    wait = WebDriverWait(driver, 10)
    while True:
        # grab the data
    
        # click next link
        try:
            element = wait.until(EC.element_to_be_clickable((By.CLASS_NAME, 'reviews_pagination_link_nav')))
            element.click()
        except TimeoutException:
            break
    

    For this to work, you need to make sure that once you hit the last page, the element with class="reviews_pagination_link_nav" is not on the page or is not clickable.

    0 讨论(0)
提交回复
热议问题