Home >Backend Development >Python Tutorial >Python+Selenium automates paging processing
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()
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!