mirror of
https://gitee.com/g1879/DrissionPage.git
synced 2024-12-10 04:00:23 +08:00
2.6.0,新增Listener类
This commit is contained in:
parent
95b019b116
commit
abd6aa707d
@ -285,7 +285,7 @@ class DriverElement(DrissionElement):
|
||||
index: int = 1,
|
||||
filter_loc: Union[tuple, str] = '',
|
||||
timeout: float = None) -> Union['DriverElement', str, None]:
|
||||
"""返回前面的一个兄弟元素,可用查询语法筛选,可指定返回筛选结果的第几个 \n
|
||||
"""返回当前元素前面的一个元素,可指定筛选条件和第几个。查找范围不限兄弟元,而是整个DOM文档 \n
|
||||
:param index: 前面第几个查询结果元素
|
||||
:param filter_loc: 用于筛选元素的查询语法
|
||||
:param timeout: 查找元素的超时时间
|
||||
@ -297,7 +297,7 @@ class DriverElement(DrissionElement):
|
||||
index: int = 1,
|
||||
filter_loc: Union[tuple, str] = '',
|
||||
timeout: float = None) -> Union['DriverElement', str, None]:
|
||||
"""返回后面的一个兄弟元素,可用查询语法筛选,可指定返回筛选结果的第几个 \n
|
||||
"""返回当前元素后面的一个元素,可指定筛选条件和第几个。查找范围不限兄弟元,而是整个DOM文档 \n
|
||||
:param index: 后面第几个查询结果元素
|
||||
:param filter_loc: 用于筛选元素的查询语法
|
||||
:param timeout: 查找元素的超时时间
|
||||
@ -328,7 +328,7 @@ class DriverElement(DrissionElement):
|
||||
def befores(self,
|
||||
filter_loc: Union[tuple, str] = '',
|
||||
timeout: float = None) -> List[Union['DriverElement', str]]:
|
||||
"""返回后面全部兄弟元素或节点组成的列表,可用查询语法筛选 \n
|
||||
"""返回当前元素后面符合条件的全部兄弟元素或节点组成的列表,可用查询语法筛选。查找范围不限兄弟元,而是整个DOM文档 \n
|
||||
:param filter_loc: 用于筛选元素的查询语法
|
||||
:param timeout: 查找元素的超时时间
|
||||
:return: 本元素前面的元素或节点组成的列表
|
||||
@ -338,7 +338,7 @@ class DriverElement(DrissionElement):
|
||||
def afters(self,
|
||||
filter_loc: Union[tuple, str] = '',
|
||||
timeout: float = None) -> List[Union['DriverElement', str]]:
|
||||
"""返回前面全部兄弟元素或节点组成的列表,可用查询语法筛选 \n
|
||||
"""返回当前元素前面符合条件的全部兄弟元素或节点组成的列表,可用查询语法筛选。查找范围不限兄弟元,而是整个DOM文档 \n
|
||||
:param filter_loc: 用于筛选元素的查询语法
|
||||
:param timeout: 查找元素的超时时间
|
||||
:return: 本元素后面的元素或节点组成的列表
|
||||
|
@ -5,7 +5,7 @@
|
||||
from json import loads, JSONDecodeError
|
||||
from threading import Thread
|
||||
from time import perf_counter, sleep
|
||||
from typing import Union, Tuple, List
|
||||
from typing import Union, Tuple, List, Iterable
|
||||
|
||||
from pychrome import Tab
|
||||
|
||||
@ -15,7 +15,13 @@ from .mix_page import MixPage
|
||||
|
||||
|
||||
class ResponseData(object):
|
||||
"""返回的数据包管理类"""
|
||||
|
||||
def __init__(self, response: dict, body: str):
|
||||
"""初始化 \n
|
||||
:param response: response格式化的数据
|
||||
:param body: response包含的内容
|
||||
"""
|
||||
self.response = response
|
||||
self.raw_body = body
|
||||
self._json_body = None
|
||||
@ -25,6 +31,7 @@ class ResponseData(object):
|
||||
|
||||
@property
|
||||
def body(self):
|
||||
"""返回body内容,如果是json格式,自动进行转换,其它格式直接返回文本"""
|
||||
if self._json_body is not False and self.response.get('mimeType', None) == 'application/json':
|
||||
if self._json_body is None:
|
||||
try:
|
||||
@ -56,13 +63,7 @@ class Listener(object):
|
||||
|
||||
self._response_count = 0
|
||||
self._requestIds = {}
|
||||
|
||||
# @property
|
||||
# def first_result(self) -> Union[ResponseData, None]:
|
||||
# try:
|
||||
# return list(self.results.values())[0][0]
|
||||
# except IndexError:
|
||||
# return None
|
||||
self._a_response_loaded = False
|
||||
|
||||
def set_targets(self, targets: Union[str, List[str], Tuple[str]]) -> None:
|
||||
"""设置要拦截的目标,可以设置多个 \n
|
||||
@ -153,7 +154,7 @@ class Listener(object):
|
||||
self.tab.Network.responseReceived = self._null_function
|
||||
self.tab.Network.loadingFinished = self._null_function
|
||||
|
||||
def _loading_finished(self, **kwargs) -> None:
|
||||
def _loading_finished(self, **kwargs):
|
||||
"""请求完成时处理方法"""
|
||||
requestId = kwargs['requestId']
|
||||
target = self._requestIds.pop(requestId, None)
|
||||
@ -166,6 +167,28 @@ class Listener(object):
|
||||
self.results[target] = [response]
|
||||
self._response_count += 1
|
||||
|
||||
self._a_response_loaded = True
|
||||
|
||||
def steps(self, response_num: int = 1) -> Iterable:
|
||||
"""用于单步操作,可实现没收到若干个数据包执行一步操作(如翻页) \n
|
||||
于是可以根据数据包是否加载完成来决定是否翻页,无须从页面dom去判断是否加载完成 \n
|
||||
大大简化代码,提高可靠性 \n
|
||||
eg: for i in listener.steps(2): \n
|
||||
btn.click() \n
|
||||
:param response_num: 每接收到多少个数据包触发
|
||||
:return: None
|
||||
"""
|
||||
count = 0
|
||||
while True:
|
||||
if not self.listening:
|
||||
return
|
||||
|
||||
if self._a_response_loaded:
|
||||
self._a_response_loaded = False
|
||||
count += 1
|
||||
if count % response_num == 0:
|
||||
yield
|
||||
|
||||
def _response_received(self, **kwargs) -> None:
|
||||
"""接收到返回信息时处理方法"""
|
||||
for target in self.targets:
|
||||
|
@ -27,7 +27,7 @@
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<div id="app">加载中。。。</div>
|
||||
<div id="app">加载中,如加载缓慢请刷新页面。</div>
|
||||
<script>
|
||||
window.$docsify = {
|
||||
count: {
|
||||
|
15
docs/版本历史.md
15
docs/版本历史.md
@ -1,17 +1,22 @@
|
||||
# 2.5.8
|
||||
# v2.6.0
|
||||
|
||||
- 新增`Listener`类
|
||||
- 可监听浏览器数据包
|
||||
- 可异步监听
|
||||
- 可实现每监听到若干数据包执行操作
|
||||
- 放弃对selenium4.1以下的支持
|
||||
- 解决使用新版浏览器时出现的一些问题
|
||||
|
||||
# v2.5.9
|
||||
|
||||
- 优化 s 模式创建连接的逻辑
|
||||
|
||||
# v2.5.7
|
||||
|
||||
- 列表元素`select()`、`deselect()`等方法添加`timeout`参数,可等待列表元素加载
|
||||
|
||||
- 优化了对消息提示框的处理
|
||||
|
||||
- `drag()`和`drag_to()`不再检测是否拖拽成功,改成返回`None`
|
||||
|
||||
- `DriverOptions`对象从父类继承的方法也支持链式操作
|
||||
|
||||
- 其它优化和问题修复
|
||||
|
||||
# v2.5.5
|
||||
|
Loading…
x
Reference in New Issue
Block a user