当前位置: 移动技术网 > IT编程>脚本编程>Python > Python+Selenium自动化实现分页(pagination)处理

Python+Selenium自动化实现分页(pagination)处理

2017年12月12日  | 移动技术网IT编程  | 我要评论

努拉莫克,广东人事考试,宝鸡社区

场景

对分页来说,我们最感兴趣的是下面几个信息

总共有多少页
当前是第几页
是否可以上一页和下一页

代码

下面代码演示如何获取分页总数及当前页数、跳转到指定页数

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网