Python+Selenium自動(dòng)化實(shí)現(xiàn)分頁(pagination)處理
場景
對分頁來說,我們最感興趣的是下面幾個(gè)信息
總共有多少頁
當(dāng)前是第幾頁
是否可以上一頁和下一頁
代碼
下面代碼演示如何獲取分頁總數(shù)及當(dāng)前頁數(shù)、跳轉(zhuǎn)到指定頁數(shù)
#coding:utf-8
from selenium import webdriver
import time
driver = webdriver.Chrome()
driver.get("https://segmentfault.com/news")
# 獲得所有分頁的數(shù)量
# -2是因?yàn)橐サ羯弦粋€(gè)和下一個(gè)
total_pages = len(driver.find_element_by_class_name("pagination").find_elements_by_tag_name("li"))-2
print "total_pages is %s" %(total_pages)
# 獲取當(dāng)前頁面是第幾頁
current_page = driver.find_element_by_class_name('pagination').find_element_by_class_name('active')
print "current page is %s" %(current_page.text)
#跳轉(zhuǎn)到第二頁
next_page = driver.find_element_by_class_name("pagination").find_element_by_link_text("2")
next_page.click()
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python利用pangu模塊實(shí)現(xiàn)文本格式化小工具
python 代碼實(shí)現(xiàn)k-means聚類分析的思路(不使用現(xiàn)成聚類庫)
python+matplotlib實(shí)現(xiàn)動(dòng)態(tài)繪制圖片實(shí)例代碼(交互式繪圖)
Tensorflow訓(xùn)練模型默認(rèn)占滿所有GPU的解決方案

