簡體   English   中英

Selenium Python - 做動作,點擊下一頁,重復直到最后一頁

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

我正在嘗試在網頁上執行操作,單擊下一步按鈕,然后重復該操作,直到到達最后一頁。 我試過在 StackOverflow 上使用類似問題的答案,但我無法讓它們工作。 現在唯一發生的是網頁打開。 我的所有代碼都沒有發生在網頁上。 我的代碼如下。 謝謝你的幫助! 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() 

我查看了該站點,似乎不存在分頁下一個類。 相反,您正在尋找的“下一步”按鈕具有類pager-next 最后

我建議然后改變這個:

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

為了這:

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

如果這有幫助,請告訴我!

請檢查以下解決方案以獲取您的參考:

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"

我注意到我網站的頁面是這樣描述的:

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

上升到page=473 所以我能夠將我的代碼包裝在一個 while 循環中,添加一個計數器,然后執行page={}.format

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM