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

查看:397
本文介绍了如何使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()

推荐答案

一旦抛出TimeoutException,我将创建一个无限的while True循环并将其中断-这将意味着没有剩余的页面了:

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天全站免登陆