Python+Selenium 自動化實現實例-處理分頁(pagination)


場景

對分頁來說,我們最感興趣的是下面幾個信息

  • 總共有多少頁
  • 當前是第幾頁
  • 是否可以上一頁和下一頁

代碼

下面代碼演示如何獲取分頁總數及當前頁數、跳轉到指定頁數

 
        
#coding:utf-8
from selenium import webdriver import time driver = webdriver.Chrome() driver.get("https://segmentfault.com/news") # 獲得所有分頁的數量 # -2是因為要去掉上一個和下一個
total_pages = len(driver.find_element_by_class_name("pagination").find_elements_by_tag_name("li"))-2
print "total_pages is %s" %(total_pages) # 獲取當前頁面是第幾頁
current_page = driver.find_element_by_class_name('pagination').find_element_by_class_name('active') print "current page is %s" %(current_page.text) #跳轉到第二頁
next_page = driver.find_element_by_class_name("pagination").find_element_by_link_text("2") next_page.click()
 
        

 



 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM