場景
對分頁來說,我們最感興趣的是下面幾個信息
- 總共有多少頁
- 當前是第幾頁
- 是否可以上一頁和下一頁
代碼
下面代碼演示如何獲取分頁總數及當前頁數、跳轉到指定頁數
#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()