Home > Backend Development > Python Tutorial > Python+Selenium automates paging processing

Python+Selenium automates paging processing

Y2J
Release: 2017-04-20 09:09:08
Original
2411 people have browsed it

This article mainly introduces in detail the method of Python+Selenium to automatically implement paging processing. It has a certain reference value. Interested friends can refer to it

Scenario

For paging, what we are most interested in is the following information

How many pages are there in total
What page is the current page
Can we go to the previous page? Page and next page

Code

The following code demonstrates how to obtain the total number of pages, the current page number, and jump to the specified page number


#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()
Copy after login

The above is the detailed content of Python+Selenium automates paging processing. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template