简体   繁体   English

Selenium Python - 做动作,点击下一页,重复直到最后一页

[英]Selenium Python - Do action, click next page, repeat until last page

I'm trying to do an action on a webpage, click the next button, then repeat that action until the last page is reached.我正在尝试在网页上执行操作,单击下一步按钮,然后重复该操作,直到到达最后一页。 I've tried using answers from similar questions on StackOverflow but I can't get them to work.我试过在 StackOverflow 上使用类似问题的答案,但我无法让它们工作。 Right now the only thing that happens is the webpage opens.现在唯一发生的是网页打开。 None of my code to do stuff with the webpage happens.我的所有代码都没有发生在网页上。 My code is below.我的代码如下。 Thanks for your help!谢谢你的帮助! from selenium import webdriver from webdriver_manager.chrome import ChromeDriverManager from selenium import webdriver from webdriver_manager.chrome import ChromeDriverManager

driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get('https://obamawhitehouse.archives.gov/briefing-room/speeches-and-remarks')

while True:
    next_page_btn = driver.find_elements_by_xpath("//li[@class = 'pagination-next']/a")
    if len(next_page_btn) < 1:
        print("No more pages left")
        break
    else:
        <MY CODE>
        WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.LINK_TEXT, 'Next'))).click() 

I took a look into the site, and it seems that the pagination-next class doesn't exist.我查看了该站点,似乎不存在分页下一个类。 Instead the "Next" button that you are looking for has the class pager-next last相反,您正在寻找的“下一步”按钮具有类pager-next 最后

I suggest then to change this:我建议然后改变这个:

next_page_btn = driver.find_elements_by_xpath("*//li[@class = 'pagination-next']/a")

for this:为了这:

next_page_btn = driver.find_elements_by_xpath("*//li[@class = 'pager-next last']/a")

Let me know if this helps!如果这有帮助,请告诉我!

Please check below solution for your ref:请检查以下解决方案以获取您的参考:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait as Wait
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import TimeoutException


driver = webdriver.Chrome(executable_path=r"\chromedriver.exe")

driver.get('https://obamawhitehouse.archives.gov/briefing-room/speeches-and-remarks')
wait = WebDriverWait(driver,30)

flag = True

while flag:
 try:
    element = wait.until(EC.element_to_be_clickable((By.XPATH, "//a[contains(text(),'Next')]")))
    if (element != 0):
        element.click()

 except TimeoutException as ex:
        print "It is all good, no element there"

I noticed that the pages of my website were delineated like this:我注意到我网站的页面是这样描述的:

https://obamawhitehouse.archives.gov/briefing-room/speeches-and-remarks?term_node_tid_depth=31&page=1

Going up to page=473 .上升到page=473 So I was able to wrap my code in a while loop, add a counter, and do page={}.format .所以我能够将我的代码包装在一个 while 循环中,添加一个计数器,然后执行page={}.format

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM