如何让 Selenium 单击“下一步"?按钮直到不再可能? [英] How can I make Selenium click on the "Next" button until it is no longer possible?

查看:32
本文介绍了如何让 Selenium 单击“下一步"?按钮直到不再可能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写一段代码,让Python在页面上抓取一些数据,然后点击页面底部的下一步"按钮,在第二页上抓取一些数据,点击下一步"按钮等,直到最后一页,不再可以点击下一步"(因为没有下一步").

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 page, click on the "next" button, etc. until the last page, where clicking on "Next" is no longer possible (because there is no "next").

我想让代码尽可能通用,而不是事先指定要完成的点击次数.按照这个问题(如何才能我让 Selenium 单击可变数量的下一步"按钮?),我有下面的代码.Python没有报错,但是程序在第一次迭代后就停止了(第一次点击next"后).

I would like to make the code as general as possible and not specify beforehand the number of clicks to be done. Following this question (How can I make Selenium click through a variable number of "next" buttons?), I have the code below. Python does not report any error, but the program stops after the first iteration (after the first click on the "next").

我在这里错过了什么?非常感谢!

What am I missing here? Many thanks!

driver = webdriver.Firefox()
driver.get("http://www.mywebsite_example.com")
try:
    wait = WebDriverWait(driver, 100)
    wait.until(EC.element_to_be_clickable((By.CLASS_NAME,'reviews_pagination_link_nav')))    
    driver.find_element_by_class_name("reviews_pagination_link_nav").click()

    wait.until(EC.element_to_be_clickable((By.CLASS_NAME, 'reviews_pagination_link_nav')))
    while EC.element_to_be_clickable((By.CLASS_NAME,'reviews_pagination_link_nav')):
      driver.find_element_by_class_name("reviews_pagination_link_nav").click()
      if not driver.find_element_by_class_name("reviews_pagination_link_nav"):
        break
      wait.until(EC.element_to_be_clickable((By.CLASS_NAME, 'reviews_pagination_link_nav')))

finally:
    driver.quit()

推荐答案

我会做一个无休止的 while True 循环并在抛出 TimeoutException 时中断它 - 这会意味着没有页面可以离开:

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

为此,您需要确保在点击最后一页后,带有 class="reviews_pagination_link_nav" 的元素不在页面上或不可点击.

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.

这篇关于如何让 Selenium 单击“下一步"?按钮直到不再可能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆