Python+Selenium automates paging (pagination) processing

  • 2020-05-27 06:26:40
  • OfStack


For pagination, we are most interested in the following information

How many pages are there altogether
What is the current page number
Can I go up to page 1 and down to page 1


The following code shows how to get the total number of pages and the current number of pages to jump to the specified number of pages

from selenium import webdriver
import time
driver = webdriver.Chrome()

#  Gets the number of all pages 
# -2 Because you want to get rid of it 1 A and the 1 a 
total_pages = len(driver.find_element_by_class_name("pagination").find_elements_by_tag_name("li"))-2
print "total_pages is %s" %(total_pages)

#  Gets the current page number 
current_page = driver.find_element_by_class_name('pagination').find_element_by_class_name('active')
print "current page is %s" %(current_page.text)

# Jump to the first 2 page 
next_page = driver.find_element_by_class_name("pagination").find_element_by_link_text("2")

Related articles: