当前位置:网站首页>Selenium basic knowledge paging processing
Selenium basic knowledge paging processing
2022-07-24 07:36:00 【everyone_ yi】
Sometimes you need to turn the page of the list ,Selenium The corresponding API.
Paging processing logic can be roughly divided into the following three steps :
(1) Get total pages .
(2) Get all pages and cycle through .
(3) Carry out subsequent logical processing for each paging .
Baidu Post Bar Python For example :
1. First navigate to paging div
2. Navigate to the last page button 
# -*- coding: utf-8 -*-
from selenium import webdriver
import time
from selenium.webdriver.common.by import By
driver = webdriver.Edge()
data = {
'index_url':'http://tieba.baidu.com/f?ie=utf-8&kw=python',
'pagination_id':'frs_list_pager',
'':''
}
# visit Python Home page
# frs_list_pager
driver.get(data['index_url'])
# Locate until paging div
pagination_div = driver.find_element(by=By.ID,value=data['pagination_id'])
print(pagination_div)
# Calculate the page number of the last page
# First click the last page button
driver.find_element(by=By.CSS_SELECTOR,value='#frs_list_pager > a.last.pagination-item').click()
time.sleep(3)
# Get the number of pages of the last page
last_page_no = driver.find_element(by=By.CSS_SELECTOR,value='#frs_list_pager > span').text
time.sleep(2)
print(last_page_no)
# Jump back to home page
driver.get(data['index_url'])
# loop last_page_no Get the data of each page once
for index in last_page_no:
# Some code to collect data , Omit
time.sleep(2)
driver.find_element(by=By.CSS_SELECTOR,value='#frs_list_pager > a.next.pagination-item').click()
# driver.quit()
After several trial runs, it was found that it stopped every time from page one to page five Then add an output statement to the loop Find every time index Namely 1 3 9 3
It's just the same as the total number of pages Just add another one type Statement output last_page_no Variable Found to be string type So there are only four cycles print(type(last_page_no))
The final code
# -*- coding: utf-8 -*-
from selenium import webdriver
import time
from selenium.webdriver.common.by import By
driver = webdriver.Edge()
data = {
'index_url':'http://tieba.baidu.com/f?ie=utf-8&kw=python',
'pagination_id':'frs_list_pager'
}
# visit Python Home page
# frs_list_pager
driver.get(data['index_url'])
# Locate until paging div
pagination_div = driver.find_element(by=By.ID,value=data['pagination_id'])
print(pagination_div)
# Calculate the page number of the last page
# First click the last page button
driver.find_element(by=By.CSS_SELECTOR,value='#frs_list_pager > a.last.pagination-item').click()
time.sleep(2)
# Get the number of pages of the last page
last_page_no = driver.find_element(by=By.CSS_SELECTOR,value='#frs_list_pager > span').text
# Now get last_page_no yes string type Need to transform Otherwise, there will only be four cycles 1 3 9 3 because lastpageno Is string ‘1393’
time.sleep(2)
print(type(last_page_no))
last_page_no = int(last_page_no)
print(type(last_page_no))
print(last_page_no)
# Jump back to home page
driver.get(data['index_url'])
# loop last_page_no Get the data of each page once
for index in range(last_page_no):
# Some code to collect data , Omit
print(index)
time.sleep(2)
driver.find_element(by=By.CSS_SELECTOR,value='#frs_list_pager > a.next.pagination-item').click()
# driver.quit()
Just like this !
边栏推荐
- Oauth2==SSO三种协议。Oauth2四种模式
- Kali安装pip以及pip换源
- 深度学习二三事-回顾那些经典卷积神经网络
- Introduction to C language
- C语言文件操作
- Jenkins detailed deployment
- [leetcode] 11. Container with the most water - go language solution
- Give a string ① please count the number of times each letter appears ② please print the pair with the most letters
- Jackson parsing JSON detailed tutorial
- MITRE ATT&CK超详细学习笔记-01(背景,术语,案例)
猜你喜欢

JS_实现多行文本根据换行分隔成数组

深度学习二三事-回顾那些经典卷积神经网络

requests-爬取页面源码数据

Introduction to C language II. Functions

Requests crawler implements a simple web page collector

Introduction to C language I. branch and loop statements
![[line test] Figure finding regular questions](/img/61/d1c2cd399cf0d808e4fa25cd5fe681.png)
[line test] Figure finding regular questions

Jenkins detailed deployment

C language advanced part II Pointer

Introduction to C language
随机推荐
Cloud version upgrade
Influxdb未授权访问&CouchDB权限绕过
24.全局事件总线
爬虫学习-概述
25. Message subscription and publishing - PubSub JS
C language advanced part III. string functions and memory operation functions
win10声音图标有个没有声音
From the perspective of CIA, common network attacks (blasting, PE, traffic attacks)
php链路日志方案
Using depth and normal textures in unity
Influxdb unauthorized access & CouchDB permission bypass
给一个字符串 ① 请统计出其中每一个字母出现的次数② 请打印出字母次数最多的那一对
[sequential logic circuit] - register
Kali installing PIP and pip source changing
使用堡垒机(跳板机)登录服务器
2022-07-23:给定N件物品,每个物品有重量(w[i])、有价值(v[i]), 只能最多选两件商品,重量不超过bag,返回价值最大能是多少? N <= 10^5, w[i] <= 10^5, v
Jackson 解析 JSON 详细教程
Log in to the server using the fortress machine (springboard machine)
从CIA看常见网络攻击(爆破,PE,流量攻击)
【sklearn】tree.DecisionTreeClassifier