Selenium 以编程方式单击下一步按钮直到最后一页 [英] Selenium clicking next button programmatically until the last page

查看:37
本文介绍了Selenium 以编程方式单击下一步按钮直到最后一页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是网络抓取的新手,一直在尝试使用 Selenium 抓取 论坛 在 python 中

hi I'm new to web scraping and have been trying to use Selenium to scrape a forum in python

我试图让 Selenium 单击下一步"直到最后一页,但我不知道如何打破循环.我在使用定位器时遇到问题:

I am trying to get Selenium to click "Next" until the last page but I am not sure how to break the loop. and I having trouble with the locator:

当我通过部分链接找到下一个按钮时,自动点击将继续到下一个线程,例如page1->page2->next thread->page1 of next thread-->page2 of next thread

When I locate the next button by partial link, the automated clicking will continue to next thread e.g page1->page2->next thread->page1 of next thread-->page2 of next thread

while True:
    next_link = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.PARTIAL_LINK_TEXT, "Next")))
    next_link.click()

当我通过类名定位下一个按钮时,自动点击会在到达最后一页时点击上一个"按钮

When I locate the next button by class name, the automated clicking will click "prev" button when it reaches the last page

while True:
    next_link = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CLASS_NAME, "prevnext")))
    next_link.click()

我的问题是:

  1. 我应该使用哪个定位器?(按类或按部分链接或任何其他建议?
  2. 如何打破循环,使其在到达最后一页?

推荐答案

您可以使用以下代码单击 Next 按钮直到到达最后一页,如果该按钮不存在则中断循环:

You can use below code to click Next button until the last page reached and break the loop if the button is not present:

from selenium.common.exceptions import TimeoutException

while True:
    try:
        WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.LINK_TEXT, "Next ›"))).click()
    except TimeoutException:
        break

这篇关于Selenium 以编程方式单击下一步按钮直到最后一页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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