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

分類:IT技術 時間:2017-04-03

場景

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

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

代碼

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

#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()

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持電腦玩物。


Tags: current active import 電腦 如何

文章來源:


ads
ads

相關文章
ads

相關文章

ad