Initial commit
This commit is contained in:
commit
57fc3525eb
7311 files changed
+1291398
No files matched your search
@@ -0,0 +1,8 @@
|
||||
# -*- coding:utf-8 -*-
|
||||
"""
|
||||
@Author : g1879
|
||||
@Contact : g1879@qq.com
|
||||
"""
|
||||
from .drissionGet import DrissionGet
|
||||
|
||||
__version__ = '1.2.1'
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,218 @@
|
||||
# -*- coding:utf-8 -*-
|
||||
from copy import copy
|
||||
from os import path as os_PATH
|
||||
from os.path import sep
|
||||
from pathlib import Path
|
||||
from random import randint
|
||||
from re import search, sub
|
||||
from time import time
|
||||
from urllib.parse import unquote
|
||||
|
||||
from DrissionRecord.tools import get_usable_path, make_valid_name
|
||||
from requests import Session
|
||||
|
||||
FILE_EXISTS_MODE = {'rename': 'rename', 'overwrite': 'overwrite', 'skip': 'skip', 'add': 'add', 'r': 'rename',
|
||||
'o': 'overwrite', 's': 'skip', 'a': 'add'}
|
||||
|
||||
|
||||
def copy_session(session):
|
||||
new = Session()
|
||||
new.headers = session.headers.copy()
|
||||
new.cookies = session.cookies.copy()
|
||||
new.stream = True
|
||||
new.auth = session.auth
|
||||
new.proxies = dict(session.proxies).copy()
|
||||
new.params = copy(session.params) #
|
||||
new.cert = session.cert
|
||||
new.max_redirects = session.max_redirects
|
||||
new.trust_env = session.trust_env
|
||||
new.verify = session.verify
|
||||
|
||||
return new
|
||||
|
||||
|
||||
class BlockSizeSetter(object):
|
||||
def __set__(self, block_size, val):
|
||||
if isinstance(val, int) and val > 0:
|
||||
size = val
|
||||
elif isinstance(val, str):
|
||||
units = {'b': 1, 'k': 1024, 'm': 1048576, 'g': 21474836480}
|
||||
num = int(val[:-1])
|
||||
unit = units.get(val[-1].lower(), None)
|
||||
if unit and num > 0:
|
||||
size = num * unit
|
||||
else:
|
||||
raise ValueError('单位只支持B、K、M、G,数字必须为大于0的整数。')
|
||||
else:
|
||||
raise TypeError('split_size只能传入int或str,数字必须为大于0的整数。')
|
||||
|
||||
block_size._block_size = size
|
||||
|
||||
def __get__(self, block_size, objtype=None) -> int:
|
||||
return block_size._block_size
|
||||
|
||||
|
||||
class PathSetter(object):
|
||||
def __set__(self, save_path, val):
|
||||
if val is not None and not isinstance(val, (str, Path)):
|
||||
raise TypeError('路径只能是str或Path类型。')
|
||||
save_path._save_path = get_save_path(val)
|
||||
|
||||
def __get__(self, save_path, objtype=None):
|
||||
return save_path._save_path
|
||||
|
||||
|
||||
class FileExistsSetter(object):
|
||||
def __set__(self, file_exists, mode):
|
||||
file_exists._file_exists = get_file_exists_mode(mode)
|
||||
|
||||
def __get__(self, file_exists, objtype=None):
|
||||
return file_exists._file_exists
|
||||
|
||||
|
||||
def get_save_path(path):
|
||||
path = Path(path or '.').absolute()
|
||||
return Path(path.anchor + sub(r'[*:|<>?"]', '', sep.join(path.parts[1:])).strip())
|
||||
|
||||
|
||||
def get_file_exists_mode(mode):
|
||||
mode = FILE_EXISTS_MODE.get(mode, mode)
|
||||
if mode not in FILE_EXISTS_MODE:
|
||||
raise ValueError(f'''mode参数只能是 '{"', '".join(FILE_EXISTS_MODE.keys())}' 之一,现在是:{mode}''')
|
||||
return mode
|
||||
|
||||
|
||||
def set_charset(response, encoding):
|
||||
if encoding:
|
||||
response.encoding = encoding
|
||||
return response
|
||||
|
||||
# 在headers中获取编码
|
||||
content_type = response.headers.get('content-type', '').lower()
|
||||
if not content_type.endswith(';'):
|
||||
content_type += ';'
|
||||
charset = search(r'charset[=: ]*(.*)?;?', content_type)
|
||||
|
||||
if charset:
|
||||
response.encoding = charset.group(1)
|
||||
|
||||
# 在headers中获取不到编码,且如果是网页
|
||||
elif content_type.replace(' ', '').startswith('text/html'):
|
||||
re_result = search(b'<meta.*?charset=[ \\\'"]*([^"\\\' />]+).*?>', response.content)
|
||||
|
||||
if re_result:
|
||||
charset = re_result.group(1).decode()
|
||||
else:
|
||||
charset = response.apparent_encoding
|
||||
|
||||
response.encoding = charset
|
||||
|
||||
return response
|
||||
|
||||
|
||||
def get_file_info(response, save_path, rename=None, suffix=None, file_exists=None, encoding=None, lock=None):
|
||||
# ------------获取文件大小------------
|
||||
file_size = response.headers.get('Content-Length', None)
|
||||
file_size = None if file_size is None else int(file_size)
|
||||
|
||||
# ------------获取网络文件名------------
|
||||
file_name = get_file_name(response, encoding)
|
||||
|
||||
# ------------获取保存文件名------------
|
||||
# -------------------重命名-------------------
|
||||
if rename:
|
||||
if suffix is not None:
|
||||
full_name = f'{rename}.{suffix}' if suffix else rename
|
||||
|
||||
else:
|
||||
tmp = file_name.rsplit('.', 1)
|
||||
ext_name = f'.{tmp[-1]}' if len(tmp) > 1 else ''
|
||||
tmp = rename.rsplit('.', 1)
|
||||
ext_rename = f'.{tmp[-1]}' if len(tmp) > 1 else ''
|
||||
full_name = rename if ext_rename == ext_name else f'{rename}{ext_name}'
|
||||
|
||||
elif suffix is not None:
|
||||
full_name = file_name.rsplit(".", 1)[0]
|
||||
if suffix:
|
||||
full_name = f'{full_name}.{suffix}'
|
||||
|
||||
else:
|
||||
full_name = file_name
|
||||
|
||||
full_name = make_valid_name(full_name)
|
||||
|
||||
# -------------------生成路径-------------------
|
||||
skip = False
|
||||
overwrite = False
|
||||
full_path = save_path / full_name
|
||||
|
||||
with lock:
|
||||
if full_path.exists():
|
||||
if file_exists == 'rename':
|
||||
full_path = get_usable_path(full_path)
|
||||
|
||||
elif file_exists == 'skip':
|
||||
skip = True
|
||||
|
||||
elif file_exists == 'overwrite':
|
||||
overwrite = True
|
||||
full_path.unlink()
|
||||
|
||||
return {'size': file_size,
|
||||
'path': full_path,
|
||||
'skip': skip,
|
||||
'overwrite': overwrite}
|
||||
|
||||
|
||||
def get_file_name(response, encoding):
|
||||
file_name = ''
|
||||
charset = ''
|
||||
content_disposition = response.headers.get('content-disposition', '').replace(' ', '')
|
||||
|
||||
# 使用header里的文件名
|
||||
if content_disposition:
|
||||
txt = search(r'filename\*="?([^";]+)', content_disposition)
|
||||
if txt: # 文件名自带编码方式
|
||||
txt = txt.group(1).split("''", 1)
|
||||
if len(txt) == 2:
|
||||
charset, file_name = txt
|
||||
else:
|
||||
file_name = txt[0]
|
||||
|
||||
else: # 文件名没带编码方式
|
||||
txt = search(r'filename="?([^";]+)', content_disposition)
|
||||
if txt:
|
||||
file_name = txt.group(1)
|
||||
|
||||
# 获取编码(如有)
|
||||
charset = encoding or response.encoding
|
||||
|
||||
file_name = file_name.strip("'")
|
||||
|
||||
# 在url里获取文件名
|
||||
if not file_name and os_PATH.basename(response.url):
|
||||
file_name = os_PATH.basename(response.url).split("?")[0]
|
||||
|
||||
# 找不到则用时间和随机数生成文件名
|
||||
if not file_name:
|
||||
file_name = f'untitled_{time()}_{randint(0, 100)}'
|
||||
|
||||
# 去除非法字符
|
||||
charset = charset or 'utf-8'
|
||||
return unquote(file_name, charset)
|
||||
|
||||
|
||||
def set_session_cookies(session, cookies):
|
||||
# cookies = cookies_to_tuple(cookies)
|
||||
for cookie in cookies:
|
||||
if cookie['value'] is None:
|
||||
cookie['value'] = ''
|
||||
|
||||
kwargs = {x: cookie[x] for x in cookie
|
||||
if x.lower() in ('version', 'port', 'domain', 'path', 'secure',
|
||||
'expires', 'discard', 'comment', 'comment_url', 'rest')}
|
||||
|
||||
if 'expiry' in cookie:
|
||||
kwargs['expires'] = cookie['expiry']
|
||||
|
||||
session.cookies.set(cookie['name'], cookie['value'], **kwargs)
|
||||
@@ -0,0 +1,93 @@
|
||||
# -*- coding:utf-8 -*-
|
||||
from pathlib import Path
|
||||
from threading import Lock
|
||||
from typing import Union, Optional
|
||||
|
||||
from requests import Session, Response
|
||||
|
||||
FILE_EXISTS_MODE: dict = ...
|
||||
|
||||
|
||||
def copy_session(session: Session) -> Session:
|
||||
"""复制输入Session对象,返回一个新的
|
||||
:param session: 被复制的Session对象
|
||||
:return: 新Session对象
|
||||
"""
|
||||
...
|
||||
|
||||
|
||||
class BlockSizeSetter(object):
|
||||
def __set__(self, block_size, val: Union[str, int]): ...
|
||||
|
||||
def __get__(self, block_size, objtype=None) -> int: ...
|
||||
|
||||
|
||||
class PathSetter(object):
|
||||
def __set__(self, save_path, val: Union[str, Path]): ...
|
||||
|
||||
def __get__(self, save_path, objtype=None): ...
|
||||
|
||||
|
||||
class FileExistsSetter(object):
|
||||
def __set__(self, file_exists, mode: str): ...
|
||||
|
||||
def __get__(self, file_exists, objtype=None): ...
|
||||
|
||||
|
||||
def get_save_path(path: Union[str, Path]) -> Path:
|
||||
"""获取绝对路径,去除Windows系统非法字符
|
||||
:param path: 输入的路径
|
||||
:return: Path格式路径
|
||||
"""
|
||||
...
|
||||
|
||||
|
||||
def get_file_exists_mode(mode: str) -> str:
|
||||
"""获取文件重名时处理策略名称
|
||||
:param mode: 输入
|
||||
:return: 标准字符串
|
||||
"""
|
||||
...
|
||||
|
||||
|
||||
def set_charset(response: Response, encoding: Optional[str]) -> Response:
|
||||
"""设置Response对象的编码
|
||||
:param response: Response对象
|
||||
:param encoding: 指定的编码格式
|
||||
:return: 设置编码后的Response对象
|
||||
"""
|
||||
...
|
||||
|
||||
|
||||
def get_file_info(response: Response, save_path: Path, rename: str = None, suffix: str = None,
|
||||
file_exists: str = None, encoding: Optional[str] = None, lock: Lock = None) -> dict:
|
||||
"""获取文件信息,大小单位为byte
|
||||
包括:size、path、skip
|
||||
:param response: Response对象
|
||||
:param save_path: 目标文件夹Path对象
|
||||
:param rename: 重命名
|
||||
:param suffix: 重命名后缀名
|
||||
:param file_exists: 存在重名文件时的处理方式
|
||||
:param encoding: 编码格式
|
||||
:param lock: 线程锁
|
||||
:return: 文件大小、完整路径、是否跳过、是否覆盖
|
||||
"""
|
||||
...
|
||||
|
||||
|
||||
def get_file_name(response: Response, encoding: str) -> str:
|
||||
"""从headers或url中获取文件名,如果获取不到,生成一个随机文件名
|
||||
:param response: 返回的response
|
||||
:param encoding: 在headers获取时指定编码格式
|
||||
:return: 下载文件的文件名
|
||||
"""
|
||||
...
|
||||
|
||||
|
||||
def set_session_cookies(session: Session, cookies: list) -> None:
|
||||
"""设置Session对象的cookies
|
||||
:param session: Session对象
|
||||
:param cookies: cookies信息
|
||||
:return: None
|
||||
"""
|
||||
...
|
||||
@@ -0,0 +1,539 @@
|
||||
# -*- coding:utf-8 -*-
|
||||
from copy import copy
|
||||
from pathlib import Path
|
||||
from queue import Queue
|
||||
from threading import Thread, Lock
|
||||
from time import sleep, perf_counter
|
||||
|
||||
from requests.structures import CaseInsensitiveDict
|
||||
|
||||
from ._funcs import (FileExistsSetter, PathSetter, BlockSizeSetter, set_charset, get_file_info, get_file_exists_mode,
|
||||
get_save_path)
|
||||
from .mission import Task, Mission
|
||||
from .setter import Setter
|
||||
|
||||
|
||||
class DrissionGet(object):
|
||||
file_exists = FileExistsSetter()
|
||||
save_path = PathSetter()
|
||||
block_size = BlockSizeSetter()
|
||||
|
||||
def __init__(self, save_path='.', roads=10, driver=None, file_exists='rename'):
|
||||
self._roads = roads
|
||||
self._missions = {}
|
||||
self._threads = {i: None for i in range(self._roads)}
|
||||
self._waiting_list = Queue()
|
||||
self._missions_num = 0
|
||||
self._running_count = 0 # 正在运行的任务数
|
||||
self._stop_printing = False # 用于控制显示线程停止
|
||||
self._lock = Lock()
|
||||
self.page = None
|
||||
self._retry = None
|
||||
self._interval = None
|
||||
self._timeout = None
|
||||
self._encoding = None
|
||||
|
||||
self._setter = None
|
||||
self._print_mode = 'all'
|
||||
self._log_mode = None
|
||||
self._logger = None
|
||||
|
||||
self.save_path = save_path
|
||||
self.file_exists = file_exists
|
||||
self.split = True
|
||||
self.block_size = '50M'
|
||||
self.set.driver(driver)
|
||||
|
||||
def __call__(self, url=None, save_path=None, rename=None, suffix=None,
|
||||
file_exists=None, split=False, show_msg=True, file_url=None, **kwargs):
|
||||
return self.download(url=url, save_path=save_path, rename=rename, suffix=suffix,
|
||||
file_exists=file_exists, show_msg=show_msg, **kwargs)
|
||||
|
||||
@property
|
||||
def set(self):
|
||||
if self._setter is None:
|
||||
self._setter = Setter(self)
|
||||
return self._setter
|
||||
|
||||
@property
|
||||
def roads(self):
|
||||
return self._roads
|
||||
|
||||
@property
|
||||
def retry(self):
|
||||
if self._retry is not None:
|
||||
return self._retry
|
||||
elif self.page is not None:
|
||||
return self.page.retry_times
|
||||
else:
|
||||
return 3
|
||||
|
||||
@property
|
||||
def interval(self):
|
||||
if self._interval is not None:
|
||||
return self._interval
|
||||
elif self.page is not None:
|
||||
return self.page.retry_interval
|
||||
else:
|
||||
return 5
|
||||
|
||||
@property
|
||||
def timeout(self):
|
||||
if self._timeout is not None:
|
||||
return self._timeout
|
||||
elif self.page is not None:
|
||||
return self.page.timeout
|
||||
else:
|
||||
return 20
|
||||
|
||||
@property
|
||||
def waiting_list(self):
|
||||
return self._waiting_list
|
||||
|
||||
@property
|
||||
def session(self):
|
||||
return self._session
|
||||
|
||||
@property
|
||||
def is_running(self):
|
||||
return self._running_count > 0
|
||||
|
||||
@property
|
||||
def missions(self):
|
||||
return self._missions
|
||||
|
||||
@property
|
||||
def encoding(self):
|
||||
"""返回指定的编码格式"""
|
||||
return self._encoding
|
||||
|
||||
def add(self, url=None, save_path=None, rename=None, suffix=None, file_exists=None, split=None,
|
||||
file_url=None, **kwargs):
|
||||
if not url and file_url:
|
||||
url = file_url
|
||||
self._missions_num += 1
|
||||
self._running_count += 1
|
||||
if file_exists is not None:
|
||||
file_exists = get_file_exists_mode(file_exists)
|
||||
mission = Mission(self._missions_num, self, url, get_save_path(save_path) if save_path else self.save_path,
|
||||
rename, suffix, file_exists or self.file_exists,
|
||||
self.split if split is None else split, self._encoding, kwargs)
|
||||
self._missions[self._missions_num] = mission
|
||||
|
||||
self._run_or_wait(mission)
|
||||
return mission
|
||||
|
||||
def download(self, url=None, save_path=None, rename=None, suffix=None, file_exists=None, split=False,
|
||||
show_msg=None, file_url=None, **kwargs):
|
||||
if not url and file_url:
|
||||
url = file_url
|
||||
self._missions_num += 1
|
||||
self._running_count += 1
|
||||
if file_exists is not None:
|
||||
file_exists = get_file_exists_mode(file_exists)
|
||||
mission = Mission(self._missions_num, self, url, get_save_path(save_path) if save_path else self.save_path,
|
||||
rename, suffix, file_exists or self.file_exists,
|
||||
self.split if split is None else split, self._encoding, kwargs)
|
||||
self._missions[self._missions_num] = mission
|
||||
|
||||
tmp, self._print_mode = self._print_mode, None
|
||||
if show_msg is None:
|
||||
show_msg = True if tmp else False
|
||||
|
||||
if split:
|
||||
self._run_or_wait(mission)
|
||||
r = mission.wait(show=show_msg)
|
||||
self._print_mode = tmp
|
||||
return r
|
||||
|
||||
ok, r, file_size = handle_mission(mission, url)
|
||||
if show_msg:
|
||||
print(f'url:{url}')
|
||||
print(f'文件名:{mission.file_name}')
|
||||
print(f'目标路径:{mission.path}')
|
||||
if not mission.size:
|
||||
print('下载中 ', end='')
|
||||
|
||||
if ok:
|
||||
task1 = Task(mission, None, '1/1', file_size)
|
||||
mission.tasks.append(task1)
|
||||
do_download_(r, task1, show_msg, file_size)
|
||||
|
||||
if show_msg:
|
||||
if mission.result is False:
|
||||
print(f'下载失败 {mission.info}')
|
||||
elif mission.result == 'success':
|
||||
print('\r100% ', end='')
|
||||
print(f'{"已覆盖" if mission._overwrote else "下载完成"} {mission.info}')
|
||||
elif mission.result == 'skipped':
|
||||
print(f'已跳过 {mission.info}')
|
||||
print()
|
||||
|
||||
self._print_mode = tmp
|
||||
return mission.result, mission.info
|
||||
|
||||
def by_browser(self, url, save_path=None, rename=None, suffix=None, timeout=None, file_exists=None):
|
||||
if hasattr(self.page, '_download_by_browser'):
|
||||
return self.page._download_by_browser(url=url, save_path=save_path, rename=rename, suffix=suffix,
|
||||
timeout=timeout, file_exists=file_exists)
|
||||
else:
|
||||
raise RuntimeError('非浏览器调用无法使用浏览器下载。')
|
||||
|
||||
def _run_or_wait(self, mission):
|
||||
thread_id = self._get_usable_thread()
|
||||
if thread_id is None:
|
||||
self._waiting_list.put(mission)
|
||||
else:
|
||||
thread = Thread(target=self._run, args=(thread_id, mission), daemon=False)
|
||||
self._threads[thread_id] = {'thread': thread, 'mission': None}
|
||||
thread.start()
|
||||
|
||||
def _run(self, ID, mission):
|
||||
while True:
|
||||
if not mission: # 如果没有任务,就从等候列表中取一个
|
||||
if self._waiting_list.empty():
|
||||
break
|
||||
else:
|
||||
try:
|
||||
mission = self._waiting_list.get(True, .5)
|
||||
except Exception:
|
||||
self._waiting_list.task_done()
|
||||
break
|
||||
|
||||
self._threads[ID]['mission'] = mission
|
||||
self._download(mission, ID)
|
||||
mission = None
|
||||
|
||||
self._threads[ID] = None
|
||||
|
||||
def get_mission(self, mission_or_id):
|
||||
return self._missions[mission_or_id] if isinstance(mission_or_id, int) else mission_or_id
|
||||
|
||||
def get_failed_missions(self):
|
||||
return [i for i in self._missions.values() if i.result is False]
|
||||
|
||||
def wait(self, mission=None, show=False, timeout=None):
|
||||
timeout = 0 if timeout is None else timeout
|
||||
if mission:
|
||||
return self.get_mission(mission).wait(show, timeout)
|
||||
|
||||
else:
|
||||
if show:
|
||||
self.show(False)
|
||||
else:
|
||||
end_time = perf_counter() + timeout
|
||||
while self.is_running and (perf_counter() < end_time or timeout == 0):
|
||||
sleep(0.1)
|
||||
|
||||
def cancel(self):
|
||||
for m in self._missions.values():
|
||||
m.cancel()
|
||||
|
||||
def show(self, asyn=True, keep=False):
|
||||
if asyn:
|
||||
Thread(target=self._show, args=(2, keep)).start()
|
||||
else:
|
||||
self._show(0.1, keep)
|
||||
|
||||
def _show(self, wait, keep=False):
|
||||
self._stop_printing = False
|
||||
|
||||
if keep:
|
||||
Thread(target=self._stop_show).start()
|
||||
|
||||
end_time = perf_counter() + wait
|
||||
while not self._stop_printing and (keep or self.is_running or perf_counter() < end_time):
|
||||
print(f'\033[K', end='')
|
||||
print(f'等待任务数:{self._waiting_list.qsize()}')
|
||||
for k, v in self._threads.items():
|
||||
m = v['mission'] if v else None
|
||||
if m:
|
||||
items = (m.mission.rate, m.mid) if isinstance(m, Task) else (m.rate, m.id)
|
||||
path = f'M{items[1]} {items[0]}% {m}'
|
||||
else:
|
||||
path = '空闲'
|
||||
print(f'\033[K', end='')
|
||||
print(f'线程{k}:{path}')
|
||||
|
||||
print(f'\033[{self.roads + 1}A\r', end='')
|
||||
sleep(0.4)
|
||||
|
||||
print(f'\033[1B', end='')
|
||||
for i in range(self.roads):
|
||||
print(f'\033[K', end='')
|
||||
print(f'线程{i}:空闲')
|
||||
|
||||
print()
|
||||
|
||||
def _connect(self, url, session, _headers, method, encoding, **kwargs):
|
||||
kwargs = CaseInsensitiveDict(kwargs)
|
||||
if 'headers' in kwargs: # 不知道为什么添加这个才能正常使用多线程
|
||||
kwargs['headers'] = CaseInsensitiveDict({**_headers, **kwargs['headers']})
|
||||
else:
|
||||
kwargs['headers'] = _headers
|
||||
|
||||
r = err = None
|
||||
for i in range(self.retry + 1):
|
||||
try:
|
||||
if method == 'get':
|
||||
r = session.get(url, **kwargs)
|
||||
elif method == 'post':
|
||||
r = session.post(url, **kwargs)
|
||||
|
||||
if r:
|
||||
return set_charset(r, encoding), 'Success'
|
||||
|
||||
except Exception as e:
|
||||
err = e
|
||||
|
||||
if r and r.status_code in (403, 404):
|
||||
break
|
||||
if i < self.retry:
|
||||
sleep(self.interval)
|
||||
|
||||
# 返回失败结果
|
||||
if r is None:
|
||||
return None, '连接失败' if err is None else err
|
||||
if not r.ok:
|
||||
return r, f'状态码:{r.status_code}'
|
||||
return None
|
||||
|
||||
def _get_usable_thread(self):
|
||||
for k, v in self._threads.items():
|
||||
if v is None:
|
||||
return k
|
||||
return None
|
||||
|
||||
def _stop_show(self):
|
||||
input()
|
||||
self._stop_printing = True
|
||||
|
||||
def _when_mission_done(self, mission):
|
||||
self._running_count -= 1
|
||||
if self._print_mode == 'all' or (self._print_mode == 'failed' and mission.result is False):
|
||||
print(f'[{mission.RESULT_TEXTS[mission.result]}] {mission.info} {mission.data.url}')
|
||||
if self._log_mode == 'all' or (self._log_mode == 'failed' and mission.result is False):
|
||||
self._logger.add_data((mission.RESULT_TEXTS[mission.result], mission.data.url, mission.info,
|
||||
mission.data.save_path, mission.data.rename, mission.data.kwargs))
|
||||
mission.session.close()
|
||||
|
||||
def _download(self, mission_or_task, thread_id):
|
||||
if mission_or_task.is_done:
|
||||
return
|
||||
if mission_or_task.state == 'cancel':
|
||||
mission_or_task.state = 'done'
|
||||
return
|
||||
|
||||
file_url = mission_or_task.data.url
|
||||
if isinstance(mission_or_task, Task):
|
||||
kwargs = copy(mission_or_task.data.kwargs)
|
||||
kwargs['headers']['Range'] = f"bytes={mission_or_task.range[0]}-{mission_or_task.range[1]}"
|
||||
r, inf = self._connect(file_url, mission_or_task.mission.session, mission_or_task.mission.headers,
|
||||
mission_or_task.mission.method, mission_or_task.mission.encoding, **kwargs)
|
||||
|
||||
if r:
|
||||
do_download(r, mission_or_task, False)
|
||||
else:
|
||||
mission_or_task._set_done(False, inf)
|
||||
|
||||
return
|
||||
|
||||
# ===================开始处理mission====================
|
||||
mission = mission_or_task
|
||||
ok, r, file_size = handle_mission(mission, file_url)
|
||||
if not ok:
|
||||
return
|
||||
|
||||
# -------------------设置分块任务-------------------
|
||||
first = False
|
||||
if mission.data.split and file_size and file_size > self.block_size and r.headers.get(
|
||||
'Accept-Ranges') == 'bytes':
|
||||
first = True
|
||||
chunks = [[s, min(s + self.block_size, file_size) - 1] for s in range(0, file_size, self.block_size)]
|
||||
chunks[-1][-1] = ''
|
||||
chunks_len = len(chunks)
|
||||
|
||||
task1 = Task(mission, chunks[0], f'1/{chunks_len}', chunks[0][1] - chunks[0][0])
|
||||
mission.tasks_count = chunks_len
|
||||
mission.tasks = [task1]
|
||||
|
||||
for ind, chunk in enumerate(chunks[1:], 2):
|
||||
s = file_size - chunk[0] if chunks_len == ind else chunk[1] - chunk[0]
|
||||
task = Task(mission, chunk, f'{ind}/{chunks_len}', s)
|
||||
mission.tasks.append(task)
|
||||
self._run_or_wait(task)
|
||||
|
||||
else: # 不分块
|
||||
task1 = Task(mission, None, '1/1', file_size)
|
||||
mission.tasks.append(task1)
|
||||
|
||||
self._threads[thread_id]['mission'] = task1
|
||||
do_download(r, task1, first)
|
||||
|
||||
|
||||
def handle_mission(mission, url):
|
||||
mission.info = '下载中'
|
||||
mission.state = 'running'
|
||||
kwargs = mission.data.kwargs
|
||||
|
||||
rename = mission.data.rename
|
||||
suffix = mission.data.suffix
|
||||
file_exists = mission.data.file_exists
|
||||
mission.data.save_path.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
if file_exists == 'skip' and rename:
|
||||
tmp = mission.data.save_path / rename
|
||||
if tmp.exists() and tmp.is_file():
|
||||
mission.file_name = rename
|
||||
mission._set_path(mission.data.save_path / rename)
|
||||
mission._set_done('skipped', str(mission.path))
|
||||
return False, None, None
|
||||
|
||||
r, inf = mission.owner._connect(url, mission.session, mission.headers, mission.method, mission.encoding, **kwargs)
|
||||
|
||||
if mission.is_done:
|
||||
return False, None, None
|
||||
if not r:
|
||||
mission._break_mission(result=False, info=inf)
|
||||
return False, None, None
|
||||
|
||||
# -------------------获取文件信息-------------------
|
||||
file_info = get_file_info(r, mission.data.save_path, rename, suffix, file_exists,
|
||||
mission.encoding, mission.owner._lock)
|
||||
file_size = file_info['size']
|
||||
full_path = file_info['path']
|
||||
mission._set_path(full_path)
|
||||
mission.size = file_size
|
||||
mission._overwrote = file_info['overwrite']
|
||||
|
||||
if file_info['skip']:
|
||||
mission._set_done('skipped', str(mission.path))
|
||||
return False, None, None
|
||||
|
||||
full_Path = Path(full_path)
|
||||
if file_exists == 'add' and full_Path.exists():
|
||||
mission.data.offset = full_Path.stat().st_size
|
||||
|
||||
return True, r, file_size
|
||||
|
||||
|
||||
def do_download_(r, task, show, size):
|
||||
if task.is_done or task.mission.is_done:
|
||||
return
|
||||
|
||||
task.set_states(result=None, info='下载中', state='running')
|
||||
block_size = 524288 # 512k
|
||||
result = None
|
||||
|
||||
try:
|
||||
if show and size:
|
||||
for chunk in r.iter_content(chunk_size=block_size):
|
||||
if task.state in ('cancel', 'done'):
|
||||
result = 'canceled'
|
||||
task.clear_cache()
|
||||
break
|
||||
if chunk:
|
||||
task.add_data(chunk, None)
|
||||
print(f'\r{task.rate}% ', end='')
|
||||
|
||||
else:
|
||||
for chunk in r.iter_content(chunk_size=block_size):
|
||||
if task.state in ('cancel', 'done'):
|
||||
result = 'canceled'
|
||||
task.clear_cache()
|
||||
break
|
||||
if chunk:
|
||||
task.add_data(chunk, None)
|
||||
|
||||
except Exception as e:
|
||||
result, info = False, f'{r.status_code} {e}'
|
||||
|
||||
else:
|
||||
result = 'success' if result is None else result
|
||||
info = str(task.path)
|
||||
|
||||
finally:
|
||||
r.close()
|
||||
|
||||
task._set_done(result=result, info=info)
|
||||
|
||||
|
||||
def do_download(r, task, first=False):
|
||||
if task.is_done or task.mission.is_done:
|
||||
return
|
||||
|
||||
task.set_states(result=None, info='下载中', state='running')
|
||||
block_size = 524288 # 512k
|
||||
result = None
|
||||
|
||||
try:
|
||||
if first: # 分块是第一块
|
||||
if task.range[1] <= block_size or task.range[1] % block_size != 0:
|
||||
r_content = r.iter_content(chunk_size=task.range[1] + 1)
|
||||
task.add_data(next(r_content), seek=0 + task.mission.data.offset)
|
||||
if task.state in ('cancel', 'done'):
|
||||
result = 'canceled'
|
||||
task.clear_cache()
|
||||
|
||||
else:
|
||||
blocks = task.range[1] // block_size
|
||||
remainder = task.range[1] % block_size
|
||||
r_content = r.iter_content(chunk_size=block_size)
|
||||
for b in range(blocks):
|
||||
task.add_data(next(r_content), seek=b * block_size + task.mission.data.offset)
|
||||
if task.state in ('cancel', 'done'):
|
||||
result = 'canceled'
|
||||
task.clear_cache()
|
||||
break
|
||||
|
||||
if task.state in ('cancel', 'done'):
|
||||
result = 'canceled'
|
||||
task.clear_cache()
|
||||
else:
|
||||
task.add_data(next(r_content)[:remainder + 1], blocks * block_size + task.mission.data.offset)
|
||||
|
||||
else:
|
||||
if task.range is None: # 不分块
|
||||
for chunk in r.iter_content(chunk_size=block_size):
|
||||
if task.state in ('cancel', 'done'):
|
||||
result = 'canceled'
|
||||
task.clear_cache()
|
||||
break
|
||||
if chunk:
|
||||
task.add_data(chunk, None)
|
||||
|
||||
elif task.range[1] == '': # 结尾的数据块
|
||||
begin = task.range[0]
|
||||
for chunk in r.iter_content(chunk_size=block_size):
|
||||
if task.state in ('cancel', 'done'):
|
||||
result = 'canceled'
|
||||
task.clear_cache()
|
||||
break
|
||||
if chunk:
|
||||
task.add_data(chunk, seek=begin + task.mission.data.offset)
|
||||
begin += len(chunk)
|
||||
|
||||
else: # 有始末数字的数据块
|
||||
begin, end = task.range
|
||||
num = (end - begin) // block_size
|
||||
for ind, chunk in enumerate(r.iter_content(chunk_size=block_size), 1):
|
||||
if task.state in ('cancel', 'done'):
|
||||
result = 'canceled'
|
||||
task.clear_cache()
|
||||
break
|
||||
if chunk:
|
||||
task.add_data(chunk, seek=begin + task.mission.data.offset)
|
||||
if ind <= num:
|
||||
begin += block_size
|
||||
|
||||
except Exception as e:
|
||||
result, info = False, f'{r.status_code} {e}'
|
||||
|
||||
else:
|
||||
result = 'success' if result is None else result
|
||||
info = str(task.path)
|
||||
|
||||
finally:
|
||||
r.close()
|
||||
|
||||
task._set_done(result=result, info=info)
|
||||
@@ -0,0 +1,353 @@
|
||||
# -*- coding:utf-8 -*-
|
||||
from pathlib import Path
|
||||
from queue import Queue
|
||||
from threading import Lock
|
||||
from typing import Union, Tuple, Any, Literal, Optional
|
||||
|
||||
from DrissionPage import SessionOptions
|
||||
from DrissionPage._base.base import BasePage
|
||||
from DrissionPage._units.downloader import DownloadMission
|
||||
from DrissionRecord import Recorder
|
||||
from requests import Session, Response
|
||||
from requests.structures import CaseInsensitiveDict
|
||||
|
||||
from ._funcs import FileExistsSetter, PathSetter, BlockSizeSetter
|
||||
from .mission import Task, Mission, BaseTask
|
||||
from .setter import Setter
|
||||
|
||||
FILE_EXISTS = Literal['add', 'skip', 'rename', 'overwrite', 'a', 's', 'r', 'o']
|
||||
|
||||
|
||||
class DrissionGet(object):
|
||||
file_exists: FileExistsSetter = ...
|
||||
save_path: PathSetter = ...
|
||||
block_size: BlockSizeSetter = ...
|
||||
|
||||
_roads: int = ...
|
||||
_setter: Optional[Setter] = ...
|
||||
_print_mode: Optional[str] = ...
|
||||
_log_mode: Optional[str] = ...
|
||||
_logger: Optional[Recorder] = ...
|
||||
_retry: Optional[int] = ...
|
||||
_interval: Optional[float] = ...
|
||||
page: Optional[BasePage] = ...
|
||||
_waiting_list: Queue = ...
|
||||
_session: Session = ...
|
||||
_running_count: int = ...
|
||||
_missions_num: int = ...
|
||||
_missions: dict = ...
|
||||
_threads: dict = ...
|
||||
_timeout: Union[int, float, None] = ...
|
||||
_stop_printing: bool = ...
|
||||
_lock: Lock = ...
|
||||
split: bool = ...
|
||||
_encoding: Optional[str] = ...
|
||||
|
||||
def __init__(self,
|
||||
save_path: Union[str, Path] = None,
|
||||
roads: int = 10,
|
||||
driver: Union[Session, BasePage, SessionOptions] = None,
|
||||
file_exists: FILE_EXISTS = 'rename'):
|
||||
"""
|
||||
:param save_path: 文件保存路径
|
||||
:param roads: 可同时运行的线程数
|
||||
:param driver: 使用的Session对象,或配置对象、页面对象等
|
||||
:param file_exists: 有同名文件名时的处理方式,可选 'skip', 'overwrite', 'rename', 'add', 's', 'o', 'r', 'a'
|
||||
"""
|
||||
...
|
||||
|
||||
def __call__(self,
|
||||
url: str,
|
||||
save_path: Union[str, Path, None] = None,
|
||||
rename: str = None,
|
||||
suffix: Optional[str] = None,
|
||||
file_exists: FILE_EXISTS = None,
|
||||
show_msg: bool = True,
|
||||
split: bool = False,
|
||||
timeout: Optional[float] = None,
|
||||
params: Optional[dict] = ...,
|
||||
data: Any = ...,
|
||||
json: Any = ...,
|
||||
headers: Optional[dict] = ...,
|
||||
cookies: Any = ...,
|
||||
files: Any = ...,
|
||||
auth: Any = ...,
|
||||
allow_redirects: bool = ...,
|
||||
proxies: Optional[dict] = ...,
|
||||
hooks: Any = ...,
|
||||
stream: Any = ...,
|
||||
verify: Any = ...,
|
||||
cert: Any = ...) -> tuple:
|
||||
"""以阻塞的方式下载一个文件并返回结果
|
||||
:param url: 文件网址
|
||||
:param save_path: 保存路径
|
||||
:param rename: 重命名的文件名
|
||||
:param suffix: 指定后缀名
|
||||
:param file_exists: 遇到同名文件时的处理方式,可选 'skip', 'overwrite', 'rename', 'add', 's', 'o', 'r', 'a',默认跟随实例属性
|
||||
:param split: 是否分块下载,为None使用设置
|
||||
:param show_msg: 是否打印进度
|
||||
:param kwargs: 连接参数
|
||||
:return: 任务结果和信息组成的tuple
|
||||
"""
|
||||
...
|
||||
|
||||
@property
|
||||
def set(self) -> Setter:
|
||||
"""用于设置打印和记录模式的对象"""
|
||||
...
|
||||
|
||||
@property
|
||||
def roads(self) -> int:
|
||||
"""可同时运行的线程数"""
|
||||
...
|
||||
|
||||
@property
|
||||
def retry(self) -> int:
|
||||
"""返回连接失败时重试次数"""
|
||||
...
|
||||
|
||||
@property
|
||||
def interval(self) -> float:
|
||||
"""返回连接失败时重试间隔"""
|
||||
...
|
||||
|
||||
@property
|
||||
def timeout(self) -> float:
|
||||
"""返回连接超时时间"""
|
||||
...
|
||||
|
||||
@property
|
||||
def waiting_list(self) -> Queue:
|
||||
"""返回等待队列"""
|
||||
...
|
||||
|
||||
@property
|
||||
def session(self) -> Session:
|
||||
"""返回用于保存默认连接设置的Session对象"""
|
||||
...
|
||||
|
||||
@property
|
||||
def is_running(self) -> bool:
|
||||
"""返回是否有线程还在运行"""
|
||||
...
|
||||
|
||||
@property
|
||||
def missions(self) -> dict:
|
||||
"""用list方式返回所有任务对象"""
|
||||
...
|
||||
|
||||
@property
|
||||
def encoding(self) -> Optional[str]:
|
||||
...
|
||||
|
||||
def add(self,
|
||||
url: str,
|
||||
save_path: Union[str, Path, None] = None,
|
||||
rename: str = None,
|
||||
suffix: str = None,
|
||||
file_exists: FILE_EXISTS = None,
|
||||
split: bool = None,
|
||||
timeout: Optional[float] = None,
|
||||
params: Optional[dict] = ...,
|
||||
data: Any = None,
|
||||
json: Union[dict, str, None] = ...,
|
||||
headers: Optional[dict] = ...,
|
||||
cookies: Any = ...,
|
||||
files: Any = ...,
|
||||
auth: Any = ...,
|
||||
allow_redirects: bool = ...,
|
||||
proxies: Optional[dict] = ...,
|
||||
hooks: Any = ...,
|
||||
stream: Any = ...,
|
||||
verify: Any = ...,
|
||||
cert: Any = ...) -> Mission:
|
||||
"""添加一个下载任务并将其返回
|
||||
:param url: 文件网址
|
||||
:param save_path: 保存路径
|
||||
:param rename: 重命名的文件名
|
||||
:param suffix: 重命名的文件后缀名
|
||||
:param file_exists: 遇到同名文件时的处理方式,可选 'skip', 'overwrite', 'rename', 'add', 's', 'o', 'r', 'a',默认跟随实例属性
|
||||
:param split: 是否允许多线程分块下载,为None则使用对象属性
|
||||
:param kwargs: 连接参数
|
||||
:return: 任务对象
|
||||
"""
|
||||
...
|
||||
|
||||
def download(self,
|
||||
url: str,
|
||||
save_path: Union[str, Path, None] = None,
|
||||
rename: str = None,
|
||||
suffix: str = None,
|
||||
file_exists: FILE_EXISTS = None,
|
||||
split: bool = False,
|
||||
show_msg: bool = None,
|
||||
timeout: Optional[float] = None,
|
||||
params: Optional[dict] = ...,
|
||||
data: Any = ...,
|
||||
json: Any = ...,
|
||||
headers: Optional[dict] = ...,
|
||||
cookies: Any = ...,
|
||||
files: Any = ...,
|
||||
auth: Any = ...,
|
||||
allow_redirects: bool = ...,
|
||||
proxies: Optional[dict] = ...,
|
||||
hooks: Any = ...,
|
||||
stream: Any = ...,
|
||||
verify: Any = ...,
|
||||
cert: Any = ...) -> tuple:
|
||||
"""以阻塞的方式下载一个文件并返回结果
|
||||
:param url: 文件网址
|
||||
:param save_path: 保存路径
|
||||
:param rename: 重命名的文件名
|
||||
:param suffix: 重命名的文件后缀名
|
||||
:param file_exists: 遇到同名文件时的处理方式,可选 'skip', 'overwrite', 'rename', 'add', 's', 'o', 'r', 'a',默认跟随实例属性
|
||||
:param split: 是否分块下载,为None使用设置
|
||||
:param show_msg: 是否打印进度,为None使用设置
|
||||
:param kwargs: 连接参数
|
||||
:return: 任务结果和信息组成的tuple
|
||||
"""
|
||||
...
|
||||
|
||||
def by_browser(self,
|
||||
url: str,
|
||||
save_path: Union[str, Path] = None,
|
||||
rename: str = None, suffix: str = None,
|
||||
timeout: float = None,
|
||||
file_exists: Literal['rename', 'overwrite', 'skip', 'r', 'o', 's'] = None) -> Union[
|
||||
DownloadMission, False]:
|
||||
"""触发浏览器下载
|
||||
:param url: 下载地址
|
||||
:param save_path: 保存路径,为None保存在原来设置的,如未设置保存到当前路径
|
||||
:param rename: 重命名文件名
|
||||
:param suffix: 指定文件后缀
|
||||
:param timeout: 等待下载触发的超时时间,为None则使用页面对象设置
|
||||
:param file_exists: 可在 'rename', 'overwrite', 'skip', 'r', 'o', 's'中选择
|
||||
:return: DownloadMission对象,任务无触发时返回False
|
||||
"""
|
||||
...
|
||||
|
||||
def _run_or_wait(self, mission: BaseTask) -> None:
|
||||
"""接收任务,有空线程则运行,没有则进入等待队列
|
||||
:param mission: 任务对象
|
||||
:return: None
|
||||
"""
|
||||
...
|
||||
|
||||
def _run(self, ID: int, mission: BaseTask) -> None:
|
||||
"""线程函数
|
||||
:param ID: 线程id
|
||||
:param mission: 任务对象,Mission或Task
|
||||
:return: None
|
||||
"""
|
||||
...
|
||||
|
||||
def get_mission(self, mission_or_id: Union[int, Mission]) -> Mission:
|
||||
"""根据id值获取一个任务
|
||||
:param mission_or_id: 任务或任务id
|
||||
:return: 任务对象
|
||||
"""
|
||||
...
|
||||
|
||||
def get_failed_missions(self) -> list:
|
||||
"""返回失败任务列表"""
|
||||
...
|
||||
|
||||
def wait(self,
|
||||
mission: Union[int, Mission] = None,
|
||||
show: bool = False,
|
||||
timeout: float = None) -> Optional[tuple]:
|
||||
"""等待所有或指定任务完成
|
||||
:param mission: 任务对象或任务id,为None时等待所有任务结束
|
||||
:param show: 是否显示进度
|
||||
:param timeout: 超时时间,None或0为无限
|
||||
:return: 任务结果和信息组成的tuple
|
||||
"""
|
||||
...
|
||||
|
||||
def cancel(self) -> None:
|
||||
"""取消所有等待中或执行中的任务"""
|
||||
...
|
||||
|
||||
def show(self, asyn: bool = True, keep: bool = False) -> None:
|
||||
"""实时显示所有线程进度
|
||||
:param asyn: 是否以异步方式显示
|
||||
:param keep: 任务列表为空时是否保持显示
|
||||
:return: None
|
||||
"""
|
||||
...
|
||||
|
||||
def _show(self, wait: float, keep: bool = False) -> None:
|
||||
"""实时显示所有线程进度
|
||||
:param wait: 超时时间(秒)
|
||||
:param keep: 任务列表为空时是否保持显示
|
||||
:return: None
|
||||
"""
|
||||
...
|
||||
|
||||
def _connect(self, url: str, session: Session, _headers: CaseInsensitiveDict,
|
||||
method: str, encoding: Optional[str], **kwargs) -> Tuple[Union[Response, None], str]:
|
||||
"""生成response对象
|
||||
:param url: 目标url
|
||||
:param session: 用于连接的Session对象
|
||||
:param _headers: 内置的headers参数
|
||||
:param method: 请求方式
|
||||
:param encoding: 编码格式
|
||||
:param kwargs: 连接参数
|
||||
:return: tuple,第一位为Response或None,第二位为出错信息或'Success'
|
||||
"""
|
||||
...
|
||||
|
||||
def _get_usable_thread(self) -> Optional[int]:
|
||||
"""获取可用线程,没有则返回None"""
|
||||
...
|
||||
|
||||
def _stop_show(self) -> None:
|
||||
"""设置停止打印的变量"""
|
||||
...
|
||||
|
||||
def _when_mission_done(self, mission: Mission) -> None:
|
||||
"""当任务完成时执行的操作
|
||||
:param mission: 完结的任务
|
||||
:return: None
|
||||
"""
|
||||
...
|
||||
|
||||
def _download(self,
|
||||
mission_or_task: Union[Mission, Task],
|
||||
thread_id: int) -> None:
|
||||
"""此方法是执行下载的线程方法,用于根据任务下载文件
|
||||
:param mission_or_task: 下载任务对象
|
||||
:param thread_id: 线程号
|
||||
:return: None
|
||||
"""
|
||||
...
|
||||
|
||||
|
||||
def handle_mission(mission: Mission, url: str) -> tuple:
|
||||
"""对任务进行预处理,创建连接、获取信息
|
||||
:param mission: 下载任务对象
|
||||
:param url: 文件url
|
||||
:return: (是否继续, Response对象, 文件大小)
|
||||
"""
|
||||
...
|
||||
|
||||
|
||||
def do_download_(r, task: Task, show, size) -> None:
|
||||
"""执行download()触发的下载任务
|
||||
:param r: Response对象
|
||||
:param task: 任务对象
|
||||
:param show: 是否打印进度
|
||||
:param size: 文件大小
|
||||
:return: None
|
||||
"""
|
||||
...
|
||||
|
||||
|
||||
def do_download(r: Response, task: Task, first: bool = False) -> None:
|
||||
"""执行下载任务
|
||||
:param r: Response对象
|
||||
:param task: 任务对象
|
||||
:param first: 是否第一个分块
|
||||
:return: None
|
||||
"""
|
||||
...
|
||||
@@ -0,0 +1,280 @@
|
||||
# -*- coding:utf-8 -*-
|
||||
from time import sleep, perf_counter
|
||||
from urllib.parse import quote, urlparse
|
||||
|
||||
from DrissionRecord import Recorder
|
||||
from requests.structures import CaseInsensitiveDict
|
||||
|
||||
from ._funcs import copy_session, set_session_cookies
|
||||
|
||||
|
||||
class MissionData(object):
|
||||
def __init__(self, url, save_path, rename, suffix, file_exists, split, kwargs, offset=0):
|
||||
self.url = quote(url, safe='-_.~!*\'"();:@&=+$,/\\?#[]%')
|
||||
self.save_path = save_path
|
||||
self.rename = rename
|
||||
self.suffix = suffix
|
||||
self.file_exists = file_exists
|
||||
self.split = split
|
||||
self.kwargs = kwargs
|
||||
self.offset = offset
|
||||
|
||||
|
||||
class BaseTask(object):
|
||||
_DONE = 'done'
|
||||
RESULT_TEXTS = {'success': '成功', 'skipped': '跳过', 'canceled': '取消', False: '失败', None: '未知'}
|
||||
|
||||
def __init__(self, ID):
|
||||
self._id = ID
|
||||
self.state = 'waiting' # 'waiting', 'running', 'done'
|
||||
self.result = None # 'success', 'skipped', 'canceled', False, None
|
||||
self.info = '等待下载' # 信息
|
||||
|
||||
@property
|
||||
def id(self):
|
||||
return self._id
|
||||
|
||||
@property
|
||||
def data(self):
|
||||
return
|
||||
|
||||
@property
|
||||
def is_done(self):
|
||||
return self.state in ('done', 'cancel')
|
||||
|
||||
def set_states(self, result=None, info=None, state='done'):
|
||||
self.result = result
|
||||
self.info = info
|
||||
self.state = state
|
||||
|
||||
|
||||
class Mission(BaseTask):
|
||||
def __init__(self, ID, owner, file_url, save_path, rename, suffix, file_exists, split, encoding, kwargs):
|
||||
super().__init__(ID)
|
||||
self.owner = owner
|
||||
self.size = None
|
||||
|
||||
self.tasks = []
|
||||
self.tasks_count = 1
|
||||
self.done_tasks_count = 0
|
||||
|
||||
self.file_name = None
|
||||
self._path = None # 文件完整路径,Path对象
|
||||
self._recorder = None
|
||||
self.encoding = encoding
|
||||
|
||||
self._set_session()
|
||||
kwargs = self._handle_kwargs(file_url, kwargs)
|
||||
self._data = MissionData(file_url, save_path, rename, suffix, file_exists, split, kwargs)
|
||||
self._overwrote = False
|
||||
self.method = 'post' if (self._data.kwargs.get('data', None) is not None or
|
||||
self._data.kwargs.get('json', None) is not None) else 'get'
|
||||
|
||||
def __repr__(self):
|
||||
return f'<Mission {self.id} {self.info} {self.file_name}>'
|
||||
|
||||
@property
|
||||
def data(self):
|
||||
return self._data
|
||||
|
||||
@property
|
||||
def path(self):
|
||||
return self._path
|
||||
|
||||
@property
|
||||
def recorder(self):
|
||||
if self._recorder is None:
|
||||
self._recorder = Recorder(cache_size=100)
|
||||
self._recorder.set.show_msg(False)
|
||||
return self._recorder
|
||||
|
||||
@property
|
||||
def rate(self):
|
||||
if not self.size:
|
||||
return None
|
||||
c = 0
|
||||
for t in self.tasks:
|
||||
c += t._downloaded_size if t._downloaded_size else 0
|
||||
return round((c / self.size) * 100, 2)
|
||||
|
||||
def cancel(self) -> None:
|
||||
self._break_mission('canceled', '已取消')
|
||||
|
||||
def del_file(self):
|
||||
if self.path and self.path.exists():
|
||||
try:
|
||||
self.path.unlink()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
def wait(self, show=True, timeout=0):
|
||||
if show:
|
||||
print(f'url:{self.data.url}')
|
||||
t2 = perf_counter()
|
||||
while self.file_name is None and perf_counter() - t2 < 4:
|
||||
sleep(0.01)
|
||||
print(f'文件名:{self.file_name}')
|
||||
print(f'目标路径:{self.path}')
|
||||
if not self.size:
|
||||
print('未知大小 ', end='')
|
||||
|
||||
t1 = perf_counter()
|
||||
while not self.is_done and (perf_counter() - t1 < timeout or timeout == 0):
|
||||
try:
|
||||
rate = round((self.path.stat().st_size / self.size) * 100, 2)
|
||||
print(f'\r{rate}% ', end='')
|
||||
except (FileNotFoundError, TypeError, ZeroDivisionError):
|
||||
pass
|
||||
sleep(0.1)
|
||||
|
||||
if self.result is False:
|
||||
print(f'下载失败 {self.info}')
|
||||
elif self.result == 'success':
|
||||
print('\r100% ', end='')
|
||||
print(f'{"已覆盖" if self._overwrote else "下载完成"} {self.info}')
|
||||
elif self.result == 'skipped':
|
||||
print(f'已跳过 {self.info}')
|
||||
print()
|
||||
|
||||
else:
|
||||
t1 = perf_counter()
|
||||
while not self.is_done and (perf_counter() - t1 < timeout or timeout == 0):
|
||||
sleep(0.1)
|
||||
|
||||
return self.result, self.info
|
||||
|
||||
def _set_session(self):
|
||||
session = copy_session(self.owner.session)
|
||||
headers = session.headers
|
||||
session.headers = None
|
||||
if self.owner.page:
|
||||
set_session_cookies(session, self.owner.page.cookies())
|
||||
if hasattr(self.owner.page, '_headers'):
|
||||
headers = CaseInsensitiveDict({**self.owner.page._headers, **headers})
|
||||
headers.update({"User-Agent": self.owner.page.user_agent})
|
||||
self.session = session
|
||||
self.headers = headers
|
||||
|
||||
def _handle_kwargs(self, url, kwargs):
|
||||
if 'timeout' not in kwargs:
|
||||
kwargs['timeout'] = self.owner.timeout
|
||||
|
||||
headers = CaseInsensitiveDict(kwargs['headers']) if 'headers' in kwargs else CaseInsensitiveDict()
|
||||
|
||||
parsed_url = urlparse(url)
|
||||
hostname = parsed_url.hostname
|
||||
scheme = parsed_url.scheme
|
||||
|
||||
if not ('Referer' in headers or 'Referer' in self.headers):
|
||||
headers['Referer'] = self.owner.page.url if self.owner.page and self.owner.page.url \
|
||||
else f'{scheme}://{hostname}'
|
||||
if not ('Host' in headers or 'Host' in self.headers):
|
||||
headers['Host'] = hostname
|
||||
kwargs['headers'] = format_headers(headers)
|
||||
if not kwargs['headers']['Host']:
|
||||
kwargs['headers'].pop('Host')
|
||||
if not kwargs['headers']['Referer']:
|
||||
kwargs['headers'].pop('Referer')
|
||||
|
||||
return kwargs
|
||||
|
||||
def _set_path(self, path):
|
||||
self.file_name = path.name
|
||||
self._path = path
|
||||
self.recorder.set.path(path, file_type='b')
|
||||
|
||||
def _set_done(self, result, info):
|
||||
if result == 'skipped':
|
||||
self.set_states(result=result, info=info, state=self._DONE)
|
||||
|
||||
elif result == 'canceled' or result is False:
|
||||
self.recorder.clear()
|
||||
self.set_states(result=result, info=info, state=self._DONE)
|
||||
|
||||
elif result == 'success':
|
||||
self.recorder.record()
|
||||
if self.size and self.path.stat().st_size < self.size:
|
||||
self.del_file()
|
||||
self.set_states(False, '下载失败', self._DONE)
|
||||
else:
|
||||
self.set_states('success', info, self._DONE)
|
||||
|
||||
self.owner._when_mission_done(self)
|
||||
|
||||
def _a_task_done(self, is_success, info):
|
||||
if self.is_done:
|
||||
return
|
||||
|
||||
if is_success is False:
|
||||
self._break_mission(False, info)
|
||||
return
|
||||
|
||||
self.done_tasks_count += 1
|
||||
if self.done_tasks_count == self.tasks_count:
|
||||
self._set_done('success', info)
|
||||
|
||||
def _break_mission(self, result, info):
|
||||
if self.is_done:
|
||||
return
|
||||
|
||||
for task in self.tasks:
|
||||
if not task.is_done:
|
||||
task.set_states(result=result, info=info, state='cancel')
|
||||
|
||||
while any((not i.is_done for i in self.tasks)):
|
||||
sleep(.3)
|
||||
|
||||
self._set_done(result, info)
|
||||
self.del_file()
|
||||
|
||||
|
||||
class Task(BaseTask):
|
||||
def __init__(self, mission, range_, ID, size):
|
||||
super().__init__(ID)
|
||||
self.mission = mission
|
||||
self.range = range_
|
||||
self.size = size
|
||||
self._downloaded_size = 0
|
||||
|
||||
def __repr__(self):
|
||||
return f'<Task M{self.mid} T{self._id} {self.rate}% {self.info} {self.file_name}>'
|
||||
|
||||
@property
|
||||
def mid(self):
|
||||
return self.mission.id
|
||||
|
||||
@property
|
||||
def data(self):
|
||||
return self.mission.data
|
||||
|
||||
@property
|
||||
def path(self):
|
||||
return self.mission.path
|
||||
|
||||
@property
|
||||
def file_name(self):
|
||||
return self.mission.file_name
|
||||
|
||||
@property
|
||||
def rate(self):
|
||||
return round((self._downloaded_size / self.size) * 100, 2) if self.size else None
|
||||
|
||||
def add_data(self, data, seek=None):
|
||||
self._downloaded_size += len(data)
|
||||
self.mission.recorder.add_data(data, coord=seek)
|
||||
|
||||
def clear_cache(self):
|
||||
self.mission.recorder.clear()
|
||||
|
||||
def _set_done(self, result, info):
|
||||
self.set_states(result=result, info=info, state=self._DONE)
|
||||
self.mission._a_task_done(result, info)
|
||||
|
||||
|
||||
def format_headers(headers):
|
||||
for k, v in headers.items():
|
||||
if k in (':method', ':scheme', ':authority', ':path'):
|
||||
headers.pop(k)
|
||||
elif v not in (None, False, True):
|
||||
headers[k] = str(v)
|
||||
return headers
|
||||
@@ -0,0 +1,259 @@
|
||||
# -*- coding:utf-8 -*-
|
||||
from pathlib import Path
|
||||
from typing import Union, List, Optional
|
||||
|
||||
from DrissionRecord import Recorder
|
||||
from requests import Session
|
||||
from requests.structures import CaseInsensitiveDict
|
||||
|
||||
from .drissionGet import DrissionGet
|
||||
|
||||
|
||||
class MissionData(object):
|
||||
"""保存任务数据的对象"""
|
||||
url: str = ...
|
||||
save_path: Path = ...
|
||||
rename: Optional[str] = ...
|
||||
suffix: Optional[str] = ...
|
||||
file_exists: str = ...
|
||||
split: bool = ...
|
||||
kwargs: dict = ...
|
||||
offset: int = ...
|
||||
|
||||
def __init__(self, url: str, save_path: Path, rename: Optional[str], suffix: Optional[str],
|
||||
file_exists: str, split: bool, kwargs: dict, offset: int = 0):
|
||||
"""保存任务数据的对象
|
||||
:param url: 下载文件url
|
||||
:param save_path: 保存文件夹
|
||||
:param rename: 文件重命名
|
||||
:param suffix: 文件重命名后缀名
|
||||
:param file_exists: 存在重名文件时处理方式
|
||||
:param split: 是否允许分块下载
|
||||
:param kwargs: requests其它参数
|
||||
:param offset: 文件存储偏移量
|
||||
"""
|
||||
...
|
||||
|
||||
|
||||
class BaseTask(object):
|
||||
"""任务类基类"""
|
||||
_DONE: str = ...
|
||||
RESULT_TEXTS: dict = ...
|
||||
|
||||
_id: str = ...
|
||||
state: str = ...
|
||||
result: Union[str, False, None] = ...
|
||||
info: str = ...
|
||||
|
||||
def __init__(self, ID: Union[int, str]):
|
||||
"""任务类基类
|
||||
:param ID: 任务id
|
||||
"""
|
||||
...
|
||||
|
||||
@property
|
||||
def id(self) -> Union[int, str]:
|
||||
"""返回任务或子任务id"""
|
||||
...
|
||||
|
||||
@property
|
||||
def data(self):
|
||||
"""返回任务数据"""
|
||||
...
|
||||
|
||||
@property
|
||||
def is_done(self) -> bool:
|
||||
"""返回任务是否结束"""
|
||||
...
|
||||
|
||||
def set_states(self,
|
||||
result: Union[bool, str, None] = None,
|
||||
info: str = None,
|
||||
state: str = 'done') -> None:
|
||||
"""设置任务结果值
|
||||
:param result: 结果:'success', 'skipped', 'canceled', False, None
|
||||
:param info: 任务信息
|
||||
:param state: 任务状态:'waiting', 'running', 'done'
|
||||
:return: None
|
||||
"""
|
||||
...
|
||||
|
||||
|
||||
class Mission(BaseTask):
|
||||
"""任务类"""
|
||||
file_name: Optional[str] = ...
|
||||
_data: MissionData = ...
|
||||
_path: Optional[Path] = ...
|
||||
_recorder: Optional[Recorder] = ...
|
||||
size: Optional[float] = ...
|
||||
done_tasks_count: int = ...
|
||||
tasks_count: int = ...
|
||||
tasks: List[Task] = ...
|
||||
owner: DrissionGet = ...
|
||||
session: Session = ...
|
||||
headers: CaseInsensitiveDict = ...
|
||||
method: str = ...
|
||||
encoding: Optional[str] = ...
|
||||
_overwrote: bool = ...
|
||||
|
||||
def __init__(self, ID: int, owner: DrissionGet, file_url: str, save_path: Path, rename: str,
|
||||
suffix: str, file_exists: str, split: bool, encoding: Optional[str], kwargs: dict):
|
||||
"""任务类
|
||||
:param ID: 任务id
|
||||
:param owner: 所属DrissionGet对象
|
||||
:param file_url: 文件网址
|
||||
:param save_path: 保存文件夹路径
|
||||
:param rename: 重命名
|
||||
:param suffix: 重命名后缀名
|
||||
:param file_exists: 存在同名文件处理方式
|
||||
:param split: 是否分块下载
|
||||
:param encoding: 编码格式
|
||||
:param kwargs: 连接参数
|
||||
"""
|
||||
...
|
||||
|
||||
def __repr__(self) -> str:
|
||||
...
|
||||
|
||||
@property
|
||||
def data(self) -> MissionData:
|
||||
"""返回任务数据"""
|
||||
...
|
||||
|
||||
@property
|
||||
def path(self) -> Union[str, Path]:
|
||||
"""返回文件保存路径"""
|
||||
...
|
||||
|
||||
@property
|
||||
def recorder(self) -> Recorder:
|
||||
"""返回记录器对象"""
|
||||
...
|
||||
|
||||
@property
|
||||
def rate(self) -> Optional[float]:
|
||||
"""返回下载进度百分比"""
|
||||
...
|
||||
|
||||
def cancel(self) -> None:
|
||||
"""取消该任务,停止未下载完的task"""
|
||||
...
|
||||
|
||||
def del_file(self) -> None:
|
||||
"""删除下载的文件"""
|
||||
...
|
||||
|
||||
def wait(self, show: bool = True, timeout: float = 0) -> tuple:
|
||||
"""等待当前任务完成
|
||||
:param show: 是否显示下载进度
|
||||
:param timeout: 超时时间
|
||||
:return: 任务结果和信息组成的tuple
|
||||
"""
|
||||
...
|
||||
|
||||
def _set_session(self) -> Session:
|
||||
"""复制Session对象,并设置cookies"""
|
||||
...
|
||||
|
||||
def _handle_kwargs(self, url: str, kwargs: dict) -> dict:
|
||||
"""处理接收到的参数
|
||||
:param url: 要访问的url
|
||||
:param kwargs: 传入的参数dict
|
||||
:return: 处理后的参数dict
|
||||
"""
|
||||
...
|
||||
|
||||
def _set_path(self, path: Path) -> None:
|
||||
"""设置文件保存路径"""
|
||||
...
|
||||
|
||||
def _set_done(self, result: Union[bool, str, None], info: str) -> None:
|
||||
"""设置一个任务为done状态
|
||||
:param result: 结果:'success'、'skipped'、'canceled'、False、None
|
||||
:param info: 任务信息
|
||||
:return: None
|
||||
"""
|
||||
...
|
||||
|
||||
def _a_task_done(self, is_success: bool, info: str) -> None:
|
||||
"""当一个task完成时调用
|
||||
:param is_success: 该task是否成功
|
||||
:param info: 该task传入的信息
|
||||
:return: None
|
||||
"""
|
||||
...
|
||||
|
||||
def _break_mission(self, result: Union[bool, str, None], info: str) -> None:
|
||||
"""中止该任务,停止未下载完的task
|
||||
:param result: 结果:'success'、'skipped'、'canceled'、False、None
|
||||
:param info: 任务信息
|
||||
:return: None
|
||||
"""
|
||||
...
|
||||
|
||||
|
||||
class Task(BaseTask):
|
||||
"""子任务类"""
|
||||
mission: Mission = ...
|
||||
range: Optional[list] = ...
|
||||
size: Optional[int] = ...
|
||||
_downloaded_size: int = 0
|
||||
|
||||
def __init__(self, mission: Mission, range_: Optional[list], ID: str, size: Optional[int]):
|
||||
"""子任务类
|
||||
:param mission: 父任务对象
|
||||
:param range_: 读取文件数据范围
|
||||
:param ID: 任务id
|
||||
"""
|
||||
...
|
||||
|
||||
def __repr__(self) -> str:
|
||||
...
|
||||
|
||||
@property
|
||||
def mid(self) -> int:
|
||||
"""返回父任务id"""
|
||||
...
|
||||
|
||||
@property
|
||||
def data(self) -> MissionData:
|
||||
"""返回任务数据对象"""
|
||||
...
|
||||
|
||||
@property
|
||||
def path(self) -> str:
|
||||
"""返回文件保存路径"""
|
||||
...
|
||||
|
||||
@property
|
||||
def file_name(self) -> str:
|
||||
"""返回文件名"""
|
||||
...
|
||||
|
||||
@property
|
||||
def rate(self) -> Optional[float]:
|
||||
"""返回下载进度百分比"""
|
||||
...
|
||||
|
||||
def add_data(self, data: bytes, seek: int = None) -> None:
|
||||
"""把数据输入到记录器
|
||||
:param data: 文件字节数据
|
||||
:param seek: 在文件中的位置,None表示最后
|
||||
:return: None
|
||||
"""
|
||||
...
|
||||
|
||||
def clear_cache(self) -> None:
|
||||
"""清除以接收但未写入硬盘的缓存"""
|
||||
...
|
||||
|
||||
def _set_done(self, result: Union[bool, str, None], info: str) -> None:
|
||||
"""设置一个子任务为done状态
|
||||
:param result: 结果:'success'、'skipped'、'canceled'、False、None
|
||||
:param info: 任务信息
|
||||
:return: None
|
||||
"""
|
||||
...
|
||||
|
||||
|
||||
def format_headers(headers: CaseInsensitiveDict): ...
|
||||
@@ -0,0 +1,122 @@
|
||||
# -*- coding:utf-8 -*-
|
||||
from DrissionRecord import Recorder
|
||||
from requests import Session
|
||||
|
||||
from ._funcs import get_file_exists_mode, get_save_path
|
||||
|
||||
|
||||
class Setter(object):
|
||||
def __init__(self, owner):
|
||||
self._owner = owner
|
||||
|
||||
@property
|
||||
def if_file_exists(self):
|
||||
return FileExists(self)
|
||||
|
||||
def log(self, path='DrissionGet_log.csv', failed_only=False):
|
||||
if not path:
|
||||
self._owner._logger = self._owner._log_mode = None
|
||||
return self
|
||||
if self._owner._logger:
|
||||
self._owner._logger.set.path(path)
|
||||
else:
|
||||
self._owner._logger = Recorder(path, cache_size=1)
|
||||
self._owner._log_mode = 'failed' if failed_only else 'all'
|
||||
return self
|
||||
|
||||
def show_msg(self, on_off=True, failed_only=False):
|
||||
self._owner._print_mode = 'failed' if failed_only else 'all' if on_off else None
|
||||
return self
|
||||
|
||||
def driver(self, driver):
|
||||
if driver is None:
|
||||
self._owner._session = Session()
|
||||
return self
|
||||
|
||||
elif isinstance(driver, Session):
|
||||
self._owner._session = driver
|
||||
return self
|
||||
|
||||
_type = str(type(driver))
|
||||
if _type.startswith('<SessionOptions '):
|
||||
self._owner._session, headers = driver.make_session()
|
||||
self._owner._session.headers = headers
|
||||
return self
|
||||
|
||||
else:
|
||||
self._owner._session = driver.session if hasattr(driver, 'session') else Session()
|
||||
self._owner.page = driver
|
||||
return self
|
||||
|
||||
def roads(self, num):
|
||||
if self._owner.is_running:
|
||||
print('有任务未完成时不能改变roads。')
|
||||
return self
|
||||
if num != self._owner.roads:
|
||||
self._owner._roads = num
|
||||
self._owner._threads = {i: None for i in range(num)}
|
||||
return self
|
||||
|
||||
def retry(self, times):
|
||||
if not isinstance(times, int) or times < 0:
|
||||
raise TypeError('times参数只能接受int格式且不能小于0。')
|
||||
self._owner._retry = times
|
||||
return self
|
||||
|
||||
def interval(self, seconds):
|
||||
if not isinstance(seconds, (int, float)) or seconds < 0:
|
||||
raise TypeError('seconds参数只能接受int或float格式且不能小于0。')
|
||||
self._owner._interval = seconds
|
||||
return self
|
||||
|
||||
def timeout(self, seconds):
|
||||
if not isinstance(seconds, (int, float)) or seconds < 0:
|
||||
raise TypeError('seconds参数只能接受int或float格式且不能小于0。')
|
||||
self._owner._timeout = seconds
|
||||
return self
|
||||
|
||||
def save_path(self, path):
|
||||
self._owner.save_path = get_save_path(path)
|
||||
return self
|
||||
|
||||
def split(self, on_off=True):
|
||||
self._owner.split = on_off
|
||||
return self
|
||||
|
||||
def block_size(self, size):
|
||||
self._owner.block_size = size
|
||||
return self
|
||||
|
||||
def proxies(self, http=None, https=None):
|
||||
self._owner._session.proxies = {'http': http, 'https': https}
|
||||
return self
|
||||
|
||||
def encoding(self, encoding):
|
||||
self._owner._encoding = encoding if encoding else None
|
||||
return self
|
||||
|
||||
|
||||
class FileExists(object):
|
||||
|
||||
def __init__(self, setter):
|
||||
self._setter = setter
|
||||
|
||||
def __call__(self, mode):
|
||||
self._setter._owner.file_exists = get_file_exists_mode(mode)
|
||||
return self._setter
|
||||
|
||||
def skip(self):
|
||||
self._setter._owner.file_exists = 'skip'
|
||||
return self._setter
|
||||
|
||||
def rename(self):
|
||||
self._setter._owner.file_exists = 'rename'
|
||||
return self._setter
|
||||
|
||||
def overwrite(self):
|
||||
self._setter._owner.file_exists = 'overwrite'
|
||||
return self._setter
|
||||
|
||||
def add(self):
|
||||
self._setter._owner.file_exists = 'add'
|
||||
return self._setter
|
||||
@@ -0,0 +1,147 @@
|
||||
# -*- coding:utf-8 -*-
|
||||
from pathlib import Path
|
||||
from typing import Union, Literal, Optional
|
||||
|
||||
from DrissionPage import SessionOptions
|
||||
from DrissionPage._base.base import BasePage
|
||||
from requests import Session
|
||||
|
||||
from .drissionGet import DrissionGet
|
||||
|
||||
FILE_EXISTS = Literal['add', 'skip', 'rename', 'overwrite', 'a', 's', 'r', 'o']
|
||||
|
||||
|
||||
class Setter(object):
|
||||
_owner: DrissionGet = ...
|
||||
|
||||
def __init__(self, owner: DrissionGet):
|
||||
"""
|
||||
:param owner: DrissionGet对象
|
||||
"""
|
||||
...
|
||||
|
||||
@property
|
||||
def if_file_exists(self) -> FileExists:
|
||||
"""返回用于设置文件同名策略的对象"""
|
||||
...
|
||||
|
||||
def log(self, path: Union[str, Path, None] = 'DrissionGet_log.csv', failed_only: bool = False) -> Setter:
|
||||
"""设置下载完成日志
|
||||
:param path: 日志文件路径,为None或False不记录
|
||||
:param failed_only: 是否只记录失败任务
|
||||
:return: Setter对象自己
|
||||
"""
|
||||
...
|
||||
|
||||
def show_msg(self, on_off: bool = True, failed_only: bool = False) -> Setter:
|
||||
"""设置是否打印下载信息
|
||||
:param on_off: bool代表开关
|
||||
:param failed_only: 是否只显示失败任务,此设置仅多线程任务时生效
|
||||
:return: Setter对象自己
|
||||
"""
|
||||
...
|
||||
|
||||
def driver(self, driver: Union[Session, BasePage, SessionOptions]) -> Setter:
|
||||
"""设置驱动
|
||||
:param driver: Session对象或DrissionPage的页面对象
|
||||
:return: Setter对象自己
|
||||
"""
|
||||
...
|
||||
|
||||
def roads(self, num: int) -> Setter:
|
||||
"""设置可同时运行的线程数
|
||||
:param num: 线程数量
|
||||
:return: Setter对象自己
|
||||
"""
|
||||
...
|
||||
|
||||
def retry(self, times: int) -> Setter:
|
||||
"""设置连接失败时重试次数
|
||||
:param times: 重试次数
|
||||
:return: Setter对象自己
|
||||
"""
|
||||
...
|
||||
|
||||
def interval(self, seconds: float) -> Setter:
|
||||
"""设置连接失败时重试间隔
|
||||
:param seconds: 连接失败时重试间隔(秒)
|
||||
:return: Setter对象自己
|
||||
"""
|
||||
...
|
||||
|
||||
def timeout(self, seconds: float) -> Setter:
|
||||
"""设置连接超时时间
|
||||
:param seconds: 超时时间(秒)
|
||||
:return: Setter对象自己
|
||||
"""
|
||||
...
|
||||
|
||||
def save_path(self, path: Union[str, Path]) -> Setter:
|
||||
"""设置文件保存路径
|
||||
:param path: 文件路径,可以是str或Path
|
||||
:return: Setter对象自己
|
||||
"""
|
||||
...
|
||||
|
||||
def split(self, on_off: bool = True) -> Setter:
|
||||
"""设置大文件是否分块下载
|
||||
:param on_off: bool代表开关
|
||||
:return: Setter对象自己
|
||||
"""
|
||||
...
|
||||
|
||||
def block_size(self, size: Union[str, int]) -> Setter:
|
||||
"""设置分块大小
|
||||
:param size: 单位为字节,可用'K'、'M'、'G'为单位,如'50M'
|
||||
:return: Setter对象自己
|
||||
"""
|
||||
...
|
||||
|
||||
def proxies(self, http: str = None, https: str = None) -> Setter:
|
||||
"""设置代理地址及端口,例:'127.0.0.1:1080'
|
||||
:param http: http代理地址及端口
|
||||
:param https: https代理地址及端口
|
||||
:return: Setter对象自己
|
||||
"""
|
||||
...
|
||||
|
||||
def encoding(self, encoding: Optional[str]) -> Setter:
|
||||
"""设置编码
|
||||
:param encoding: 编码名称,传入None取消之前的设置
|
||||
:return: Setter对象自己
|
||||
"""
|
||||
...
|
||||
|
||||
|
||||
class FileExists(object):
|
||||
"""用于设置存在同名文件时处理方法"""
|
||||
_setter: Setter = ...
|
||||
|
||||
def __init__(self, setter: Setter):
|
||||
"""
|
||||
:param setter: Setter对象
|
||||
"""
|
||||
...
|
||||
|
||||
def __call__(self, mode: FILE_EXISTS) -> Setter:
|
||||
"""设置文件存在时的处理方式
|
||||
:param mode: 'skip', 'rename', 'overwrite', 'add', 's', 'r', 'o', 'a'
|
||||
:return: Setter 对象
|
||||
"""
|
||||
...
|
||||
|
||||
def skip(self) -> Setter:
|
||||
"""设为跳过"""
|
||||
...
|
||||
|
||||
def rename(self) -> Setter:
|
||||
"""设为重命名,文件名后加序号"""
|
||||
...
|
||||
|
||||
def overwrite(self) -> Setter:
|
||||
"""设为覆盖"""
|
||||
...
|
||||
|
||||
def add(self) -> Setter:
|
||||
"""设为追加"""
|
||||
...
|
||||
@@ -0,0 +1,28 @@
|
||||
# -*- coding:utf-8 -*-
|
||||
"""
|
||||
@Author : g1879
|
||||
@Contact : g1879@qq.com
|
||||
@Website : https://DrissionPage.cn
|
||||
@Copyright: (c) 2020 by g1879, Inc. All Rights Reserved.
|
||||
|
||||
允许任何人以个人身份使用或分发本项目源代码,但仅限于学习和合法非盈利目的。
|
||||
个人或组织如未获得版权持有人授权,不得将本项目以源代码或二进制形式用于商业行为。
|
||||
|
||||
使用本项目需满足以下条款,如使用过程中出现违反任意一项条款的情形,授权自动失效。
|
||||
* 禁止将DrissionPage应用到任何可能违反当地法律规定和道德约束的项目中
|
||||
* 禁止将DrissionPage用于任何可能有损他人利益的项目中
|
||||
* 禁止将DrissionPage用于攻击与骚扰行为
|
||||
* 遵守Robots协议,禁止将DrissionPage用于采集法律或系统Robots协议不允许的数据
|
||||
|
||||
使用DrissionPage发生的一切行为均由使用人自行负责。
|
||||
因使用DrissionPage进行任何行为所产生的一切纠纷及后果均与版权持有人无关,
|
||||
版权持有人不承担任何使用DrissionPage带来的风险和损失。
|
||||
版权持有人不对DrissionPage可能存在的缺陷导致的任何损失负任何责任。
|
||||
"""
|
||||
from ._base.chromium import Chromium
|
||||
from ._configs.chromium_options import ChromiumOptions
|
||||
from ._configs.session_options import SessionOptions
|
||||
from ._pages.chromium_page import ChromiumPage
|
||||
from ._pages.session_page import SessionPage
|
||||
from ._pages.web_page import WebPage
|
||||
from .version import __version__
|
||||
@@ -0,0 +1,17 @@
|
||||
# -*- coding:utf-8 -*-
|
||||
"""
|
||||
@Author : g1879
|
||||
@Contact : g1879@qq.com
|
||||
@Website : https://DrissionPage.cn
|
||||
@Copyright: (c) 2020 by g1879, Inc. All Rights Reserved.
|
||||
"""
|
||||
from ._base.chromium import Chromium
|
||||
from ._configs.chromium_options import ChromiumOptions
|
||||
from ._configs.session_options import SessionOptions
|
||||
from ._pages.chromium_page import ChromiumPage
|
||||
from ._pages.session_page import SessionPage
|
||||
from ._pages.web_page import WebPage
|
||||
from .version import __version__
|
||||
|
||||
|
||||
__all__ = ['WebPage', 'ChromiumPage', 'Chromium', 'ChromiumOptions', 'SessionOptions', 'SessionPage', '__version__']
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,362 @@
|
||||
# -*- coding:utf-8 -*-
|
||||
"""
|
||||
@Author : g1879
|
||||
@Contact : g1879@qq.com
|
||||
@Website : https://DrissionPage.cn
|
||||
@Copyright: (c) 2020 by g1879, Inc. All Rights Reserved.
|
||||
"""
|
||||
from abc import abstractmethod
|
||||
from copy import copy
|
||||
from pathlib import Path
|
||||
from re import sub
|
||||
from urllib.parse import quote
|
||||
|
||||
from DrissionGet import DrissionGet
|
||||
from requests import Session
|
||||
|
||||
from .._configs.session_options import SessionOptions
|
||||
from .._elements.none_element import NoneElement
|
||||
from .._functions.elements import get_frame, get_eles
|
||||
from .._functions.locator import get_loc
|
||||
from .._functions.settings import Settings as _S
|
||||
from .._functions.web import format_html
|
||||
from ..errors import ElementNotFoundError, LocatorError
|
||||
|
||||
|
||||
class BaseParser(object):
|
||||
def __call__(self, locator):
|
||||
return self.ele(locator)
|
||||
|
||||
def ele(self, locator, index=1, timeout=None):
|
||||
return self._ele(locator, timeout, index=index, method='ele()')
|
||||
|
||||
def eles(self, locator, timeout=None):
|
||||
return self._ele(locator, timeout, index=None)
|
||||
|
||||
def find(self, locators, any_one=True, first_ele=True, timeout=None):
|
||||
if 'Session' in self._type:
|
||||
timeout = 0
|
||||
if timeout is None:
|
||||
timeout = self.timeout
|
||||
r = get_eles(locators, self, any_one, first_ele, timeout)
|
||||
if any_one:
|
||||
for ele in r:
|
||||
if r[ele]:
|
||||
return ele, r[ele]
|
||||
return None, None
|
||||
return r
|
||||
|
||||
# ----------------以下属性或方法待后代实现----------------
|
||||
@property
|
||||
def html(self):
|
||||
return ''
|
||||
|
||||
def s_ele(self, locator=None):
|
||||
pass
|
||||
|
||||
def s_eles(self, locator):
|
||||
pass
|
||||
|
||||
def _ele(self, locator, timeout=None, index=1, raise_err=None, method=None):
|
||||
pass
|
||||
|
||||
def _find_elements(self, locator, timeout, index=1, relative=False, raise_err=None):
|
||||
pass
|
||||
|
||||
|
||||
class BaseElement(BaseParser):
|
||||
def __init__(self, owner=None):
|
||||
self.owner = owner
|
||||
self._type = 'BaseElement'
|
||||
|
||||
def get_frame(self, loc_or_ind, timeout=None):
|
||||
if not isinstance(loc_or_ind, (int, str, tuple)):
|
||||
raise ValueError(_S._lang.join(_S._lang.INCORRECT_TYPE_, 'loc_or_ind',
|
||||
ALLOW_TYPE=_S._lang.LOC_OR_IND, CURR_VAL=loc_or_ind))
|
||||
return get_frame(self, loc_ind_ele=loc_or_ind, timeout=timeout)
|
||||
|
||||
def _ele(self, locator, timeout=None, index=1, relative=False, raise_err=None, method=None):
|
||||
if hasattr(locator, '_type'):
|
||||
return locator
|
||||
if timeout is None:
|
||||
timeout = self.timeout
|
||||
r = self._find_elements(locator, timeout=timeout, index=index, relative=relative, raise_err=raise_err)
|
||||
if r or isinstance(r, list):
|
||||
return r
|
||||
if raise_err is True or (_S.raise_when_ele_not_found and raise_err is None):
|
||||
raise ElementNotFoundError(METHOD=method, ARGS={'locator': locator, 'index': index, 'timeout': timeout})
|
||||
|
||||
r.method = method
|
||||
r.args = {'locator': locator, 'index': index, 'timeout': timeout}
|
||||
return r
|
||||
|
||||
@property
|
||||
def timeout(self):
|
||||
return self.owner.timeout if self.owner else 10
|
||||
|
||||
@property
|
||||
def child_count(self):
|
||||
return int(self._ele('xpath:count(./*)'))
|
||||
|
||||
# ----------------以下属性或方法由后代实现----------------
|
||||
@property
|
||||
def tag(self):
|
||||
return
|
||||
|
||||
def parent(self, level_or_loc=1):
|
||||
pass
|
||||
|
||||
def next(self, index=1):
|
||||
pass
|
||||
|
||||
def nexts(self):
|
||||
pass
|
||||
|
||||
|
||||
class DrissionElement(BaseElement):
|
||||
|
||||
@property
|
||||
def link(self):
|
||||
return self.attr('href') or self.attr('src')
|
||||
|
||||
@property
|
||||
def css_path(self):
|
||||
return self._get_ele_path(xpath=False)
|
||||
|
||||
@property
|
||||
def xpath(self):
|
||||
return self._get_ele_path()
|
||||
|
||||
@property
|
||||
def comments(self):
|
||||
return self.eles('xpath:.//comment()')
|
||||
|
||||
def texts(self, text_node_only=False):
|
||||
texts = self.eles('xpath:/text()') if text_node_only else [x if isinstance(x, str) else x.text
|
||||
for x in self.eles('xpath:./text() | *')]
|
||||
return [format_html(x.strip(' ').rstrip('\n')) for x in texts if x and sub('[\r\n\t ]', '', x) != '']
|
||||
|
||||
def parent(self, level_or_loc=1, index=1, timeout=None):
|
||||
if isinstance(level_or_loc, int):
|
||||
loc = f'xpath:./ancestor::*[{level_or_loc}]'
|
||||
|
||||
elif isinstance(level_or_loc, (tuple, str)):
|
||||
loc = get_loc(level_or_loc, True)
|
||||
if loc[0] == 'css selector':
|
||||
raise LocatorError(_S._lang.UNSUPPORTED_CSS_SYNTAX)
|
||||
loc = f'xpath:./ancestor::{loc[1].lstrip(". / ")}[{index}]'
|
||||
|
||||
else:
|
||||
raise ValueError(_S._lang.join(_S._lang.INCORRECT_TYPE_, 'level_or_loc', ALLOW_TYPE='tuple, int, str',
|
||||
CURR_VAL=level_or_loc))
|
||||
|
||||
return self._ele(loc, timeout=timeout, relative=True, raise_err=False, method='parent()')
|
||||
|
||||
def child(self, locator='', index=1, timeout=None, ele_only=True):
|
||||
if isinstance(locator, int):
|
||||
index = locator
|
||||
locator = ''
|
||||
if not locator:
|
||||
loc = '*' if ele_only else 'node()'
|
||||
else:
|
||||
loc = get_loc(locator, True) # 把定位符转换为xpath
|
||||
if loc[0] == 'css selector':
|
||||
raise LocatorError(_S._lang.UNSUPPORTED_CSS_SYNTAX)
|
||||
loc = loc[1].lstrip('./')
|
||||
|
||||
node = self._ele(f'xpath:./{loc}', timeout=timeout, index=index, relative=True, raise_err=False)
|
||||
return node if node else NoneElement(self.owner, 'child()',
|
||||
{'locator': locator, 'index': index, 'ele_only': ele_only})
|
||||
|
||||
def prev(self, locator='', index=1, timeout=None, ele_only=True):
|
||||
return self._get_relative('prev()', 'preceding', True, locator, index, timeout, ele_only)
|
||||
|
||||
def next(self, locator='', index=1, timeout=None, ele_only=True):
|
||||
return self._get_relative('next()', 'following', True, locator, index, timeout, ele_only)
|
||||
|
||||
def before(self, locator='', index=1, timeout=None, ele_only=True):
|
||||
return self._get_relative('before()', 'preceding', False, locator, index, timeout, ele_only)
|
||||
|
||||
def after(self, locator='', index=1, timeout=None, ele_only=True):
|
||||
return self._get_relative('after()', 'following', False, locator, index, timeout, ele_only)
|
||||
|
||||
def children(self, locator='', timeout=None, ele_only=True):
|
||||
if not locator:
|
||||
loc = '*' if ele_only else 'node()'
|
||||
else:
|
||||
loc = get_loc(locator, True) # 把定位符转换为xpath
|
||||
if loc[0] == 'css selector':
|
||||
raise LocatorError(_S._lang.UNSUPPORTED_CSS_SYNTAX)
|
||||
loc = loc[1].lstrip('./')
|
||||
|
||||
loc = f'xpath:./{loc}'
|
||||
nodes = self._ele(loc, timeout=timeout, index=None, relative=True)
|
||||
return [e for e in nodes if not (isinstance(e, str) and sub('[ \n\t\r]', '', e) == '')]
|
||||
|
||||
def prevs(self, locator='', timeout=None, ele_only=True):
|
||||
return self._get_relatives(locator=locator, direction='preceding', timeout=timeout, ele_only=ele_only)
|
||||
|
||||
def nexts(self, locator='', timeout=None, ele_only=True):
|
||||
return self._get_relatives(locator=locator, direction='following', timeout=timeout, ele_only=ele_only)
|
||||
|
||||
def befores(self, locator='', timeout=None, ele_only=True):
|
||||
return self._get_relatives(locator=locator, direction='preceding',
|
||||
brother=False, timeout=timeout, ele_only=ele_only)
|
||||
|
||||
def afters(self, locator='', timeout=None, ele_only=True):
|
||||
return self._get_relatives(locator=locator, direction='following',
|
||||
brother=False, timeout=timeout, ele_only=ele_only)
|
||||
|
||||
def _get_relative(self, func, direction, brother, locator='', index=1, timeout=None, ele_only=True):
|
||||
if isinstance(locator, int):
|
||||
index = locator
|
||||
locator = ''
|
||||
node = self._get_relatives(index, locator, direction, brother, timeout, ele_only)
|
||||
return node if node else NoneElement(self.owner, func,
|
||||
{'locator': locator, 'index': index, 'ele_only': ele_only})
|
||||
|
||||
def _get_relatives(self, index=None, locator='', direction='following', brother=True, timeout=.5, ele_only=True):
|
||||
brother = '-sibling' if brother else ''
|
||||
|
||||
if not locator:
|
||||
loc = '*' if ele_only else 'node()'
|
||||
|
||||
else:
|
||||
loc = get_loc(locator, True) # 把定位符转换为xpath
|
||||
if loc[0] == 'css selector':
|
||||
raise LocatorError(_S._lang.UNSUPPORTED_CSS_SYNTAX)
|
||||
loc = loc[1].lstrip('./')
|
||||
|
||||
loc = f'xpath:./{direction}{brother}::{loc}'
|
||||
|
||||
if index is not None:
|
||||
index = index if direction == 'following' else -index
|
||||
nodes = self._ele(loc, timeout=timeout, index=index, relative=True, raise_err=False)
|
||||
if isinstance(nodes, list):
|
||||
nodes = [e for e in nodes if not (isinstance(e, str) and sub('[ \n\t\r]', '', e) == '')]
|
||||
return nodes
|
||||
|
||||
# ----------------以下属性或方法由后代实现----------------
|
||||
@property
|
||||
def attrs(self):
|
||||
return
|
||||
|
||||
@property
|
||||
def text(self):
|
||||
return
|
||||
|
||||
@property
|
||||
def raw_text(self):
|
||||
return
|
||||
|
||||
@abstractmethod
|
||||
def attr(self, name):
|
||||
return ''
|
||||
|
||||
def _get_ele_path(self, xpath=True):
|
||||
return ''
|
||||
|
||||
def _find_elements(self, locator, timeout, index=1, relative=False, raise_err=None):
|
||||
pass
|
||||
|
||||
|
||||
class BasePage(BaseParser):
|
||||
|
||||
def __init__(self):
|
||||
self._url = None
|
||||
self._url_available = None
|
||||
self.retry_times = 3
|
||||
self.retry_interval = 2
|
||||
self._DrissionGet = None
|
||||
self._download_path = None
|
||||
self._none_ele_return_value = False
|
||||
self._none_ele_value = None
|
||||
self._session = None
|
||||
self._headers = None
|
||||
self._session_options = None
|
||||
self._type = 'BasePage'
|
||||
|
||||
@property
|
||||
def title(self):
|
||||
ele = self._ele('xpath://title', raise_err=False, method='title')
|
||||
return ele.text if ele else None
|
||||
|
||||
@property
|
||||
def url_available(self):
|
||||
return self._url_available
|
||||
|
||||
@property
|
||||
def download_path(self):
|
||||
return self._download_path
|
||||
|
||||
@property
|
||||
def download(self):
|
||||
if self._DrissionGet is None:
|
||||
if not self._session:
|
||||
self._create_session()
|
||||
self._DrissionGet = DrissionGet(driver=self, save_path=self.download_path)
|
||||
return self._DrissionGet
|
||||
|
||||
def _before_connect(self, url, retry, interval):
|
||||
is_file = False
|
||||
if isinstance(url, Path) or ('://' not in url and ':\\\\' not in url):
|
||||
p = Path(url)
|
||||
if p.exists():
|
||||
url = str(p.absolute())
|
||||
is_file = True
|
||||
|
||||
self._url = url if is_file else quote(url, safe='-_.~!*\'"();:@&=+$,/\\?#[]%')
|
||||
retry = retry if retry is not None else self.retry_times
|
||||
interval = interval if interval is not None else self.retry_interval
|
||||
return retry, interval, is_file
|
||||
|
||||
def _set_session_options(self, session_or_options=None):
|
||||
if not session_or_options:
|
||||
self._session_options = SessionOptions(session_or_options)
|
||||
|
||||
elif isinstance(session_or_options, SessionOptions):
|
||||
self._session_options = session_or_options
|
||||
|
||||
elif isinstance(session_or_options, Session):
|
||||
self._session_options = SessionOptions()
|
||||
self._session = copy(session_or_options)
|
||||
self._headers = self._session.headers
|
||||
self._session.headers = None
|
||||
|
||||
def _create_session(self):
|
||||
if not self._session_options:
|
||||
self._set_session_options()
|
||||
self._session, self._headers = self._session_options.make_session()
|
||||
|
||||
# ----------------以下属性或方法由后代实现----------------
|
||||
@property
|
||||
def url(self):
|
||||
return
|
||||
|
||||
@property
|
||||
def json(self):
|
||||
return
|
||||
|
||||
@property
|
||||
def user_agent(self):
|
||||
return
|
||||
|
||||
@abstractmethod
|
||||
def get(self, url, show_errmsg=False, retry=None, interval=None):
|
||||
pass
|
||||
|
||||
def _ele(self, locator, timeout=None, index=1, raise_err=None, method=None):
|
||||
if not locator:
|
||||
raise ElementNotFoundError(METHOD=method, ARGS={'locator': locator, 'index': index, 'timeout': timeout})
|
||||
if timeout is None:
|
||||
timeout = self.timeout
|
||||
|
||||
r = self._find_elements(locator, timeout=timeout, index=index, raise_err=raise_err)
|
||||
if r or isinstance(r, list):
|
||||
return r
|
||||
if raise_err is True or (_S.raise_when_ele_not_found and raise_err is None):
|
||||
raise ElementNotFoundError(METHOD=method, ARGS={'locator': locator, 'index': index, 'timeout': timeout})
|
||||
|
||||
r.method = method
|
||||
r.args = {'locator': locator, 'index': index, 'timeout': timeout}
|
||||
return r
|
||||
@@ -0,0 +1,449 @@
|
||||
# -*- coding:utf-8 -*-
|
||||
"""
|
||||
@Author : g1879
|
||||
@Contact : g1879@qq.com
|
||||
@Website : https://DrissionPage.cn
|
||||
@Copyright: (c) 2020 by g1879, Inc. All Rights Reserved.
|
||||
"""
|
||||
from abc import abstractmethod
|
||||
from typing import Union, Tuple, List, Any, Optional, Dict
|
||||
|
||||
from DrissionGet import DrissionGet
|
||||
from requests import Session
|
||||
from requests.structures import CaseInsensitiveDict
|
||||
|
||||
from .._configs.session_options import SessionOptions
|
||||
from .._elements.chromium_element import ChromiumElement
|
||||
from .._elements.none_element import NoneElement
|
||||
from .._elements.session_element import SessionElement
|
||||
from .._functions.elements import SessionElementsList
|
||||
from .._pages.chromium_frame import ChromiumFrame
|
||||
from .._pages.chromium_page import ChromiumPage
|
||||
from .._pages.session_page import SessionPage
|
||||
from .._pages.web_page import WebPage
|
||||
|
||||
|
||||
class BaseParser(object):
|
||||
"""所有页面、元素类的基类"""
|
||||
_type: str
|
||||
timeout: float
|
||||
|
||||
def __call__(self, locator: Union[Tuple[str, str], str], index: int = 1): ...
|
||||
|
||||
def ele(self,
|
||||
locator: Union[Tuple[str, str], str, BaseElement],
|
||||
index: int = 1,
|
||||
timeout: float = None): ...
|
||||
|
||||
def eles(self, locator: Union[Tuple[str, str], str], timeout=None): ...
|
||||
|
||||
def find(self,
|
||||
locators: Union[str, List[str], tuple],
|
||||
any_one: bool = True,
|
||||
first_ele: bool = True,
|
||||
timeout: float = None) -> Union[Dict[str, ChromiumElement], Dict[str, SessionElement],
|
||||
Dict[str, List[ChromiumElement]], Dict[str, List[SessionElement]], Tuple[str, SessionElement],
|
||||
Tuple[str, ChromiumElement]]:
|
||||
"""传入多个定位符,获取多个ele
|
||||
:param locators: 定位符组成的列表
|
||||
:param any_one: 是否任何一个定位符找到结果即返回
|
||||
:param first_ele: 每个定位符是否只获取第一个元素
|
||||
:param timeout: 超时时间(秒)
|
||||
:return: any_one为True时,返回一个找到的元素定位符和对象组成的元组,格式:(loc, ele),全都没找到返回(None, None)
|
||||
any_one为False时,返回dict格式,key为定位符,value为找到的元素或列表
|
||||
"""
|
||||
...
|
||||
|
||||
# ----------------以下属性或方法待后代实现----------------
|
||||
@property
|
||||
def html(self) -> str: ...
|
||||
|
||||
def s_ele(self,
|
||||
locator: Union[Tuple[str, str], str, BaseElement, None] = None,
|
||||
index: int = 1) -> SessionElement: ...
|
||||
|
||||
def s_eles(self, locator: Union[Tuple[str, str], str]) -> SessionElementsList: ...
|
||||
|
||||
def _ele(self,
|
||||
locator: Union[Tuple[str, str], str],
|
||||
timeout: float = None,
|
||||
index: Optional[int] = 1,
|
||||
raise_err: bool = None,
|
||||
method: str = None): ...
|
||||
|
||||
def _find_elements(self,
|
||||
locator: Union[Tuple[str, str], str],
|
||||
timeout: float,
|
||||
index: Optional[int] = 1,
|
||||
relative: bool = False,
|
||||
raise_err: bool = None): ...
|
||||
|
||||
|
||||
class BaseElement(BaseParser):
|
||||
"""各元素类的基类"""
|
||||
owner: BasePage = ...
|
||||
|
||||
def __init__(self, owner: BasePage = None): ...
|
||||
|
||||
@property
|
||||
def timeout(self) -> float:
|
||||
"""返回其查找元素时超时时间"""
|
||||
...
|
||||
|
||||
@property
|
||||
def child_count(self) -> int:
|
||||
"""返回直接子元素的个数"""
|
||||
...
|
||||
|
||||
# ----------------以下属性或方法由后代实现----------------
|
||||
@property
|
||||
def tag(self) -> str: ...
|
||||
|
||||
def parent(self, level_or_loc: Union[tuple, str, int] = 1): ...
|
||||
|
||||
def prev(self, index: int = 1) -> None: ...
|
||||
|
||||
def prevs(self) -> None: ...
|
||||
|
||||
def next(self, index: int = 1): ...
|
||||
|
||||
def nexts(self): ...
|
||||
|
||||
def get_frame(self, loc_or_ind, timeout=None) -> ChromiumFrame:
|
||||
"""获取元素中一个frame对象
|
||||
:param loc_or_ind: 定位符、iframe序号,序号从1开始,可传入负数获取倒数第几个
|
||||
:param timeout: 查找元素超时时间(秒)
|
||||
:return: ChromiumFrame对象
|
||||
"""
|
||||
...
|
||||
|
||||
def _ele(self,
|
||||
locator: Union[Tuple[str, str], str],
|
||||
timeout: float = None,
|
||||
index: Optional[int] = 1,
|
||||
relative: bool = False,
|
||||
raise_err: bool = None,
|
||||
method: str = None):
|
||||
"""调用获取元素的方法
|
||||
:param locator: 定位符
|
||||
:param timeout: 超时时间(秒)
|
||||
:param index: 获取第几个,从1开始,可传入负数获取倒数第几个
|
||||
:param relative: 是否相对定位
|
||||
:param raise_err: 找不到时是否抛出异常
|
||||
:param method: 调用的方法名
|
||||
:return: 元素对象或它们组成的列表
|
||||
"""
|
||||
...
|
||||
|
||||
|
||||
class DrissionElement(BaseElement):
|
||||
"""ChromiumElement 和 SessionElement的基类,但不是ShadowRoot的基类"""
|
||||
|
||||
def __init__(self, owner: BasePage = None): ...
|
||||
|
||||
@property
|
||||
def link(self) -> str:
|
||||
"""返回href或src绝对url"""
|
||||
...
|
||||
|
||||
@property
|
||||
def css_path(self) -> str:
|
||||
"""返回css path路径"""
|
||||
...
|
||||
|
||||
@property
|
||||
def xpath(self) -> str:
|
||||
"""返回xpath路径"""
|
||||
...
|
||||
|
||||
@property
|
||||
def comments(self) -> list:
|
||||
"""返回元素注释文本组成的列表"""
|
||||
...
|
||||
|
||||
def texts(self, text_node_only: bool = False) -> list:
|
||||
"""返回元素内所有直接子节点的文本,包括元素和文本节点
|
||||
:param text_node_only: 是否只返回文本节点
|
||||
:return: 文本列表
|
||||
"""
|
||||
...
|
||||
|
||||
def parent(self,
|
||||
level_or_loc: Union[tuple, str, int] = 1,
|
||||
index: int = 1,
|
||||
timeout: float = None) -> Union[DrissionElement, None]:
|
||||
"""返回上面某一级父元素,可指定层数或用查询语法定位
|
||||
:param level_or_loc: 第几级父元素,1开始,或定位符
|
||||
:param index: 当level_or_loc传入定位符,使用此参数选择第几个结果,1开始
|
||||
:param timeout: 时间(秒)
|
||||
:return: 上级元素对象
|
||||
"""
|
||||
...
|
||||
|
||||
def child(self,
|
||||
locator: Union[Tuple[str, str], str, int] = '',
|
||||
index: int = 1,
|
||||
timeout: float = None,
|
||||
ele_only: bool = True) -> Union[DrissionElement, str, NoneElement]:
|
||||
"""返回直接子元素元素或节点组成的列表,可用查询语法筛选
|
||||
:param locator: 用于筛选的查询语法
|
||||
:param index: 第几个查询结果,1开始
|
||||
:param timeout: 查找节点的超时时间(秒)
|
||||
:param ele_only: 是否只获取元素,为False时把文本、注释节点也纳入
|
||||
:return: 直接子元素或节点文本组成的列表
|
||||
"""
|
||||
...
|
||||
|
||||
def prev(self,
|
||||
locator: Union[Tuple[str, str], str, int] = '',
|
||||
index: int = 1,
|
||||
timeout: float = None,
|
||||
ele_only: bool = True) -> Union[DrissionElement, str, NoneElement]:
|
||||
"""返回前面的一个兄弟元素,可用查询语法筛选,可指定返回筛选结果的第几个
|
||||
:param locator: 用于筛选的查询语法
|
||||
:param index: 前面第几个查询结果,1开始
|
||||
:param timeout: 查找节点的超时时间(秒)
|
||||
:param ele_only: 是否只获取元素,为False时把文本、注释节点也纳入
|
||||
:return: 兄弟元素
|
||||
"""
|
||||
...
|
||||
|
||||
def next(self,
|
||||
locator: Union[Tuple[str, str], str, int] = '',
|
||||
index: int = 1,
|
||||
timeout: float = None,
|
||||
ele_only: bool = True) -> Union[DrissionElement, str, NoneElement]:
|
||||
"""返回后面的一个兄弟元素,可用查询语法筛选,可指定返回筛选结果的第几个
|
||||
:param locator: 用于筛选的查询语法
|
||||
:param index: 后面第几个查询结果,1开始
|
||||
:param timeout: 查找节点的超时时间(秒)
|
||||
:param ele_only: 是否只获取元素,为False时把文本、注释节点也纳入
|
||||
:return: 兄弟元素
|
||||
"""
|
||||
...
|
||||
|
||||
def before(self,
|
||||
locator: Union[Tuple[str, str], str, int] = '',
|
||||
index: int = 1,
|
||||
timeout: float = None,
|
||||
ele_only: bool = True) -> Union[DrissionElement, str, NoneElement]:
|
||||
"""返回前面的一个兄弟元素,可用查询语法筛选,可指定返回筛选结果的第几个
|
||||
:param locator: 用于筛选的查询语法
|
||||
:param index: 前面第几个查询结果,1开始
|
||||
:param timeout: 查找节点的超时时间(秒)
|
||||
:param ele_only: 是否只获取元素,为False时把文本、注释节点也纳入
|
||||
:return: 本元素前面的某个元素或节点
|
||||
"""
|
||||
...
|
||||
|
||||
def after(self,
|
||||
locator: Union[Tuple[str, str], str, int] = '',
|
||||
index: int = 1,
|
||||
timeout: float = None,
|
||||
ele_only: bool = True) -> Union[DrissionElement, str, NoneElement]:
|
||||
"""返回后面的一个兄弟元素,可用查询语法筛选,可指定返回筛选结果的第几个
|
||||
:param locator: 用于筛选的查询语法
|
||||
:param index: 后面第几个查询结果,1开始
|
||||
:param timeout: 查找节点的超时时间(秒)
|
||||
:param ele_only: 是否只获取元素,为False时把文本、注释节点也纳入
|
||||
:return: 本元素后面的某个元素或节点
|
||||
"""
|
||||
...
|
||||
|
||||
def children(self,
|
||||
locator: Union[Tuple[str, str], str] = '',
|
||||
timeout: float = None,
|
||||
ele_only: bool = True) -> List[Union[DrissionElement, str]]:
|
||||
"""返回直接子元素元素或节点组成的列表,可用查询语法筛选
|
||||
:param locator: 用于筛选的查询语法
|
||||
:param timeout: 查找节点的超时时间(秒)
|
||||
:param ele_only: 是否只获取元素,为False时把文本、注释节点也纳入
|
||||
:return: 直接子元素或节点文本组成的列表
|
||||
"""
|
||||
...
|
||||
|
||||
def prevs(self,
|
||||
locator: Union[Tuple[str, str], str] = '',
|
||||
timeout: float = None,
|
||||
ele_only: bool = True) -> List[Union[DrissionElement, str]]:
|
||||
"""返回前面全部兄弟元素或节点组成的列表,可用查询语法筛选
|
||||
:param locator: 用于筛选的查询语法
|
||||
:param timeout: 查找节点的超时时间(秒)
|
||||
:param ele_only: 是否只获取元素,为False时把文本、注释节点也纳入
|
||||
:return: 兄弟元素或节点文本组成的列表
|
||||
"""
|
||||
...
|
||||
|
||||
def nexts(self,
|
||||
locator: Union[Tuple[str, str], str] = '',
|
||||
timeout: float = None,
|
||||
ele_only: bool = True) -> List[Union[DrissionElement, str]]:
|
||||
"""返回后面全部兄弟元素或节点组成的列表,可用查询语法筛选
|
||||
:param locator: 用于筛选的查询语法
|
||||
:param timeout: 查找节点的超时时间(秒)
|
||||
:param ele_only: 是否只获取元素,为False时把文本、注释节点也纳入
|
||||
:return: 兄弟元素或节点文本组成的列表
|
||||
"""
|
||||
...
|
||||
|
||||
def befores(self,
|
||||
locator: Union[Tuple[str, str], str] = '',
|
||||
timeout: float = None,
|
||||
ele_only: bool = True) -> List[Union[DrissionElement, str]]:
|
||||
"""返回后面全部兄弟元素或节点组成的列表,可用查询语法筛选
|
||||
:param locator: 用于筛选的查询语法
|
||||
:param timeout: 查找节点的超时时间(秒)
|
||||
:param ele_only: 是否只获取元素,为False时把文本、注释节点也纳入
|
||||
:return: 本元素前面的元素或节点组成的列表
|
||||
"""
|
||||
...
|
||||
|
||||
def afters(self,
|
||||
locator: Union[Tuple[str, str], str] = '',
|
||||
timeout: float = None,
|
||||
ele_only: bool = True) -> List[Union[DrissionElement, str]]:
|
||||
"""返回前面全部兄弟元素或节点组成的列表,可用查询语法筛选
|
||||
:param locator: 用于筛选的查询语法
|
||||
:param timeout: 查找节点的超时时间(秒)
|
||||
:param ele_only: 是否只获取元素,为False时把文本、注释节点也纳入
|
||||
:return: 本元素后面的元素或节点组成的列表
|
||||
"""
|
||||
...
|
||||
|
||||
def _get_relative(self,
|
||||
func: str,
|
||||
direction: str,
|
||||
brother: bool,
|
||||
locator: Union[Tuple[str, str], str] = '',
|
||||
index: int = 1,
|
||||
timeout: float = None,
|
||||
ele_only: bool = True) -> DrissionElement:
|
||||
"""获取一个亲戚元素或节点,可用查询语法筛选,可指定返回筛选结果的第几个
|
||||
:param func: 方法名称
|
||||
:param direction: 方向,'following' 或 'preceding'
|
||||
:param locator: 用于筛选的查询语法
|
||||
:param index: 前面第几个查询结果,1开始
|
||||
:param timeout: 查找节点的超时时间(秒)
|
||||
:param ele_only: 是否只获取元素,为False时把文本、注释节点也纳入
|
||||
:return: 本元素前面的某个元素或节点
|
||||
"""
|
||||
...
|
||||
|
||||
def _get_relatives(self,
|
||||
index: int = None,
|
||||
locator: Union[Tuple[str, str], str] = '',
|
||||
direction: str = 'following',
|
||||
brother: bool = True,
|
||||
timeout: float = 0.5,
|
||||
ele_only: bool = True) -> List[Union[DrissionElement, str]]:
|
||||
"""按要求返回兄弟元素或节点组成的列表
|
||||
:param index: 获取第几个,该参数不为None时只获取该编号的元素
|
||||
:param locator: 用于筛选的查询语法
|
||||
:param direction: 'following' 或 'preceding',查找的方向
|
||||
:param brother: 查找范围,在同级查找还是整个dom前后查找
|
||||
:param timeout: 查找等待时间(秒)
|
||||
:return: 元素对象或字符串
|
||||
"""
|
||||
...
|
||||
|
||||
# ----------------以下属性或方法由后代实现----------------
|
||||
@property
|
||||
def attrs(self) -> dict: ...
|
||||
|
||||
@property
|
||||
def text(self) -> str: ...
|
||||
|
||||
@property
|
||||
def raw_text(self) -> str: ...
|
||||
|
||||
@abstractmethod
|
||||
def attr(self, name: str) -> str: ...
|
||||
|
||||
def _get_ele_path(self, xpath: bool = True) -> str: ...
|
||||
|
||||
|
||||
class BasePage(BaseParser):
|
||||
"""页面类的基类"""
|
||||
|
||||
_url_available: Optional[bool] = ...
|
||||
retry_times: int = ...
|
||||
retry_interval: float = ...
|
||||
_download_path: Optional[str] = ...
|
||||
_DrissionGet: Optional[DrissionGet] = ...
|
||||
_none_ele_return_value: bool = ...
|
||||
_none_ele_value: Any = ...
|
||||
_page: Union[ChromiumPage, SessionPage, WebPage] = ...
|
||||
_session: Optional[Session] = ...
|
||||
_headers: Optional[CaseInsensitiveDict] = ...
|
||||
_session_options: Optional[SessionOptions] = ...
|
||||
|
||||
def __init__(self): ...
|
||||
|
||||
@property
|
||||
def title(self) -> Union[str, None]:
|
||||
"""返回网页title"""
|
||||
...
|
||||
|
||||
@property
|
||||
def url_available(self) -> bool:
|
||||
"""返回当前访问的url有效性"""
|
||||
...
|
||||
|
||||
@property
|
||||
def download_path(self) -> str:
|
||||
"""返回默认下载路径"""
|
||||
...
|
||||
|
||||
@property
|
||||
def download(self) -> DrissionGet:
|
||||
"""返回下载器对象"""
|
||||
...
|
||||
|
||||
def _before_connect(self, url: str, retry: int, interval: float) -> tuple:
|
||||
"""连接前的准备
|
||||
:param url: 要访问的url
|
||||
:param retry: 重试次数
|
||||
:param interval: 重试间隔
|
||||
:return: 重试次数、间隔、是否文件组成的tuple
|
||||
"""
|
||||
...
|
||||
|
||||
def _set_session_options(self, session_or_options: Union[Session, SessionOptions] = None) -> None:
|
||||
"""启动配置
|
||||
:param session_or_options: Session、SessionOptions对象
|
||||
:return: None
|
||||
"""
|
||||
...
|
||||
|
||||
def _create_session(self) -> None:
|
||||
"""创建内建Session对象"""
|
||||
...
|
||||
|
||||
# ----------------以下属性或方法由后代实现----------------
|
||||
@property
|
||||
def url(self) -> str: ...
|
||||
|
||||
@property
|
||||
def json(self) -> dict: ...
|
||||
|
||||
@property
|
||||
def user_agent(self) -> str: ...
|
||||
|
||||
@abstractmethod
|
||||
def get(self, url: str, show_errmsg: bool = False, retry: int = None, interval: float = None): ...
|
||||
|
||||
def _ele(self,
|
||||
locator,
|
||||
timeout: float = None,
|
||||
index: Optional[int] = 1,
|
||||
raise_err: bool = None,
|
||||
method: str = None):
|
||||
"""调用获取元素的方法
|
||||
:param locator: 定位符
|
||||
:param timeout: 超时时间(秒)
|
||||
:param index: 获取第几个,从1开始,可传入负数获取倒数第几个
|
||||
:param raise_err: 找不到时是否抛出异常
|
||||
:param method: 调用的方法名
|
||||
:return: 元素对象或它们组成的列表
|
||||
"""
|
||||
...
|
||||
@@ -0,0 +1,537 @@
|
||||
# -*- coding:utf-8 -*-
|
||||
"""
|
||||
@Author : g1879
|
||||
@Contact : g1879@qq.com
|
||||
@Website : https://DrissionPage.cn
|
||||
@Copyright: (c) 2020 by g1879, Inc. All Rights Reserved.
|
||||
"""
|
||||
from pathlib import Path
|
||||
from re import match
|
||||
from shutil import rmtree
|
||||
from threading import Lock
|
||||
from time import sleep, perf_counter
|
||||
|
||||
from requests import Session
|
||||
from websocket import WebSocketBadStatusException
|
||||
|
||||
from .driver import BrowserDriver, Driver
|
||||
from .._configs.chromium_options import ChromiumOptions
|
||||
from .._functions.browser import connect_browser
|
||||
from .._functions.cookies import CookiesList
|
||||
from .._functions.settings import Settings as _S
|
||||
from .._functions.tools import PortFinder, raise_error
|
||||
from .._pages.chromium_base import Timeout
|
||||
from .._pages.chromium_tab import ChromiumTab
|
||||
from .._pages.mix_tab import MixTab
|
||||
from .._units.downloader import DownloadManager
|
||||
from .._units.setter import BrowserSetter
|
||||
from .._units.states import BrowserStates
|
||||
from .._units.waiter import BrowserWaiter
|
||||
from ..errors import BrowserConnectError, CDPError, PageDisconnectedError, IncorrectURLError
|
||||
|
||||
__ERROR__ = 'error'
|
||||
|
||||
|
||||
class Chromium(object):
|
||||
_BROWSERS = {}
|
||||
_lock = Lock()
|
||||
|
||||
def __new__(cls, addr_or_opts=None, session_options=None):
|
||||
opt = handle_options(addr_or_opts)
|
||||
is_headless, browser_id, is_exists, ws_only = run_browser(opt)
|
||||
with cls._lock:
|
||||
if browser_id in cls._BROWSERS:
|
||||
r = cls._BROWSERS[browser_id]
|
||||
while not hasattr(r, '_driver'):
|
||||
sleep(.05)
|
||||
return r
|
||||
r = object.__new__(cls)
|
||||
r._chromium_options = opt
|
||||
r._is_headless = is_headless
|
||||
r._is_exists = is_exists
|
||||
r._ws_only = ws_only
|
||||
r.id = browser_id
|
||||
cls._BROWSERS[browser_id] = r
|
||||
return r
|
||||
|
||||
def __init__(self, addr_or_opts=None, session_options=None):
|
||||
if hasattr(self, '_created'):
|
||||
return
|
||||
self._created = True
|
||||
|
||||
self._type = 'Chromium'
|
||||
self._frames = {}
|
||||
self._drivers = {}
|
||||
self._all_drivers = {}
|
||||
self._relation = {}
|
||||
self._newest_tab_id = None
|
||||
|
||||
self._set = None
|
||||
self._wait = None
|
||||
self._states = None
|
||||
self._timeouts = Timeout(**self._chromium_options.timeouts)
|
||||
self._load_mode = self._chromium_options.load_mode
|
||||
self._download_path = str(Path(self._chromium_options.download_path).absolute())
|
||||
self._auto_handle_alert = None
|
||||
self._none_ele_return_value = False
|
||||
self._none_ele_value = None
|
||||
self.retry_times = self._chromium_options.retry_times
|
||||
self.retry_interval = self._chromium_options.retry_interval
|
||||
self.address = self._chromium_options.address
|
||||
self._ws_address = (self._chromium_options.ws_address if self._chromium_options.ws_address
|
||||
else f'ws://{self.address}/devtools/browser/{self.id}')
|
||||
self._disconnect_flag = False
|
||||
self._driver = BrowserDriver(self.id, self._ws_address, self)
|
||||
|
||||
if (not self._ws_only
|
||||
and (not self._chromium_options._ua_set and self._is_headless != self._chromium_options.is_headless)
|
||||
or (self._is_exists and self._chromium_options._new_env)):
|
||||
self.quit(3, True)
|
||||
connect_browser(self._chromium_options)
|
||||
s = Session()
|
||||
s.trust_env = False
|
||||
s.keep_alive = False
|
||||
ws = s.get(f'http://{self.address}/json/version', headers={'Connection': 'close'})
|
||||
self.id = ws.json()['webSocketDebuggerUrl'].split('/')[-1]
|
||||
self._driver = BrowserDriver(self.id, self._ws_address, self)
|
||||
ws.close()
|
||||
s.close()
|
||||
self._is_exists = False
|
||||
self._frames = {}
|
||||
self._drivers = {}
|
||||
self._all_drivers = {}
|
||||
|
||||
self.version = self._run_cdp('Browser.getVersion')['product']
|
||||
|
||||
self._process_id = None
|
||||
try:
|
||||
r = self._run_cdp('SystemInfo.getProcessInfo')
|
||||
for i in r.get('processInfo', []):
|
||||
if i['type'] == 'browser':
|
||||
self._process_id = i['id']
|
||||
break
|
||||
except:
|
||||
pass
|
||||
|
||||
self._run_cdp('Target.setDiscoverTargets', discover=True)
|
||||
self._driver.set_callback('Target.targetDestroyed', self._onTargetDestroyed)
|
||||
self._driver.set_callback('Target.targetCreated', self._onTargetCreated)
|
||||
self._dl_mgr = DownloadManager(self)
|
||||
|
||||
self._session_options = session_options
|
||||
|
||||
@property
|
||||
def user_data_path(self):
|
||||
return self._chromium_options.user_data_path
|
||||
|
||||
@property
|
||||
def process_id(self):
|
||||
return self._process_id
|
||||
|
||||
@property
|
||||
def timeout(self):
|
||||
return self._timeouts.base
|
||||
|
||||
@property
|
||||
def timeouts(self):
|
||||
return self._timeouts
|
||||
|
||||
@property
|
||||
def load_mode(self):
|
||||
return self._load_mode
|
||||
|
||||
@property
|
||||
def download_path(self):
|
||||
return self._download_path
|
||||
|
||||
@property
|
||||
def set(self):
|
||||
if self._set is None:
|
||||
self._set = BrowserSetter(self)
|
||||
return self._set
|
||||
|
||||
@property
|
||||
def states(self):
|
||||
if self._states is None:
|
||||
self._states = BrowserStates(self)
|
||||
return self._states
|
||||
|
||||
@property
|
||||
def wait(self):
|
||||
if self._wait is None:
|
||||
self._wait = BrowserWaiter(self)
|
||||
return self._wait
|
||||
|
||||
@property
|
||||
def tabs_count(self):
|
||||
j = self._run_cdp('Target.getTargets')['targetInfos'] # 不要改用get,避免卡死
|
||||
return len([i for i in j if i['type'] in ('page', 'webview') and not i['url'].startswith('devtools://')])
|
||||
|
||||
@property
|
||||
def tab_ids(self):
|
||||
if self._ws_only:
|
||||
return [i['targetId'] for i in self._run_cdp('Target.getTargets')['targetInfos']
|
||||
if i['type'] in ('page', 'webview') and not i['url'].startswith('devtools://')]
|
||||
else:
|
||||
return [i['id'] for i in self._driver.get(f'http://{self.address}/json').json()
|
||||
if i['type'] in ('page', 'webview') and not i['url'].startswith('devtools://')]
|
||||
|
||||
@property
|
||||
def latest_tab(self):
|
||||
return self._get_tab(id_or_num=self.tab_ids[0], as_id=not _S.singleton_tab_obj)
|
||||
|
||||
def cookies(self, all_info=False):
|
||||
cks = self._run_cdp(f'Storage.getCookies')['cookies']
|
||||
r = cks if all_info else [{'name': c['name'], 'value': c['value'], 'domain': c['domain']} for c in cks]
|
||||
return CookiesList(r)
|
||||
|
||||
def new_tab(self, url=None, new_window=False, background=False, new_context=False):
|
||||
return self._new_tab(True, url=url, new_window=new_window, background=background, new_context=new_context)
|
||||
|
||||
def get_tab(self, id_or_num=None, title=None, url=None, tab_type='page'):
|
||||
t = self._get_tab(id_or_num=id_or_num, title=title, url=url, tab_type=tab_type, mix=True, as_id=False)
|
||||
if t._type != 'MixTab':
|
||||
raise RuntimeError(_S._lang.join(_S._lang.TAB_OBJ_EXISTS))
|
||||
return t
|
||||
|
||||
def get_tabs(self, title=None, url=None, tab_type='page'):
|
||||
return self._get_tabs(title=title, url=url, tab_type=tab_type, mix=True, as_id=False)
|
||||
|
||||
def close_tabs(self, tabs_or_ids, others=False):
|
||||
if isinstance(tabs_or_ids, str):
|
||||
tabs = {tabs_or_ids}
|
||||
elif isinstance(tabs_or_ids, ChromiumTab):
|
||||
tabs = {tabs_or_ids.tab_id}
|
||||
elif isinstance(tabs_or_ids, (list, tuple)):
|
||||
tabs = set(i.tab_id if isinstance(i, ChromiumTab) else i for i in tabs_or_ids)
|
||||
else:
|
||||
raise ValueError(_S._lang.join(_S._lang.INCORRECT_TYPE_, 'tabs_or_ids',
|
||||
ALLOW_TYPE=_S._lang.TAB_OR_ID, CURR_VAL=tabs_or_ids))
|
||||
|
||||
all_tabs = set(self.tab_ids)
|
||||
if others:
|
||||
tabs = all_tabs - tabs
|
||||
|
||||
if len(all_tabs - tabs) > 0:
|
||||
for tab in tabs:
|
||||
self._close_tab(tab=tab)
|
||||
else:
|
||||
self.quit()
|
||||
|
||||
def _close_tab(self, tab):
|
||||
if isinstance(tab, str):
|
||||
tab = self.get_tab(tab)
|
||||
tab._run_cdp('Target.closeTarget', targetId=tab.tab_id)
|
||||
while tab.driver.is_running and tab.tab_id in self._all_drivers:
|
||||
sleep(.01)
|
||||
|
||||
def activate_tab(self, id_ind_tab):
|
||||
if isinstance(id_ind_tab, int):
|
||||
id_ind_tab += -1 if id_ind_tab else 1
|
||||
id_ind_tab = self.tab_ids[id_ind_tab]
|
||||
elif isinstance(id_ind_tab, ChromiumTab):
|
||||
id_ind_tab = id_ind_tab.tab_id
|
||||
self._run_cdp('Target.activateTarget', targetId=id_ind_tab)
|
||||
|
||||
def reconnect(self):
|
||||
self._disconnect_flag = True
|
||||
self._driver.stop()
|
||||
BrowserDriver.BROWSERS.pop(self.id)
|
||||
self._driver = BrowserDriver(self.id, self._ws_address, self)
|
||||
self._run_cdp('Target.setDiscoverTargets', discover=True)
|
||||
self._driver.set_callback('Target.targetDestroyed', self._onTargetDestroyed)
|
||||
self._driver.set_callback('Target.targetCreated', self._onTargetCreated)
|
||||
self._disconnect_flag = False
|
||||
|
||||
def clear_cache(self, cache=True, cookies=True):
|
||||
if cache:
|
||||
self.latest_tab.run_cdp('Network.clearBrowserCache')
|
||||
|
||||
if cookies:
|
||||
self._run_cdp('Storage.clearCookies')
|
||||
|
||||
def quit(self, timeout=5, force=False, del_data=False):
|
||||
try:
|
||||
self._run_cdp('Browser.close')
|
||||
except PageDisconnectedError:
|
||||
pass
|
||||
self._driver.stop()
|
||||
|
||||
drivers = list(self._all_drivers.values())
|
||||
for tab in drivers:
|
||||
for driver in tab:
|
||||
driver.stop()
|
||||
if not self.address.startswith('127.0.0.1'):
|
||||
return
|
||||
|
||||
if force:
|
||||
pids = None
|
||||
try:
|
||||
pids = [pid['id'] for pid in self._run_cdp('SystemInfo.getProcessInfo')['processInfo']]
|
||||
except:
|
||||
pass
|
||||
|
||||
if pids:
|
||||
from psutil import Process
|
||||
for pid in pids:
|
||||
try:
|
||||
Process(pid).kill()
|
||||
except:
|
||||
pass
|
||||
|
||||
from os import popen
|
||||
from platform import system
|
||||
end_time = perf_counter() + timeout
|
||||
while perf_counter() < end_time:
|
||||
ok = True
|
||||
for pid in pids:
|
||||
txt = f'tasklist | findstr {pid}' if system().lower() == 'windows' else f'ps -ef | grep {pid}'
|
||||
p = popen(txt)
|
||||
sleep(.05)
|
||||
try:
|
||||
if f' {pid} ' in p.read():
|
||||
ok = False
|
||||
break
|
||||
except TypeError:
|
||||
pass
|
||||
|
||||
if ok:
|
||||
break
|
||||
|
||||
if del_data and not self._chromium_options.is_auto_port and self._chromium_options.user_data_path:
|
||||
path = Path(self._chromium_options.user_data_path)
|
||||
rmtree(path, True)
|
||||
|
||||
def _new_tab(self, mix=True, url=None, new_window=False, background=False, new_context=False):
|
||||
tab_type = MixTab if mix else ChromiumTab
|
||||
tab = None
|
||||
if new_context:
|
||||
tab = self._run_cdp('Target.createBrowserContext')['browserContextId']
|
||||
|
||||
kwargs = {'url': ''}
|
||||
if new_window:
|
||||
kwargs['newWindow'] = True
|
||||
if background:
|
||||
kwargs['background'] = True
|
||||
if tab:
|
||||
kwargs['browserContextId'] = tab
|
||||
|
||||
if self.states.is_incognito and not new_context:
|
||||
return _new_tab_by_js(self, url, tab_type, new_window)
|
||||
else:
|
||||
try:
|
||||
tab = self._run_cdp('Target.createTarget', **kwargs)['targetId']
|
||||
except CDPError:
|
||||
return _new_tab_by_js(self, url, tab_type, new_window)
|
||||
|
||||
while self.states.is_alive:
|
||||
if tab in self._drivers:
|
||||
break
|
||||
sleep(.01)
|
||||
else:
|
||||
raise BrowserConnectError(_S._lang.BROWSER_DISCONNECTED)
|
||||
tab = tab_type(self, tab)
|
||||
if url:
|
||||
tab.get(url)
|
||||
return tab
|
||||
|
||||
def _get_tab(self, id_or_num=None, title=None, url=None, tab_type='page', mix=True, as_id=False):
|
||||
if id_or_num is not None:
|
||||
if isinstance(id_or_num, int):
|
||||
id_or_num = self.tab_ids[id_or_num - 1 if id_or_num > 0 else id_or_num]
|
||||
elif isinstance(id_or_num, ChromiumTab):
|
||||
return id_or_num.tab_id if as_id else ChromiumTab(self, id_or_num.tab_id)
|
||||
else:
|
||||
j = self._run_cdp('Target.getTargets')['targetInfos']
|
||||
if id_or_num not in [i['targetId'] for i in j]:
|
||||
raise RuntimeError(_S._lang.join(_S._lang.NO_SUCH_TAB, ARG=id_or_num, ALL_TABS=self.tab_ids))
|
||||
|
||||
elif title == url is None and tab_type == 'page':
|
||||
id_or_num = self.tab_ids[0]
|
||||
|
||||
else:
|
||||
tabs = self._get_tabs(title=title, url=url, tab_type=tab_type, as_id=True)
|
||||
if tabs:
|
||||
id_or_num = tabs[0]
|
||||
else:
|
||||
raise RuntimeError(_S._lang.join(_S._lang.NO_SUCH_TAB,
|
||||
ARGS={'id_or_num': id_or_num, 'title': title, 'url': url,
|
||||
'tab_type': tab_type}))
|
||||
|
||||
if as_id:
|
||||
return id_or_num
|
||||
with self._lock:
|
||||
return MixTab(self, id_or_num) if mix else ChromiumTab(self, id_or_num)
|
||||
|
||||
def _get_tabs(self, title=None, url=None, tab_type='page', mix=True, as_id=False):
|
||||
if self._ws_only:
|
||||
tabs = self._run_cdp('Target.getTargets')['targetInfos']
|
||||
_id = 'targetId'
|
||||
else:
|
||||
tabs = self._driver.get(f'http://{self.address}/json').json() # 不要改用cdp
|
||||
_id = 'id'
|
||||
|
||||
if isinstance(tab_type, str):
|
||||
tab_type = {tab_type}
|
||||
elif isinstance(tab_type, (list, tuple, set)):
|
||||
tab_type = set(tab_type)
|
||||
elif tab_type is not None:
|
||||
raise ValueError(_S._lang.join(_S._lang.INCORRECT_TYPE_, 'tab_type',
|
||||
ALLOW_TYPE='set, list, tuple, str, None', CURR_VAL=tab_type))
|
||||
|
||||
tabs = [i for i in tabs if ((title is None or title in i['title']) and (url is None or url in i['url'])
|
||||
and (tab_type is None or i['type'] in tab_type)
|
||||
and i['title'] != 'chrome-extension://neajdppkdcdipfabeoofebfddakdcjhd/audio.html')]
|
||||
if as_id:
|
||||
return [tab[_id] for tab in tabs]
|
||||
with self._lock:
|
||||
if mix:
|
||||
return [MixTab(self, tab[_id]) for tab in tabs]
|
||||
else:
|
||||
return [ChromiumTab(self, tab[_id]) for tab in tabs]
|
||||
|
||||
def _run_cdp(self, cmd, **cmd_args):
|
||||
ignore = cmd_args.pop('_ignore', None)
|
||||
r = self._driver.run(cmd, **cmd_args)
|
||||
return r if __ERROR__ not in r else raise_error(r, self, ignore)
|
||||
|
||||
def _get_driver(self, tab_id, owner=None):
|
||||
d = self._drivers.pop(tab_id, None)
|
||||
if not d:
|
||||
if self._ws_only:
|
||||
d = Driver(tab_id, self._ws_address)
|
||||
d.session_id = d.run('Target.attachToTarget', targetId=tab_id, flatten=True)['sessionId']
|
||||
else:
|
||||
d = Driver(tab_id, f'ws://{self.address}/devtools/page/{tab_id}')
|
||||
d.owner = owner
|
||||
self._all_drivers.setdefault(tab_id, set()).add(d)
|
||||
return d
|
||||
|
||||
def _onTargetCreated(self, **kwargs):
|
||||
if (kwargs['targetInfo']['type'] in ('page', 'webview')
|
||||
and kwargs['targetInfo']['targetId'] not in self._all_drivers
|
||||
and not kwargs['targetInfo']['url'].startswith('devtools://')):
|
||||
try:
|
||||
tab_id = kwargs['targetInfo']['targetId']
|
||||
self._frames[tab_id] = tab_id
|
||||
if self._ws_only:
|
||||
d = Driver(tab_id, self._ws_address)
|
||||
d.session_id = d.run('Target.attachToTarget', targetId=tab_id, flatten=True)['sessionId']
|
||||
else:
|
||||
d = Driver(tab_id, f'ws://{self.address}/devtools/page/{tab_id}')
|
||||
self._relation[tab_id] = kwargs['targetInfo'].get('openerId', None)
|
||||
self._drivers[tab_id] = d
|
||||
self._all_drivers.setdefault(tab_id, set()).add(d)
|
||||
self._newest_tab_id = tab_id
|
||||
except WebSocketBadStatusException:
|
||||
pass
|
||||
|
||||
def _onTargetDestroyed(self, **kwargs):
|
||||
tab_id = kwargs['targetId']
|
||||
self._dl_mgr.clear_tab_info(tab_id)
|
||||
for key in [k for k, i in self._frames.items() if i == tab_id]:
|
||||
self._frames.pop(key, None)
|
||||
for d in self._all_drivers.get(tab_id, tuple()):
|
||||
d.stop()
|
||||
self._drivers.pop(tab_id, None)
|
||||
self._all_drivers.pop(tab_id, None)
|
||||
self._relation.pop(tab_id, None)
|
||||
|
||||
def _on_disconnect(self):
|
||||
if not self._disconnect_flag:
|
||||
Chromium._BROWSERS.pop(self.id, None)
|
||||
if self._chromium_options.is_auto_port and self._chromium_options.user_data_path:
|
||||
path = Path(self._chromium_options.user_data_path)
|
||||
end_time = perf_counter() + 7
|
||||
while perf_counter() < end_time:
|
||||
if not path.exists():
|
||||
break
|
||||
try:
|
||||
rmtree(path)
|
||||
break
|
||||
except (PermissionError, FileNotFoundError, OSError):
|
||||
pass
|
||||
sleep(.03)
|
||||
|
||||
|
||||
def handle_options(addr_or_opts):
|
||||
if not addr_or_opts:
|
||||
_chromium_options = ChromiumOptions(addr_or_opts)
|
||||
if _chromium_options.is_auto_port:
|
||||
port, path = PortFinder(_chromium_options.tmp_path).get_port(_chromium_options.is_auto_port)
|
||||
_chromium_options.set_address(f'127.0.0.1:{port}')
|
||||
_chromium_options.set_user_data_path(path)
|
||||
_chromium_options.auto_port(scope=_chromium_options.is_auto_port)
|
||||
|
||||
elif isinstance(addr_or_opts, ChromiumOptions):
|
||||
if addr_or_opts.is_auto_port:
|
||||
port, path = PortFinder(addr_or_opts.tmp_path).get_port(addr_or_opts.is_auto_port)
|
||||
addr_or_opts._address = f'127.0.0.1:{port}'
|
||||
addr_or_opts.set_user_data_path(path)
|
||||
_chromium_options = addr_or_opts
|
||||
|
||||
elif isinstance(addr_or_opts, str) and ':' in addr_or_opts:
|
||||
_chromium_options = ChromiumOptions()
|
||||
_chromium_options.set_address(addr_or_opts)
|
||||
|
||||
elif isinstance(addr_or_opts, int):
|
||||
_chromium_options = ChromiumOptions()
|
||||
_chromium_options.set_local_port(addr_or_opts)
|
||||
|
||||
else:
|
||||
raise ValueError(_S._lang.join(_S._lang.INCORRECT_VAL_, 'addr_or_opts',
|
||||
ALLOW_TYPE=_S._lang.IP_OR_OPTIONS, CURR_VAL=addr_or_opts))
|
||||
|
||||
return _chromium_options
|
||||
|
||||
|
||||
def run_browser(chromium_options):
|
||||
if chromium_options.ws_address:
|
||||
d = Driver('', chromium_options.ws_address)
|
||||
browser_id = d.run('Target.getTargets')
|
||||
if 'error' in browser_id:
|
||||
raise BrowserConnectError(_S._lang.join(_S._lang.BROWSER_CONNECT_ERR2, INFO=browser_id['error']))
|
||||
browser_id = browser_id['targetInfos'][0]['browserContextId']
|
||||
is_headless = 'headless' in d.run('Browser.getVersion')['userAgent'].lower()
|
||||
is_exists = True
|
||||
d.stop()
|
||||
if 'devtools/browser' not in chromium_options.ws_address:
|
||||
return is_headless, browser_id, is_exists, True
|
||||
else:
|
||||
is_exists = connect_browser(chromium_options)
|
||||
|
||||
try:
|
||||
s = Session()
|
||||
s.trust_env = False
|
||||
s.keep_alive = False
|
||||
ws = s.get(f'http://{chromium_options.address}/json/version', headers={'Connection': 'close'}, timeout=2)
|
||||
if not ws and not chromium_options.ws_address:
|
||||
raise BrowserConnectError(_S._lang.BROWSER_CONNECT_ERR2)
|
||||
json = ws.json()
|
||||
if not chromium_options.ws_address:
|
||||
browser_id = json['webSocketDebuggerUrl'].split('/')[-1]
|
||||
is_headless = 'headless' in json['User-Agent'].lower()
|
||||
ws.close()
|
||||
s.close()
|
||||
ws_only = False
|
||||
except KeyError:
|
||||
raise BrowserConnectError(_S._lang.BROWSER_NOT_FOR_CONTROL)
|
||||
except:
|
||||
if chromium_options.ws_address:
|
||||
ws_only = True
|
||||
else:
|
||||
raise BrowserConnectError(_S._lang.BROWSER_CONNECT_ERR2)
|
||||
return is_headless, browser_id, is_exists, ws_only
|
||||
|
||||
|
||||
def _new_tab_by_js(browser: Chromium, url, tab_type, new_window):
|
||||
mix = tab_type == MixTab
|
||||
tab = browser._get_tab(mix=mix)
|
||||
if url and not match(r'^.*?://.*', url):
|
||||
raise IncorrectURLError(_S._lang.INVALID_URL, url=url)
|
||||
url = f'"{url}"' if url else '""'
|
||||
new = 'target="_new"' if new_window else 'target="_blank"'
|
||||
tid = browser._newest_tab_id
|
||||
tab.run_js(f'window.open({url}, {new})')
|
||||
tid = browser.wait.new_tab(curr_tab=tid)
|
||||
return browser._get_tab(tid, mix=mix)
|
||||
@@ -0,0 +1,319 @@
|
||||
# -*- coding:utf-8 -*-
|
||||
"""
|
||||
@Author : g1879
|
||||
@Contact : g1879@qq.com
|
||||
@Website : https://DrissionPage.cn
|
||||
@Copyright: (c) 2020 by g1879, Inc. All Rights Reserved.
|
||||
"""
|
||||
from threading import Lock
|
||||
from typing import List, Optional, Set, Dict, Union, Tuple, Literal, Any
|
||||
|
||||
from .driver import BrowserDriver, Driver
|
||||
from .._configs.chromium_options import ChromiumOptions
|
||||
from .._configs.session_options import SessionOptions
|
||||
from .._functions.cookies import CookiesList
|
||||
from .._pages.chromium_base import Timeout, ChromiumBase
|
||||
from .._pages.chromium_tab import ChromiumTab
|
||||
from .._pages.mix_tab import MixTab
|
||||
from .._units.downloader import DownloadManager
|
||||
from .._units.setter import BrowserSetter
|
||||
from .._units.states import BrowserStates
|
||||
from .._units.waiter import BrowserWaiter
|
||||
|
||||
|
||||
class Chromium(object):
|
||||
_BROWSERS: dict = ...
|
||||
_lock: Lock = ...
|
||||
|
||||
id: str = ...
|
||||
address: str = ...
|
||||
_ws_address: str = ...
|
||||
version: str = ...
|
||||
retry_times: int = ...
|
||||
retry_interval: float = ...
|
||||
|
||||
_set: Optional[BrowserSetter] = ...
|
||||
_wait: Optional[BrowserWaiter] = ...
|
||||
_states: Optional[BrowserStates] = ...
|
||||
_chromium_options: ChromiumOptions = ...
|
||||
_session_options: SessionOptions = ...
|
||||
_driver: BrowserDriver = ...
|
||||
_frames: dict = ...
|
||||
_drivers: Dict[str, Driver] = ...
|
||||
_all_drivers: Dict[str, Set[Driver]] = ...
|
||||
_relation: Dict[str, Optional[str]] = ...
|
||||
_process_id: Optional[int] = ...
|
||||
_dl_mgr: DownloadManager = ...
|
||||
_timeouts: Timeout = ...
|
||||
_load_mode: str = ...
|
||||
_download_path: str = ...
|
||||
_auto_handle_alert: Optional[bool] = ...
|
||||
_is_exists: bool = ...
|
||||
_is_headless: bool = ...
|
||||
_ws_only: bool = ...
|
||||
_disconnect_flag: bool = ...
|
||||
_none_ele_return_value: bool = ...
|
||||
_none_ele_value: Any = ...
|
||||
_newest_tab_id: Optional[str] = ...
|
||||
|
||||
def __new__(cls,
|
||||
addr_or_opts: Union[str, int, ChromiumOptions] = None,
|
||||
session_options: Union[SessionOptions, None, False] = None):
|
||||
"""
|
||||
:param addr_or_opts: 浏览器地址:端口、ws地址、ChromiumOptions对象或端口数字(int)
|
||||
:param session_options: 使用双模Tab时使用的默认Session配置,为None使用ini文件配置,为False不从ini读取
|
||||
"""
|
||||
...
|
||||
|
||||
def __init__(self, addr_or_opts: Union[str, int, ChromiumOptions] = None,
|
||||
session_options: Union[SessionOptions, None, False] = None):
|
||||
"""
|
||||
:param addr_or_opts: 浏览器地址:端口、ws地址、ChromiumOptions对象或端口数字(int)
|
||||
:param session_options: 使用双模Tab时使用的默认Session配置,为None使用ini文件配置,为False不从ini读取
|
||||
"""
|
||||
...
|
||||
|
||||
@property
|
||||
def user_data_path(self) -> str:
|
||||
"""返回用户文件夹路径"""
|
||||
...
|
||||
|
||||
@property
|
||||
def process_id(self) -> Optional[int]:
|
||||
"""返回浏览器进程id"""
|
||||
...
|
||||
|
||||
@property
|
||||
def timeout(self) -> float:
|
||||
"""返回基础超时设置"""
|
||||
...
|
||||
|
||||
@property
|
||||
def timeouts(self) -> Timeout:
|
||||
"""返回所有超时设置"""
|
||||
...
|
||||
|
||||
@property
|
||||
def load_mode(self) -> Literal['none', 'normal', 'eager']:
|
||||
"""返回页面加载模式,包括 'none', 'normal', 'eager' 三种"""
|
||||
...
|
||||
|
||||
@property
|
||||
def download_path(self) -> str:
|
||||
"""返回默认下载路径"""
|
||||
...
|
||||
|
||||
@property
|
||||
def set(self) -> BrowserSetter:
|
||||
"""返回用于设置的对象"""
|
||||
...
|
||||
|
||||
@property
|
||||
def states(self) -> BrowserStates:
|
||||
"""返回用于获取状态的对象"""
|
||||
...
|
||||
|
||||
@property
|
||||
def wait(self) -> BrowserWaiter:
|
||||
"""返回用于等待的对象"""
|
||||
...
|
||||
|
||||
@property
|
||||
def tabs_count(self) -> int:
|
||||
"""返回标签页数量,只统计page、webview类型"""
|
||||
...
|
||||
|
||||
@property
|
||||
def tab_ids(self) -> List[str]:
|
||||
"""返回所有标签页id组成的列表,只统计page、webview类型"""
|
||||
...
|
||||
|
||||
@property
|
||||
def latest_tab(self) -> Union[MixTab, str]:
|
||||
"""返回最新的标签页,最新标签页指最后创建或最后被激活的
|
||||
当Settings.singleton_tab_obj==True时返回Tab对象,否则返回tab id"""
|
||||
...
|
||||
|
||||
def cookies(self, all_info: bool = False) -> CookiesList:
|
||||
"""以list格式返回所有域名的cookies
|
||||
:param all_info: 是否返回所有内容,False则只返回name, value, domain
|
||||
:return: cookies组成的列表
|
||||
"""
|
||||
...
|
||||
|
||||
def new_tab(self,
|
||||
url: str = None,
|
||||
new_window: bool = False,
|
||||
background: bool = False,
|
||||
new_context: bool = False) -> MixTab:
|
||||
"""新建一个标签页
|
||||
:param url: 新标签页跳转到的网址,为None时新建空标签页
|
||||
:param new_window: 是否在新窗口打开标签页,隐身模式下无效
|
||||
:param background: 是否不激活新标签页,隐身模式和访客模式及new_window为True时无效
|
||||
:param new_context: 是否创建独立环境,隐身模式和访客模式下无效
|
||||
:return: 新标签页对象
|
||||
"""
|
||||
...
|
||||
|
||||
def get_tab(self,
|
||||
id_or_num: Union[str, int] = None,
|
||||
title: str = None,
|
||||
url: str = None,
|
||||
tab_type: Union[str, list, tuple] = 'page',
|
||||
as_id: bool = False) -> Union[MixTab, str]:
|
||||
"""获取一个标签页对象,id_or_num不为None时,后面几个参数无效
|
||||
:param id_or_num: 要获取的标签页id或序号,序号从1开始,可传入负数获取倒数第几个,不是视觉排列顺序,而是激活顺序
|
||||
:param title: 要匹配title的文本,模糊匹配,为None则匹配所有
|
||||
:param url: 要匹配url的文本,模糊匹配,为None则匹配所有
|
||||
:param tab_type: tab类型,可用列表输入多个,如 'page', 'iframe' 等,为None则匹配所有
|
||||
:param as_id: 是否返回标签页id而不是标签页对象
|
||||
:return: Tab对象
|
||||
"""
|
||||
...
|
||||
|
||||
def get_tabs(self,
|
||||
title: str = None,
|
||||
url: str = None,
|
||||
tab_type: Union[str, list, tuple] = 'page',
|
||||
as_id: bool = False) -> List[MixTab, str]:
|
||||
"""查找符合条件的tab,返回它们组成的列表,title和url是与关系
|
||||
:param title: 要匹配title的文本
|
||||
:param url: 要匹配url的文本
|
||||
:param tab_type: tab类型,可用列表输入多个
|
||||
:param as_id: 是否返回标签页id而不是标签页对象
|
||||
:return: Tab对象列表
|
||||
"""
|
||||
...
|
||||
|
||||
def close_tabs(self,
|
||||
tabs_or_ids: Union[str, ChromiumTab, List[Union[str, ChromiumTab]],
|
||||
Tuple[Union[str, ChromiumTab]]],
|
||||
others: bool = False) -> None:
|
||||
"""关闭传入的标签页,可传入多个
|
||||
:param tabs_or_ids: 指定的标签页对象或id,可用列表或元组传入多个
|
||||
:param others: 是否关闭指定标签页之外的
|
||||
:return: None
|
||||
"""
|
||||
...
|
||||
|
||||
def _close_tab(self, tab: Union[ChromiumBase, str]):
|
||||
"""关闭一个标签页
|
||||
:param tab: 标签页对象或id
|
||||
:return: None
|
||||
"""
|
||||
|
||||
def activate_tab(self, id_ind_tab: Union[int, str, ChromiumTab]) -> None:
|
||||
"""使一个标签页显示到前端
|
||||
:param id_ind_tab: 标签页id(str)、Tab对象或标签页序号(int),序号从1开始
|
||||
:return: None
|
||||
"""
|
||||
...
|
||||
|
||||
def reconnect(self) -> None:
|
||||
"""断开重连"""
|
||||
...
|
||||
|
||||
def clear_cache(self, cache: bool = True, cookies: bool = True) -> None:
|
||||
"""清除缓存,可选要清除的项
|
||||
:param cache: 是否清除cache
|
||||
:param cookies: 是否清除cookies
|
||||
:return: None
|
||||
"""
|
||||
...
|
||||
|
||||
def quit(self, timeout: float = 5, force: bool = False, del_data: bool = False) -> None:
|
||||
"""关闭浏览器
|
||||
:param timeout: 等待浏览器关闭超时时间(秒)
|
||||
:param force: 是否立刻强制终止进程
|
||||
:param del_data: 是否删除用户文件夹
|
||||
:return: None
|
||||
"""
|
||||
...
|
||||
|
||||
def _new_tab(self,
|
||||
mix: bool = True,
|
||||
url: str = None,
|
||||
new_window: bool = False,
|
||||
background: bool = False,
|
||||
new_context: bool = False) -> Union[ChromiumTab, MixTab]:
|
||||
"""新建一个标签页
|
||||
:param mix: 是否创建MixTab
|
||||
:param url: 新标签页跳转到的网址
|
||||
:param new_window: 是否在新窗口打开标签页
|
||||
:param background: 是否不激活新标签页,如new_window为True则无效
|
||||
:param new_context: 是否创建新的上下文
|
||||
:return: 新标签页对象
|
||||
"""
|
||||
...
|
||||
|
||||
def _get_tab(self,
|
||||
id_or_num: Union[str, int] = None,
|
||||
title: str = None,
|
||||
url: str = None,
|
||||
tab_type: Union[str, list, tuple] = 'page',
|
||||
mix: bool = True,
|
||||
as_id: bool = False) -> Union[ChromiumTab, str]:
|
||||
"""获取一个标签页对象,id_or_num不为None时,后面几个参数无效
|
||||
:param id_or_num: 要获取的标签页id或序号,序号从1开始,可传入负数获取倒数第几个,不是视觉排列顺序,而是激活顺序
|
||||
:param title: 要匹配title的文本,模糊匹配,为None则匹配所有
|
||||
:param url: 要匹配url的文本,模糊匹配,为None则匹配所有
|
||||
:param tab_type: tab类型,可用列表输入多个,如 'page', 'iframe' 等,为None则匹配所有
|
||||
:param mix: 是否返回可切换模式的Tab对象
|
||||
:param as_id: 是否返回标签页id而不是标签页对象,mix=False时无效
|
||||
:return: Tab对象
|
||||
"""
|
||||
...
|
||||
|
||||
def _get_tabs(self,
|
||||
title: str = None,
|
||||
url: str = None,
|
||||
tab_type: Union[str, list, tuple] = 'page',
|
||||
mix: bool = True,
|
||||
as_id: bool = False) -> List[ChromiumTab, str]:
|
||||
"""查找符合条件的tab,返回它们组成的列表,title和url是与关系
|
||||
:param title: 要匹配title的文本
|
||||
:param url: 要匹配url的文本
|
||||
:param tab_type: tab类型,可用列表输入多个
|
||||
:param mix: 是否返回可切换模式的Tab对象
|
||||
:param as_id: 是否返回标签页id而不是标签页对象,mix=False时无效
|
||||
:return: Tab对象列表
|
||||
"""
|
||||
...
|
||||
|
||||
def _run_cdp(self, cmd, **cmd_args) -> dict:
|
||||
"""执行Chrome DevTools Protocol语句
|
||||
:param cmd: 协议项目
|
||||
:param cmd_args: 参数
|
||||
:return: 执行的结果
|
||||
"""
|
||||
...
|
||||
|
||||
def _get_driver(self, tab_id: str, owner=None) -> Driver:
|
||||
"""新建并返回指定tab id的Driver
|
||||
:param tab_id: 标签页id
|
||||
:param owner: 使用该驱动的对象
|
||||
:return: Driver对象
|
||||
"""
|
||||
...
|
||||
|
||||
def _onTargetCreated(self, **kwargs): ...
|
||||
|
||||
def _onTargetDestroyed(self, **kwargs): ...
|
||||
|
||||
def _on_disconnect(self): ...
|
||||
|
||||
|
||||
def handle_options(addr_or_opts):
|
||||
"""设置浏览器启动属性
|
||||
:param addr_or_opts: 'ip:port'、ChromiumOptions、Driver
|
||||
:return: 返回ChromiumOptions对象
|
||||
"""
|
||||
...
|
||||
|
||||
|
||||
def run_browser(chromium_options)->Tuple[bool, str, bool, bool]:
|
||||
"""连接浏览器
|
||||
:param chromium_options: ChromiumOptions对象
|
||||
:return: 返回(是否无头, 浏览器id, 是否接管已存在, 是否只能用ws连接)
|
||||
"""
|
||||
...
|
||||
@@ -0,0 +1,229 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
@Author : g1879
|
||||
@Contact : g1879@qq.com
|
||||
@Website : https://DrissionPage.cn
|
||||
@Copyright: (c) 2020 by g1879, Inc. All Rights Reserved.
|
||||
"""
|
||||
from json import dumps, loads, JSONDecodeError
|
||||
from queue import Queue, Empty
|
||||
from threading import Thread
|
||||
from time import perf_counter, sleep
|
||||
|
||||
from requests import Session
|
||||
from requests import adapters
|
||||
from websocket import (WebSocketTimeoutException, WebSocketConnectionClosedException, create_connection,
|
||||
WebSocketException, WebSocketBadStatusException)
|
||||
|
||||
from .._functions.settings import Settings as _S
|
||||
from ..errors import PageDisconnectedError, BrowserConnectError
|
||||
|
||||
adapters.DEFAULT_RETRIES = 5
|
||||
|
||||
|
||||
class Driver(object):
|
||||
def __init__(self, _id, address, owner=None):
|
||||
self.id = _id
|
||||
self.address = address
|
||||
self.owner = owner
|
||||
self.alert_flag = False # 标记alert出现,跳过一条请求后复原
|
||||
|
||||
self._cur_id = 0
|
||||
self._ws = None
|
||||
|
||||
self._recv_th = Thread(target=self._recv_loop)
|
||||
self._handle_event_th = Thread(target=self._handle_event_loop)
|
||||
self._recv_th.daemon = True
|
||||
self._handle_event_th.daemon = True
|
||||
self._handle_immediate_event_th = None
|
||||
|
||||
self.is_running = False
|
||||
self.session_id = None
|
||||
|
||||
self.event_handlers = {}
|
||||
self.immediate_event_handlers = {}
|
||||
self.method_results = {}
|
||||
self.event_queue = Queue()
|
||||
self.immediate_event_queue = Queue()
|
||||
|
||||
self.start()
|
||||
|
||||
def _send(self, message, timeout=None):
|
||||
self._cur_id += 1
|
||||
ws_id = self._cur_id
|
||||
message['id'] = ws_id
|
||||
message_json = dumps(message)
|
||||
|
||||
end_time = perf_counter() + timeout if timeout is not None else None
|
||||
self.method_results[ws_id] = Queue()
|
||||
try:
|
||||
self._ws.send(message_json)
|
||||
if timeout == 0:
|
||||
self.method_results.pop(ws_id, None)
|
||||
return {'id': ws_id, 'result': {}}
|
||||
|
||||
except (OSError, WebSocketConnectionClosedException):
|
||||
self.method_results.pop(ws_id, None)
|
||||
return {'error': {'message': 'connection disconnected'}, 'type': 'connection_error'}
|
||||
|
||||
while self.is_running:
|
||||
try:
|
||||
result = self.method_results[ws_id].get(timeout=.2)
|
||||
self.method_results.pop(ws_id, None)
|
||||
return result
|
||||
|
||||
except Empty:
|
||||
if self.alert_flag and message['method'].startswith(('Input.', 'Runtime.')):
|
||||
return {'error': {'message': 'alert exists.'}, 'type': 'alert_exists'}
|
||||
|
||||
if timeout is not None and perf_counter() > end_time:
|
||||
self.method_results.pop(ws_id, None)
|
||||
return {'error': {'message': 'alert exists.'}, 'type': 'alert_exists'} \
|
||||
if self.alert_flag else {'error': {'message': 'timeout'}, 'type': 'timeout'}
|
||||
|
||||
continue
|
||||
|
||||
return {'error': {'message': 'connection disconnected'}, 'type': 'connection_error'}
|
||||
|
||||
def _recv_loop(self):
|
||||
while self.is_running:
|
||||
try:
|
||||
# self._ws.settimeout(1)
|
||||
msg_json = self._ws.recv()
|
||||
msg = loads(msg_json)
|
||||
except WebSocketTimeoutException:
|
||||
continue
|
||||
except (WebSocketException, OSError, WebSocketConnectionClosedException, JSONDecodeError):
|
||||
self._stop()
|
||||
return
|
||||
|
||||
if 'method' in msg:
|
||||
if msg['method'].startswith('Page.javascriptDialog'):
|
||||
self.alert_flag = msg['method'].endswith('Opening')
|
||||
function = self.immediate_event_handlers.get(msg['method'])
|
||||
if function:
|
||||
self._handle_immediate_event(function, msg['params'])
|
||||
else:
|
||||
self.event_queue.put(msg)
|
||||
|
||||
elif msg.get('id') in self.method_results:
|
||||
self.method_results[msg['id']].put(msg)
|
||||
|
||||
def _handle_event_loop(self):
|
||||
while self.is_running:
|
||||
try:
|
||||
event = self.event_queue.get(timeout=1)
|
||||
except Empty:
|
||||
continue
|
||||
|
||||
function = self.event_handlers.get(event['method'])
|
||||
if function:
|
||||
function(**event['params'])
|
||||
|
||||
self.event_queue.task_done()
|
||||
|
||||
def _handle_immediate_event_loop(self):
|
||||
while not self.immediate_event_queue.empty():
|
||||
function, kwargs = self.immediate_event_queue.get(timeout=1)
|
||||
try:
|
||||
function(**kwargs)
|
||||
except PageDisconnectedError:
|
||||
pass
|
||||
|
||||
def _handle_immediate_event(self, function, kwargs):
|
||||
self.immediate_event_queue.put((function, kwargs))
|
||||
if self._handle_immediate_event_th is None or not self._handle_immediate_event_th.is_alive():
|
||||
self._handle_immediate_event_th = Thread(target=self._handle_immediate_event_loop)
|
||||
self._handle_immediate_event_th.daemon = True
|
||||
self._handle_immediate_event_th.start()
|
||||
|
||||
def run(self, _method, sessionId=None, **kwargs):
|
||||
if not self.is_running:
|
||||
return {'error': 'connection disconnected', 'type': 'connection_error'}
|
||||
|
||||
timeout = kwargs.pop('_timeout', _S.cdp_timeout)
|
||||
if self.session_id:
|
||||
result = self._send({'method': _method, 'params': kwargs, 'sessionId': self.session_id}, timeout=timeout)
|
||||
elif sessionId:
|
||||
result = self._send({'method': _method, 'params': kwargs, 'sessionId': sessionId}, timeout=timeout)
|
||||
else:
|
||||
result = self._send({'method': _method, 'params': kwargs}, timeout=timeout)
|
||||
if 'result' not in result and 'error' in result:
|
||||
kwargs['_timeout'] = timeout
|
||||
return {'error': result['error']['message'], 'type': result.get('type', 'call_method_error'),
|
||||
'method': _method, 'args': kwargs, 'data': result['error'].get('data')}
|
||||
else:
|
||||
return result['result']
|
||||
|
||||
def start(self):
|
||||
self.is_running = True
|
||||
try:
|
||||
self._ws = create_connection(self.address, enable_multithread=True, suppress_origin=True)
|
||||
except WebSocketBadStatusException as e:
|
||||
if 'Handshake status 403 Forbidden' in str(e):
|
||||
raise EnvironmentError(_S._lang.join(_S._lang.UPGRADE_WS))
|
||||
else:
|
||||
raise
|
||||
except ConnectionRefusedError:
|
||||
raise BrowserConnectError(_S._lang.BROWSER_NOT_EXIST)
|
||||
self._recv_th.start()
|
||||
self._handle_event_th.start()
|
||||
return True
|
||||
|
||||
def stop(self):
|
||||
self._stop()
|
||||
while self._handle_event_th.is_alive() or self._recv_th.is_alive():
|
||||
sleep(.01)
|
||||
return True
|
||||
|
||||
def _stop(self):
|
||||
if not self.is_running:
|
||||
return False
|
||||
|
||||
self.is_running = False
|
||||
if self._ws:
|
||||
self._ws.close()
|
||||
self._ws = None
|
||||
|
||||
self.event_handlers.clear()
|
||||
self.method_results.clear()
|
||||
self.event_queue.queue.clear()
|
||||
|
||||
if hasattr(self.owner, '_on_disconnect'):
|
||||
self.owner._on_disconnect()
|
||||
|
||||
def set_callback(self, event, callback, immediate=False):
|
||||
handler = self.immediate_event_handlers if immediate else self.event_handlers
|
||||
if callback:
|
||||
handler[event] = callback
|
||||
else:
|
||||
handler.pop(event, None)
|
||||
|
||||
|
||||
class BrowserDriver(Driver):
|
||||
BROWSERS = {}
|
||||
|
||||
def __new__(cls, _id, address, owner):
|
||||
if _id in cls.BROWSERS:
|
||||
return cls.BROWSERS[_id]
|
||||
return object.__new__(cls)
|
||||
|
||||
def __init__(self, _id, address, owner):
|
||||
if hasattr(self, '_created'):
|
||||
return
|
||||
self._created = True
|
||||
BrowserDriver.BROWSERS[_id] = self
|
||||
super().__init__(_id, address, owner)
|
||||
|
||||
def __repr__(self):
|
||||
return f'<BrowserDriver {self.id}>'
|
||||
|
||||
@staticmethod
|
||||
def get(url):
|
||||
s = Session()
|
||||
s.trust_env = False
|
||||
s.keep_alive = False
|
||||
r = s.get(url, headers={'Connection': 'close'})
|
||||
r.close()
|
||||
s.close()
|
||||
return r
|
||||
@@ -0,0 +1,114 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
@Author : g1879
|
||||
@Contact : g1879@qq.com
|
||||
@Website : https://DrissionPage.cn
|
||||
@Copyright: (c) 2020 by g1879, Inc. All Rights Reserved.
|
||||
"""
|
||||
from queue import Queue
|
||||
from threading import Thread
|
||||
from typing import Union, Callable, Dict, Optional
|
||||
|
||||
from requests import Response
|
||||
from websocket import WebSocket
|
||||
|
||||
from .._base.chromium import Chromium
|
||||
|
||||
|
||||
class Driver(object):
|
||||
id: str
|
||||
address: str
|
||||
owner = ...
|
||||
alert_flag: bool
|
||||
_cur_id: int
|
||||
_ws: Optional[WebSocket]
|
||||
_recv_th: Thread
|
||||
_handle_event_th: Thread
|
||||
_handle_immediate_event_th: Optional[Thread]
|
||||
session_id: Optional[str] = ...
|
||||
is_running: bool
|
||||
event_handlers: dict
|
||||
immediate_event_handlers: dict
|
||||
method_results: dict
|
||||
event_queue: Queue
|
||||
immediate_event_queue: Queue
|
||||
|
||||
def __init__(self, _id: str, address: str, owner=None):
|
||||
"""
|
||||
:param _id: 标签页id
|
||||
:param address: 浏览器连接地址
|
||||
:param owner: 创建这个驱动的对象
|
||||
"""
|
||||
...
|
||||
|
||||
def _send(self, message: dict, timeout: float = None) -> dict:
|
||||
"""发送信息到浏览器,并返回浏览器返回的信息
|
||||
:param message: 发送给浏览器的数据
|
||||
:param timeout: 超时时间,为None表示无限
|
||||
:return: 浏览器返回的数据
|
||||
"""
|
||||
...
|
||||
|
||||
def _recv_loop(self) -> None:
|
||||
"""接收浏览器信息的守护线程方法"""
|
||||
...
|
||||
|
||||
def _handle_event_loop(self) -> None:
|
||||
"""当接收到浏览器信息,执行已绑定的方法"""
|
||||
...
|
||||
|
||||
def _handle_immediate_event_loop(self): ...
|
||||
|
||||
def _handle_immediate_event(self, function: Callable, kwargs: dict):
|
||||
"""处理立即执行的动作
|
||||
:param function: 要运行下方法
|
||||
:param kwargs: 方法参数
|
||||
:return: None
|
||||
"""
|
||||
...
|
||||
|
||||
def run(self, _method: str, **kwargs) -> dict:
|
||||
"""执行cdp方法
|
||||
:param _method: cdp方法名
|
||||
:param kwargs: cdp参数
|
||||
:return: 执行结果
|
||||
"""
|
||||
...
|
||||
|
||||
def start(self) -> bool:
|
||||
"""启动连接"""
|
||||
...
|
||||
|
||||
def stop(self) -> bool:
|
||||
"""中断连接"""
|
||||
...
|
||||
|
||||
def _stop(self) -> None:
|
||||
"""中断连接"""
|
||||
...
|
||||
|
||||
def set_callback(self, event: str, callback: Union[Callable, None], immediate: bool = False) -> None:
|
||||
"""绑定cdp event和回调方法
|
||||
:param event: cdp event
|
||||
:param callback: 绑定到cdp event的回调方法
|
||||
:param immediate: 是否要立即处理的动作
|
||||
:return: None
|
||||
"""
|
||||
...
|
||||
|
||||
|
||||
class BrowserDriver(Driver):
|
||||
BROWSERS: Dict[str, Driver] = ...
|
||||
owner: Chromium = ...
|
||||
|
||||
def __new__(cls, _id: str, address: str, owner: Chromium): ...
|
||||
|
||||
def __init__(self, _id: str, address: str, owner: Chromium): ...
|
||||
|
||||
@staticmethod
|
||||
def get(url) -> Response:
|
||||
"""
|
||||
:param url: 要访问的链接
|
||||
:return: Response对象
|
||||
"""
|
||||
...
|
||||
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
@@ -0,0 +1,439 @@
|
||||
# -*- coding:utf-8 -*-
|
||||
"""
|
||||
@Author : g1879
|
||||
@Contact : g1879@qq.com
|
||||
@Website : https://DrissionPage.cn
|
||||
@Copyright: (c) 2020 by g1879, Inc. All Rights Reserved.
|
||||
"""
|
||||
from pathlib import Path
|
||||
from re import search
|
||||
from urllib.parse import urlparse
|
||||
|
||||
from .options_manage import OptionsManager
|
||||
from .._functions.settings import Settings as _S
|
||||
|
||||
|
||||
class ChromiumOptions(object):
|
||||
def __init__(self, read_file=True, ini_path=None):
|
||||
self._user_data_path = None
|
||||
self._user = 'Default'
|
||||
self._prefs_to_del = []
|
||||
self.clear_file_flags = False
|
||||
self._is_headless = False
|
||||
self._ua_set = False
|
||||
self.ws_address = ''
|
||||
|
||||
if read_file is False:
|
||||
ini_path = False
|
||||
self.ini_path = None
|
||||
elif ini_path:
|
||||
ini_path = Path(ini_path).absolute()
|
||||
if not ini_path.exists():
|
||||
raise FileNotFoundError(_S._lang.join(_S._lang.INI_NOT_FOUND, PATH=ini_path))
|
||||
self.ini_path = str(ini_path)
|
||||
else:
|
||||
self.ini_path = str(Path(__file__).parent / 'configs.ini')
|
||||
|
||||
om = OptionsManager(ini_path)
|
||||
options = om.chromium_options
|
||||
self._download_path = om.paths.get('download_path', '.') or '.'
|
||||
self._tmp_path = om.paths.get('tmp_path', None) or None
|
||||
self._arguments = options.get('arguments', [])
|
||||
self._browser_path = options.get('browser_path', '')
|
||||
self._extensions = options.get('extensions', [])
|
||||
self._prefs = options.get('prefs', {})
|
||||
self._flags = options.get('flags', {})
|
||||
self._address = options.get('address', '')
|
||||
self._load_mode = options.get('load_mode', 'normal')
|
||||
self._system_user_path = options.get('system_user_path', False)
|
||||
self._existing_only = options.get('existing_only', False)
|
||||
self._new_env = options.get('new_env', False)
|
||||
for i in self._arguments:
|
||||
if i.startswith('--headless'):
|
||||
self._is_headless = True
|
||||
break
|
||||
|
||||
self._proxy = om.proxies.get('http', None) or om.proxies.get('https', None)
|
||||
|
||||
user_path = user = False
|
||||
for arg in self._arguments:
|
||||
if arg.startswith('--user-data-dir='):
|
||||
self.set_user_data_path(arg[16:])
|
||||
user_path = True
|
||||
if arg.startswith('--profile-directory='):
|
||||
self.set_user(arg[20:])
|
||||
user = True
|
||||
if user and user_path:
|
||||
break
|
||||
|
||||
timeouts = om.timeouts
|
||||
self._timeouts = {'base': timeouts['base'],
|
||||
'page_load': timeouts['page_load'],
|
||||
'script': timeouts['script']}
|
||||
|
||||
self._auto_port = options.get('auto_port', False)
|
||||
|
||||
others = om.others
|
||||
self._retry_times = others.get('retry_times', 3)
|
||||
self._retry_interval = others.get('retry_interval', 2)
|
||||
|
||||
return
|
||||
|
||||
def __repr__(self):
|
||||
return f'<ChromiumOptions at {id(self)}>'
|
||||
|
||||
@property
|
||||
def download_path(self):
|
||||
return self._download_path
|
||||
|
||||
@property
|
||||
def browser_path(self):
|
||||
return self._browser_path
|
||||
|
||||
@property
|
||||
def user_data_path(self):
|
||||
return self._user_data_path
|
||||
|
||||
@property
|
||||
def tmp_path(self):
|
||||
return self._tmp_path
|
||||
|
||||
@property
|
||||
def user(self):
|
||||
return self._user
|
||||
|
||||
@property
|
||||
def load_mode(self):
|
||||
return self._load_mode
|
||||
|
||||
@property
|
||||
def timeouts(self):
|
||||
return self._timeouts
|
||||
|
||||
@property
|
||||
def proxy(self):
|
||||
return self._proxy
|
||||
|
||||
@property
|
||||
def address(self):
|
||||
return self._address
|
||||
|
||||
@property
|
||||
def arguments(self):
|
||||
return self._arguments
|
||||
|
||||
@property
|
||||
def extensions(self):
|
||||
return self._extensions
|
||||
|
||||
@property
|
||||
def preferences(self):
|
||||
return self._prefs
|
||||
|
||||
@property
|
||||
def flags(self):
|
||||
return self._flags
|
||||
|
||||
@property
|
||||
def system_user_path(self):
|
||||
return self._system_user_path
|
||||
|
||||
@property
|
||||
def is_existing_only(self):
|
||||
return self._existing_only
|
||||
|
||||
@property
|
||||
def is_auto_port(self):
|
||||
return self._auto_port
|
||||
|
||||
@property
|
||||
def retry_times(self):
|
||||
return self._retry_times
|
||||
|
||||
@property
|
||||
def retry_interval(self):
|
||||
return self._retry_interval
|
||||
|
||||
@property
|
||||
def is_headless(self):
|
||||
return self._is_headless
|
||||
|
||||
def set_retry(self, times=None, interval=None):
|
||||
if times is not None:
|
||||
self._retry_times = times
|
||||
if interval is not None:
|
||||
self._retry_interval = interval
|
||||
return self
|
||||
|
||||
def set_argument(self, arg, value=None):
|
||||
self.remove_argument(arg)
|
||||
if value is not False:
|
||||
if arg == '--headless':
|
||||
if value == 'false':
|
||||
self._is_headless = False
|
||||
else:
|
||||
if value is None:
|
||||
value = 'new'
|
||||
self._arguments.append(f'--headless={value}')
|
||||
self._is_headless = True
|
||||
else:
|
||||
arg_str = arg if value is None else f'{arg}={value}'
|
||||
self._arguments.append(arg_str)
|
||||
elif arg == '--headless':
|
||||
self._is_headless = False
|
||||
return self
|
||||
|
||||
def remove_argument(self, value):
|
||||
elements_to_delete = [arg for arg in self._arguments if arg == value or arg.startswith(f'{value}=')]
|
||||
if not elements_to_delete:
|
||||
return self
|
||||
|
||||
if len(elements_to_delete) == 1:
|
||||
self._arguments.remove(elements_to_delete[0])
|
||||
else:
|
||||
self._arguments = [arg for arg in self._arguments if arg not in elements_to_delete]
|
||||
|
||||
return self
|
||||
|
||||
def add_extension(self, path):
|
||||
self._extensions.append(path)
|
||||
return self
|
||||
|
||||
def remove_extensions(self):
|
||||
self._extensions = []
|
||||
return self
|
||||
|
||||
def set_pref(self, arg, value):
|
||||
self._prefs[arg] = value
|
||||
return self
|
||||
|
||||
def remove_pref(self, arg):
|
||||
self._prefs.pop(arg, None)
|
||||
return self
|
||||
|
||||
def remove_pref_from_file(self, arg):
|
||||
self._prefs_to_del.append(arg)
|
||||
return self
|
||||
|
||||
def set_flag(self, flag, value=None):
|
||||
if value is False:
|
||||
self._flags.pop(flag, None)
|
||||
else:
|
||||
self._flags[flag] = value
|
||||
return self
|
||||
|
||||
def clear_flags_in_file(self):
|
||||
self.clear_file_flags = True
|
||||
return self
|
||||
|
||||
def clear_flags(self):
|
||||
self._flags = {}
|
||||
return self
|
||||
|
||||
def clear_arguments(self):
|
||||
self._arguments = []
|
||||
return self
|
||||
|
||||
def clear_prefs(self):
|
||||
self._prefs = {}
|
||||
return self
|
||||
|
||||
def set_timeouts(self, base=None, page_load=None, script=None):
|
||||
if base is not None:
|
||||
self._timeouts['base'] = base
|
||||
if page_load is not None:
|
||||
self._timeouts['page_load'] = page_load
|
||||
if script is not None:
|
||||
self._timeouts['script'] = script
|
||||
|
||||
return self
|
||||
|
||||
def set_user(self, user='Default'):
|
||||
self.set_argument('--profile-directory', user)
|
||||
self._user = user
|
||||
return self
|
||||
|
||||
def headless(self, on_off=True):
|
||||
on_off = 'new' if on_off else on_off
|
||||
return self.set_argument('--headless', on_off)
|
||||
|
||||
def no_imgs(self, on_off=True):
|
||||
on_off = None if on_off else False
|
||||
return self.set_argument('--blink-settings=imagesEnabled=false', on_off)
|
||||
|
||||
def no_js(self, on_off=True):
|
||||
on_off = None if on_off else False
|
||||
return self.set_argument('--disable-javascript', on_off)
|
||||
|
||||
def mute(self, on_off=True):
|
||||
on_off = None if on_off else False
|
||||
return self.set_argument('--mute-audio', on_off)
|
||||
|
||||
def incognito(self, on_off=True):
|
||||
on_off = None if on_off else False
|
||||
self.set_argument('--incognito', on_off)
|
||||
return self.set_argument('--inprivate', on_off) # edge
|
||||
|
||||
def new_env(self, on_off=True):
|
||||
self._new_env = on_off
|
||||
return self
|
||||
|
||||
def ignore_certificate_errors(self, on_off=True):
|
||||
on_off = None if on_off else False
|
||||
return self.set_argument('--ignore-certificate-errors', on_off)
|
||||
|
||||
def set_user_agent(self, user_agent):
|
||||
return self.set_argument('--user-agent', user_agent)
|
||||
|
||||
def set_proxy(self, proxy):
|
||||
if search(r'.*?:.*?@.*?\..*', proxy):
|
||||
print(_S._lang.UNSUPPORTED_USER_PROXY)
|
||||
if proxy.lower().startswith('socks'):
|
||||
print(_S._lang.UNSUPPORTED_SOCKS_PROXY)
|
||||
self._proxy = proxy
|
||||
return self.set_argument('--proxy-server', proxy)
|
||||
|
||||
def set_load_mode(self, value):
|
||||
if value not in ('normal', 'eager', 'none'):
|
||||
raise ValueError(_S._lang.join(_S._lang.INCORRECT_VAL_, 'value',
|
||||
ALLOW_VAL="'normal', 'eager', 'none'", CURR_VAL=value))
|
||||
self._load_mode = value.lower()
|
||||
return self
|
||||
|
||||
def set_paths(self, browser_path=None, local_port=None, address=None, download_path=None,
|
||||
user_data_path=None, cache_path=None):
|
||||
"""快捷的路径设置函数,即将废弃
|
||||
:param browser_path: 浏览器可执行文件路径
|
||||
:param local_port: 本地端口号
|
||||
:param address: 调试浏览器地址,例:127.0.0.1:9222
|
||||
:param download_path: 下载文件路径
|
||||
:param user_data_path: 用户数据路径
|
||||
:param cache_path: 缓存路径
|
||||
:return: 当前对象
|
||||
"""
|
||||
if browser_path is not None:
|
||||
self.set_browser_path(browser_path)
|
||||
|
||||
if local_port is not None:
|
||||
self.set_local_port(local_port)
|
||||
|
||||
if address is not None:
|
||||
self.set_address(address)
|
||||
|
||||
if download_path is not None:
|
||||
self.set_download_path(download_path)
|
||||
|
||||
if user_data_path is not None:
|
||||
self.set_user_data_path(user_data_path)
|
||||
|
||||
if cache_path is not None:
|
||||
self.set_cache_path(cache_path)
|
||||
|
||||
return self
|
||||
|
||||
def set_local_port(self, port):
|
||||
self._address = f'127.0.0.1:{port}'
|
||||
self._auto_port = False
|
||||
self.ws_address = ''
|
||||
return self
|
||||
|
||||
def set_address(self, address):
|
||||
address = address.replace('localhost', '127.0.0.1')
|
||||
self.ws_address = ''
|
||||
if address.startswith('http'):
|
||||
address = address.lstrip('htps:/')
|
||||
elif address.startswith(('ws:', 'wss:')):
|
||||
self.ws_address = address
|
||||
address = urlparse(address).netloc
|
||||
self._address = address
|
||||
self._auto_port = False
|
||||
return self
|
||||
|
||||
def set_browser_path(self, path):
|
||||
self._browser_path = str(path)
|
||||
return self
|
||||
|
||||
def set_download_path(self, path):
|
||||
self._download_path = '.' if path is None else str(path)
|
||||
return self
|
||||
|
||||
def set_tmp_path(self, path):
|
||||
self._tmp_path = str(path)
|
||||
return self
|
||||
|
||||
def set_user_data_path(self, path):
|
||||
u = str(path)
|
||||
self.set_argument('--user-data-dir', u)
|
||||
self._user_data_path = u
|
||||
self._auto_port = False
|
||||
return self
|
||||
|
||||
def set_cache_path(self, path):
|
||||
self.set_argument('--disk-cache-dir', str(path))
|
||||
return self
|
||||
|
||||
def use_system_user_path(self, on_off=True):
|
||||
self._system_user_path = on_off
|
||||
return self
|
||||
|
||||
def auto_port(self, on_off=True, scope=None):
|
||||
if on_off:
|
||||
self._auto_port = scope if scope else (9600, 59600)
|
||||
else:
|
||||
self._auto_port = False
|
||||
self._address = ''
|
||||
self.ws_address = ''
|
||||
return self
|
||||
|
||||
def existing_only(self, on_off=True):
|
||||
self._existing_only = on_off
|
||||
return self
|
||||
|
||||
def save(self, path=None):
|
||||
if path == 'default':
|
||||
path = (Path(__file__).parent / 'configs.ini').absolute()
|
||||
|
||||
elif path is None:
|
||||
if self.ini_path:
|
||||
path = Path(self.ini_path).absolute()
|
||||
else:
|
||||
path = (Path(__file__).parent / 'configs.ini').absolute()
|
||||
|
||||
else:
|
||||
path = Path(path).absolute()
|
||||
|
||||
path = path / 'config.ini' if path.is_dir() else path
|
||||
|
||||
if path.exists():
|
||||
om = OptionsManager(path)
|
||||
else:
|
||||
om = OptionsManager(self.ini_path or (Path(__file__).parent / 'configs.ini'))
|
||||
|
||||
# 设置chromium_options
|
||||
attrs = ('address', 'browser_path', 'arguments', 'extensions', 'user', 'load_mode',
|
||||
'auto_port', 'system_user_path', 'existing_only', 'flags', 'new_env')
|
||||
for i in attrs:
|
||||
om.set_item('chromium_options', i, self.__getattribute__(f'_{i}'))
|
||||
# 设置代理
|
||||
om.set_item('proxies', 'http', self._proxy or '')
|
||||
om.set_item('proxies', 'https', self._proxy or '')
|
||||
# 设置路径
|
||||
om.set_item('paths', 'download_path', self._download_path or '')
|
||||
om.set_item('paths', 'tmp_path', self._tmp_path or '')
|
||||
# 设置timeout
|
||||
om.set_item('timeouts', 'base', self._timeouts['base'])
|
||||
om.set_item('timeouts', 'page_load', self._timeouts['page_load'])
|
||||
om.set_item('timeouts', 'script', self._timeouts['script'])
|
||||
# 设置重试
|
||||
om.set_item('others', 'retry_times', self.retry_times)
|
||||
om.set_item('others', 'retry_interval', self.retry_interval)
|
||||
# 设置prefs
|
||||
om.set_item('chromium_options', 'prefs', self._prefs)
|
||||
|
||||
path = str(path)
|
||||
om.save(path)
|
||||
|
||||
return path
|
||||
|
||||
def save_to_default(self):
|
||||
return self.save('default')
|
||||
@@ -0,0 +1,399 @@
|
||||
# -*- coding:utf-8 -*-
|
||||
"""
|
||||
@Author : g1879
|
||||
@Contact : g1879@qq.com
|
||||
@Website : https://DrissionPage.cn
|
||||
@Copyright: (c) 2020 by g1879, Inc. All Rights Reserved.
|
||||
"""
|
||||
from pathlib import Path
|
||||
from typing import Union, Any, Literal, Optional, Tuple
|
||||
|
||||
|
||||
class ChromiumOptions(object):
|
||||
ini_path: Optional[str] = ...
|
||||
_user_data_path: Optional[str] = ...
|
||||
_download_path: str = ...
|
||||
_tmp_path: str = ...
|
||||
_arguments: list = ...
|
||||
_browser_path: str = ...
|
||||
_user: str = ...
|
||||
_load_mode: str = ...
|
||||
_timeouts: dict = ...
|
||||
_proxy: str = ...
|
||||
_address: str = ...
|
||||
_extensions: list = ...
|
||||
_prefs: dict = ...
|
||||
_flags: dict = ...
|
||||
_prefs_to_del: list = ...
|
||||
_new_env: bool = ...
|
||||
clear_file_flags: bool = ...
|
||||
_auto_port: Union[Tuple[int, int], False] = ...
|
||||
_system_user_path: bool = ...
|
||||
_existing_only: bool = ...
|
||||
_retry_times: int = ...
|
||||
_retry_interval: float = ...
|
||||
_is_headless: bool = ...
|
||||
_ua_set: bool = ...
|
||||
ws_address: Optional[str] = ...
|
||||
|
||||
def __init__(self,
|
||||
read_file: [bool, None] = True,
|
||||
ini_path: Union[str, Path] = None):
|
||||
"""
|
||||
:param read_file: 是否从默认ini文件中读取配置信息
|
||||
:param ini_path: ini文件路径,为None则读取默认ini文件
|
||||
"""
|
||||
...
|
||||
|
||||
@property
|
||||
def download_path(self) -> str:
|
||||
"""默认下载路径文件路径"""
|
||||
...
|
||||
|
||||
@property
|
||||
def browser_path(self) -> str:
|
||||
"""浏览器启动文件路径"""
|
||||
...
|
||||
|
||||
@property
|
||||
def user_data_path(self) -> str:
|
||||
"""返回用户数据文件夹路径"""
|
||||
...
|
||||
|
||||
@property
|
||||
def tmp_path(self) -> Optional[str]:
|
||||
"""返回临时文件夹路径"""
|
||||
...
|
||||
|
||||
@property
|
||||
def user(self) -> str:
|
||||
"""返回用户配置文件夹名称"""
|
||||
...
|
||||
|
||||
@property
|
||||
def load_mode(self) -> str:
|
||||
"""返回页面加载策略,'normal', 'eager', 'none'"""
|
||||
...
|
||||
|
||||
@property
|
||||
def timeouts(self) -> dict:
|
||||
"""返回timeouts设置"""
|
||||
...
|
||||
|
||||
@property
|
||||
def proxy(self) -> str:
|
||||
"""返回代理设置"""
|
||||
...
|
||||
|
||||
@property
|
||||
def address(self) -> str:
|
||||
"""返回浏览器地址,ip:port"""
|
||||
...
|
||||
|
||||
@property
|
||||
def arguments(self) -> list:
|
||||
"""返回浏览器命令行设置列表"""
|
||||
...
|
||||
|
||||
@property
|
||||
def extensions(self) -> list:
|
||||
"""以list形式返回要加载的插件路径"""
|
||||
...
|
||||
|
||||
@property
|
||||
def preferences(self) -> dict:
|
||||
"""返回用户首选项配置"""
|
||||
...
|
||||
|
||||
@property
|
||||
def flags(self) -> dict:
|
||||
"""返回实验项配置"""
|
||||
...
|
||||
|
||||
@property
|
||||
def system_user_path(self) -> bool:
|
||||
"""返回是否使用系统安装的浏览器所使用的用户数据文件夹"""
|
||||
...
|
||||
|
||||
@property
|
||||
def is_existing_only(self) -> bool:
|
||||
"""返回是否只接管现有浏览器方式"""
|
||||
...
|
||||
|
||||
@property
|
||||
def is_auto_port(self) -> Union[bool, Tuple[int, int]]:
|
||||
"""返回是否使用自动端口和用户文件,如指定范围则返回范围tuple"""
|
||||
...
|
||||
|
||||
@property
|
||||
def retry_times(self) -> int:
|
||||
"""返回连接失败时的重试次数"""
|
||||
...
|
||||
|
||||
@property
|
||||
def retry_interval(self) -> float:
|
||||
"""返回连接失败时的重试间隔(秒)"""
|
||||
...
|
||||
|
||||
@property
|
||||
def is_headless(self) -> bool:
|
||||
"""返回是否无头模式"""
|
||||
...
|
||||
|
||||
def set_retry(self, times: int = None, interval: float = None) -> ChromiumOptions:
|
||||
"""设置连接失败时的重试操作
|
||||
:param times: 重试次数
|
||||
:param interval: 重试间隔
|
||||
:return: 当前对象
|
||||
"""
|
||||
...
|
||||
|
||||
def set_argument(self, arg: str, value: Union[str, None, bool] = None) -> ChromiumOptions:
|
||||
"""设置浏览器配置的argument属性
|
||||
:param arg: 属性名
|
||||
:param value: 属性值,有值的属性传入值,没有的传入None,如传入False,删除该项
|
||||
:return: 当前对象
|
||||
"""
|
||||
...
|
||||
|
||||
def remove_argument(self, value: str) -> ChromiumOptions:
|
||||
"""移除一个argument项
|
||||
:param value: 设置项名,有值的设置项传入设置名称即可
|
||||
:return: 当前对象
|
||||
"""
|
||||
...
|
||||
|
||||
def add_extension(self, path: Union[str, Path]) -> ChromiumOptions:
|
||||
"""添加插件
|
||||
:param path: 插件路径,可指向文件夹
|
||||
:return: 当前对象
|
||||
"""
|
||||
...
|
||||
|
||||
def remove_extensions(self) -> ChromiumOptions:
|
||||
"""移除所有插件
|
||||
:return: 当前对象
|
||||
"""
|
||||
...
|
||||
|
||||
def set_pref(self, arg: str, value: Any) -> ChromiumOptions:
|
||||
"""设置Preferences文件中的用户设置项
|
||||
:param arg: 设置项名称
|
||||
:param value: 设置项值
|
||||
:return: 当前对象
|
||||
"""
|
||||
...
|
||||
|
||||
def remove_pref(self, arg: str) -> ChromiumOptions:
|
||||
"""删除用户首选项设置,不能删除已设置到文件中的项
|
||||
:param arg: 设置项名称
|
||||
:return: 当前对象
|
||||
"""
|
||||
...
|
||||
|
||||
def remove_pref_from_file(self, arg: str) -> ChromiumOptions:
|
||||
"""删除用户配置文件中已设置的项
|
||||
:param arg: 设置项名称
|
||||
:return: 当前对象
|
||||
"""
|
||||
...
|
||||
|
||||
def set_flag(self, flag: str, value: Union[int, str, bool] = None) -> ChromiumOptions:
|
||||
"""设置实验项
|
||||
:param flag: 设置项名称
|
||||
:param value: 设置项的值,为False则删除该项
|
||||
:return: 当前对象
|
||||
"""
|
||||
...
|
||||
|
||||
def clear_flags_in_file(self) -> ChromiumOptions:
|
||||
"""删除浏览器配置文件中已设置的实验项"""
|
||||
...
|
||||
|
||||
def clear_flags(self) -> ChromiumOptions:
|
||||
"""清空本对象已设置的flag参数"""
|
||||
...
|
||||
|
||||
def clear_arguments(self) -> ChromiumOptions:
|
||||
"""清空本对象已设置的argument参数"""
|
||||
...
|
||||
|
||||
def clear_prefs(self) -> ChromiumOptions:
|
||||
"""清空本对象已设置的pref参数"""
|
||||
...
|
||||
|
||||
def set_timeouts(self,
|
||||
base: float = None,
|
||||
page_load: float = None,
|
||||
script: float = None) -> ChromiumOptions:
|
||||
"""设置超时时间,单位为秒
|
||||
:param base: 默认超时时间
|
||||
:param page_load: 页面加载超时时间
|
||||
:param script: 脚本运行超时时间
|
||||
:return: 当前对象
|
||||
"""
|
||||
...
|
||||
|
||||
def set_user(self, user: str = 'Default') -> ChromiumOptions:
|
||||
"""设置使用哪个用户配置文件夹
|
||||
:param user: 用户文件夹名称
|
||||
:return: 当前对象
|
||||
"""
|
||||
...
|
||||
|
||||
def headless(self, on_off: bool = True) -> ChromiumOptions:
|
||||
"""设置是否隐藏浏览器界面
|
||||
:param on_off: 开或关
|
||||
:return: 当前对象
|
||||
"""
|
||||
...
|
||||
|
||||
def no_imgs(self, on_off: bool = True) -> ChromiumOptions:
|
||||
"""设置是否加载图片
|
||||
:param on_off: 开或关
|
||||
:return: 当前对象
|
||||
"""
|
||||
...
|
||||
|
||||
def no_js(self, on_off: bool = True) -> ChromiumOptions:
|
||||
"""设置是否禁用js
|
||||
:param on_off: 开或关
|
||||
:return: 当前对象
|
||||
"""
|
||||
...
|
||||
|
||||
def mute(self, on_off: bool = True) -> ChromiumOptions:
|
||||
"""设置是否静音
|
||||
:param on_off: 开或关
|
||||
:return: 当前对象
|
||||
"""
|
||||
...
|
||||
|
||||
def incognito(self, on_off: bool = True) -> ChromiumOptions:
|
||||
"""设置是否使用无痕模式启动
|
||||
:param on_off: 开或关
|
||||
:return: 当前对象
|
||||
"""
|
||||
...
|
||||
|
||||
def new_env(self, on_off: bool = True) -> ChromiumOptions:
|
||||
"""设置是否使用全新浏览器环境
|
||||
:param on_off: 开或关
|
||||
:return: 当前对象
|
||||
"""
|
||||
...
|
||||
|
||||
def ignore_certificate_errors(self, on_off=True) -> ChromiumOptions:
|
||||
"""设置是否忽略证书错误
|
||||
:param on_off: 开或关
|
||||
:return: 当前对象
|
||||
"""
|
||||
...
|
||||
|
||||
def set_user_agent(self, user_agent: str) -> ChromiumOptions:
|
||||
"""设置user agent
|
||||
:param user_agent: user agent文本
|
||||
:return: 当前对象
|
||||
"""
|
||||
...
|
||||
|
||||
def set_proxy(self, proxy: str) -> ChromiumOptions:
|
||||
"""设置代理
|
||||
:param proxy: 代理url和端口
|
||||
:return: 当前对象
|
||||
"""
|
||||
...
|
||||
|
||||
def set_load_mode(self, value: Literal['normal', 'eager', 'none']) -> ChromiumOptions:
|
||||
"""设置load_mode,可接收 'normal', 'eager', 'none'
|
||||
normal:默认情况下使用, 等待所有资源下载完成
|
||||
eager:DOM访问已准备就绪, 但其他资源 (如图像) 可能仍在加载中
|
||||
none:完全不阻塞
|
||||
:param value: 可接收 'normal', 'eager', 'none'
|
||||
:return: 当前对象
|
||||
"""
|
||||
...
|
||||
|
||||
def set_local_port(self, port: Union[str, int]) -> ChromiumOptions:
|
||||
"""设置本地启动端口,与set_address()和auto_port()互斥
|
||||
:param port: 端口号
|
||||
:return: 当前对象
|
||||
"""
|
||||
...
|
||||
|
||||
def set_address(self, address: str) -> ChromiumOptions:
|
||||
"""设置浏览器地址,格式'ip:port',与auto_port()和set_local_port()互斥
|
||||
:param address: 浏览器地址
|
||||
:return: 当前对象
|
||||
"""
|
||||
...
|
||||
|
||||
def set_browser_path(self, path: Union[str, Path]) -> ChromiumOptions:
|
||||
"""设置浏览器可执行文件路径
|
||||
:param path: 浏览器路径
|
||||
:return: 当前对象
|
||||
"""
|
||||
...
|
||||
|
||||
def set_download_path(self, path: Union[str, Path]) -> ChromiumOptions:
|
||||
"""设置下载文件保存路径
|
||||
:param path: 下载路径
|
||||
:return: 当前对象
|
||||
"""
|
||||
...
|
||||
|
||||
def set_tmp_path(self, path: Union[str, Path]) -> ChromiumOptions:
|
||||
"""设置临时文件文件保存路径
|
||||
:param path: 下载路径
|
||||
:return: 当前对象
|
||||
"""
|
||||
...
|
||||
|
||||
def set_user_data_path(self, path: Union[str, Path]) -> ChromiumOptions:
|
||||
"""设置用户文件夹路径
|
||||
:param path: 用户文件夹路径
|
||||
:return: 当前对象
|
||||
"""
|
||||
...
|
||||
|
||||
def set_cache_path(self, path: Union[str, Path]) -> ChromiumOptions:
|
||||
"""设置缓存路径
|
||||
:param path: 缓存路径
|
||||
:return: 当前对象
|
||||
"""
|
||||
...
|
||||
|
||||
def use_system_user_path(self, on_off: bool = True) -> ChromiumOptions:
|
||||
"""设置是否使用系统安装的浏览器默认用户文件夹
|
||||
:param on_off: 开或关
|
||||
:return: 当前对象
|
||||
"""
|
||||
...
|
||||
|
||||
def auto_port(self,
|
||||
on_off: bool = True,
|
||||
scope: Tuple[int, int] = None) -> ChromiumOptions:
|
||||
"""自动获取可用端口,与set_address()和set_local_port()互斥
|
||||
:param on_off: 是否开启自动获取端口号
|
||||
:param scope: 指定端口范围,不含最后的数字,为None则使用[9600-59600)
|
||||
:return: 当前对象
|
||||
"""
|
||||
...
|
||||
|
||||
def existing_only(self, on_off: bool = True) -> ChromiumOptions:
|
||||
"""设置只接管已有浏览器,不自动启动新的
|
||||
:param on_off: 是否开启自动获取端口号
|
||||
:return: 当前对象
|
||||
"""
|
||||
...
|
||||
|
||||
def save(self, path: Union[str, Path] = None) -> str:
|
||||
"""保存设置到文件
|
||||
:param path: ini文件的路径, None 保存到当前读取的配置文件,传入 'default' 保存到默认ini文件
|
||||
:return: 保存文件的绝对路径
|
||||
"""
|
||||
...
|
||||
|
||||
def save_to_default(self) -> str:
|
||||
"""保存当前配置到默认ini文件"""
|
||||
...
|
||||
@@ -0,0 +1,33 @@
|
||||
[paths]
|
||||
download_path =
|
||||
tmp_path =
|
||||
|
||||
[chromium_options]
|
||||
address = 127.0.0.1:9222
|
||||
browser_path = chrome
|
||||
arguments = ['--no-default-browser-check', '--disable-suggestions-ui', '--no-first-run', '--disable-infobars', '--disable-popup-blocking', '--hide-crash-restore-bubble', '--disable-features=PrivacySandboxSettings4']
|
||||
extensions = []
|
||||
prefs = {'profile.default_content_settings.popups': 0, 'profile.default_content_setting_values': {'notifications': 2}}
|
||||
flags = {}
|
||||
load_mode = normal
|
||||
user = Default
|
||||
auto_port = False
|
||||
system_user_path = False
|
||||
existing_only = False
|
||||
new_env = False
|
||||
|
||||
[session_options]
|
||||
headers = {'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/603.3.8 (KHTML, like Gecko) Version/10.1.2 Safari/603.3.8', 'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'connection': 'keep-alive', 'accept-charset': 'GB2312,utf-8;q=0.7,*;q=0.7'}
|
||||
|
||||
[timeouts]
|
||||
base = 10
|
||||
page_load = 30
|
||||
script = 30
|
||||
|
||||
[proxies]
|
||||
http =
|
||||
https =
|
||||
|
||||
[others]
|
||||
retry_times = 3
|
||||
retry_interval = 2
|
||||
@@ -0,0 +1,141 @@
|
||||
# -*- coding:utf-8 -*-
|
||||
"""
|
||||
@Author : g1879
|
||||
@Contact : g1879@qq.com
|
||||
@Website : https://DrissionPage.cn
|
||||
@Copyright: (c) 2020 by g1879, Inc. All Rights Reserved.
|
||||
"""
|
||||
from configparser import RawConfigParser, NoSectionError, NoOptionError
|
||||
from pathlib import Path
|
||||
from pprint import pprint
|
||||
|
||||
from .._functions.settings import Settings as _S
|
||||
|
||||
|
||||
class OptionsManager(object):
|
||||
def __init__(self, path=None):
|
||||
if path is False:
|
||||
self.ini_path = None
|
||||
else:
|
||||
default_configs = Path(__file__).parent / 'configs.ini'
|
||||
if path is None:
|
||||
dp_configs = Path('dp_configs.ini')
|
||||
if dp_configs.exists():
|
||||
self.ini_path = dp_configs
|
||||
else:
|
||||
self.ini_path = default_configs
|
||||
elif path == 'default':
|
||||
self.ini_path = default_configs
|
||||
elif isinstance(path, Path):
|
||||
self.ini_path = path
|
||||
else:
|
||||
self.ini_path = Path(path)
|
||||
|
||||
self._conf = RawConfigParser()
|
||||
if path is not False and self.ini_path.exists():
|
||||
self.file_exists = True
|
||||
self._conf.read(self.ini_path, encoding='utf-8')
|
||||
else:
|
||||
self.file_exists = False
|
||||
self._conf.add_section('paths')
|
||||
self._conf.add_section('chromium_options')
|
||||
self._conf.add_section('session_options')
|
||||
self._conf.add_section('timeouts')
|
||||
self._conf.add_section('proxies')
|
||||
self._conf.add_section('others')
|
||||
self.set_item('paths', 'download_path', '')
|
||||
self.set_item('paths', 'tmp_path', '')
|
||||
self.set_item('chromium_options', 'address', '127.0.0.1:9222')
|
||||
self.set_item('chromium_options', 'browser_path', 'chrome')
|
||||
self.set_item('chromium_options', 'arguments', "['--no-default-browser-check', '--disable-suggestions-ui', "
|
||||
"'--no-first-run', '--disable-infobars', "
|
||||
"'--disable-popup-blocking', '--hide-crash-restore-bubble', "
|
||||
"'--disable-features=PrivacySandboxSettings4']")
|
||||
self.set_item('chromium_options', 'extensions', '[]')
|
||||
self.set_item('chromium_options', 'prefs', "{'profile.default_content_settings.popups': 0, "
|
||||
"'profile.default_content_setting_values': "
|
||||
"{'notifications': 2}}")
|
||||
self.set_item('chromium_options', 'flags', '{}')
|
||||
self.set_item('chromium_options', 'load_mode', 'normal')
|
||||
self.set_item('chromium_options', 'user', 'Default')
|
||||
self.set_item('chromium_options', 'auto_port', 'False')
|
||||
self.set_item('chromium_options', 'system_user_path', 'False')
|
||||
self.set_item('chromium_options', 'existing_only', 'False')
|
||||
self.set_item('chromium_options', 'new_env', 'False')
|
||||
self.set_item('session_options', 'headers', "{'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X "
|
||||
"10_12_6) AppleWebKit/603.3.8 (KHTML, like Gecko) Version/10."
|
||||
"1.2 Safari/603.3.8', 'accept': 'text/html,application/xhtml"
|
||||
"+xml,application/xml;q=0.9,*/*;q=0.8', 'connection': "
|
||||
"'keep-alive', 'accept-charset': 'GB2312,utf-8;q=0.7,*;q=0.7'}")
|
||||
self.set_item('timeouts', 'base', '10')
|
||||
self.set_item('timeouts', 'page_load', '30')
|
||||
self.set_item('timeouts', 'script', '30')
|
||||
self.set_item('proxies', 'http', '')
|
||||
self.set_item('proxies', 'https', '')
|
||||
self.set_item('others', 'retry_times', '3')
|
||||
self.set_item('others', 'retry_interval', '2')
|
||||
|
||||
def __getattr__(self, item):
|
||||
return self.get_option(item)
|
||||
|
||||
def get_value(self, section, item):
|
||||
try:
|
||||
return eval(self._conf.get(section, item))
|
||||
except (SyntaxError, NameError):
|
||||
return self._conf.get(section, item)
|
||||
except NoSectionError and NoOptionError:
|
||||
return None
|
||||
|
||||
def get_option(self, section):
|
||||
items = self._conf.items(section)
|
||||
option = dict()
|
||||
|
||||
for j in items:
|
||||
try:
|
||||
option[j[0]] = eval(self._conf.get(section, j[0]))
|
||||
except Exception:
|
||||
option[j[0]] = self._conf.get(section, j[0])
|
||||
|
||||
return option
|
||||
|
||||
def set_item(self, section, item, value):
|
||||
self._conf.set(section, item, str(value))
|
||||
self.__setattr__(f'_{section}', None)
|
||||
return self
|
||||
|
||||
def remove_item(self, section, item):
|
||||
self._conf.remove_option(section, item)
|
||||
return self
|
||||
|
||||
def save(self, path=None):
|
||||
default_path = (Path(__file__).parent / 'configs.ini').absolute()
|
||||
if path == 'default':
|
||||
path = default_path
|
||||
elif path is None:
|
||||
if self.ini_path is None:
|
||||
raise RuntimeError(_S._lang.join(_S._lang.INI_NOT_SET))
|
||||
path = self.ini_path.absolute()
|
||||
else:
|
||||
path = Path(path).absolute()
|
||||
|
||||
path = path / 'config.ini' if path.is_dir() else path
|
||||
path.parent.mkdir(exist_ok=True, parents=True)
|
||||
|
||||
path = str(path)
|
||||
self._conf.write(open(path, 'w', encoding='utf-8'))
|
||||
|
||||
print(f'{_S._lang.OPTIONS_HAVE_SAVED}: {path}')
|
||||
if path == str(default_path):
|
||||
print(_S._lang.AUTO_LOAD_TIP)
|
||||
|
||||
self.file_exists = True
|
||||
return path
|
||||
|
||||
def save_to_default(self):
|
||||
return self.save('default')
|
||||
|
||||
def show(self):
|
||||
for i in self._conf.sections():
|
||||
print(f'[{i}]')
|
||||
pprint(self.get_option(i))
|
||||
print()
|
||||
@@ -0,0 +1,77 @@
|
||||
# -*- coding:utf-8 -*-
|
||||
"""
|
||||
@Author : g1879
|
||||
@Contact : g1879@qq.com
|
||||
@Website : https://DrissionPage.cn
|
||||
@Copyright: (c) 2020 by g1879, Inc. All Rights Reserved.
|
||||
"""
|
||||
from configparser import RawConfigParser
|
||||
from pathlib import Path
|
||||
from typing import Any, Optional, Union
|
||||
|
||||
|
||||
class OptionsManager(object):
|
||||
"""管理配置文件内容的类"""
|
||||
ini_path: Optional[Path] = ...
|
||||
file_exists: bool = ...
|
||||
_conf: RawConfigParser = ...
|
||||
|
||||
def __init__(self, path: Union[Path, str] = None):
|
||||
"""初始化,读取配置文件,如没有设置临时文件夹,则设置并新建
|
||||
:param path: ini文件的路径,为None则找项目文件夹下的,找不到则读取模块文件夹下的
|
||||
"""
|
||||
...
|
||||
|
||||
def __getattr__(self, item) -> dict:
|
||||
"""以dict形似返回获取大项信息
|
||||
:param item: 项名
|
||||
:return: None
|
||||
"""
|
||||
...
|
||||
|
||||
def get_value(self, section: str, item: str) -> Any:
|
||||
"""获取配置的值
|
||||
:param section: 段名
|
||||
:param item: 项名
|
||||
:return: 项值
|
||||
"""
|
||||
...
|
||||
|
||||
def get_option(self, section: str) -> dict:
|
||||
"""把section内容以字典方式返回
|
||||
:param section: 段名
|
||||
:return: 段内容生成的字典
|
||||
"""
|
||||
...
|
||||
|
||||
def set_item(self, section: str, item: str, value: Any) -> None:
|
||||
"""设置配置值
|
||||
:param section: 段名
|
||||
:param item: 项名
|
||||
:param value: 项值
|
||||
:return: None
|
||||
"""
|
||||
...
|
||||
|
||||
def remove_item(self, section: str, item: str) -> None:
|
||||
"""删除配置值
|
||||
:param section: 段名
|
||||
:param item: 项名
|
||||
:return: None
|
||||
"""
|
||||
...
|
||||
|
||||
def save(self, path: str = None) -> str:
|
||||
"""保存配置文件
|
||||
:param path: ini文件的路径,传入 'default' 保存到默认ini文件
|
||||
:return: 保存路径
|
||||
"""
|
||||
...
|
||||
|
||||
def save_to_default(self) -> str:
|
||||
"""保存当前配置到默认ini文件"""
|
||||
...
|
||||
|
||||
def show(self) -> None:
|
||||
"""打印所有设置信息"""
|
||||
...
|
||||
@@ -0,0 +1,371 @@
|
||||
# -*- coding:utf-8 -*-
|
||||
"""
|
||||
@Author : g1879
|
||||
@Contact : g1879@qq.com
|
||||
@Website : https://DrissionPage.cn
|
||||
@Copyright: (c) 2020 by g1879, Inc. All Rights Reserved.
|
||||
"""
|
||||
from copy import copy
|
||||
from pathlib import Path
|
||||
|
||||
from requests import Session
|
||||
from requests.structures import CaseInsensitiveDict
|
||||
|
||||
from .options_manage import OptionsManager
|
||||
from .._functions.cookies import cookies_to_tuple, set_session_cookies
|
||||
from .._functions.settings import Settings as _S
|
||||
from .._functions.web import format_headers
|
||||
|
||||
|
||||
class SessionOptions(object):
|
||||
|
||||
def __init__(self, read_file=True, ini_path=None):
|
||||
self.ini_path = None
|
||||
self._download_path = '.'
|
||||
self._timeout = 10
|
||||
self._del_set = set() # 记录要从ini文件删除的参数
|
||||
|
||||
if read_file is False:
|
||||
ini_path = False
|
||||
self.ini_path = None
|
||||
elif ini_path:
|
||||
ini_path = Path(ini_path).absolute()
|
||||
if not ini_path.exists():
|
||||
raise FileNotFoundError(_S._lang.join(_S._lang.INI_NOT_FOUND, PATH=ini_path))
|
||||
self.ini_path = str(ini_path)
|
||||
else:
|
||||
self.ini_path = str(Path(__file__).parent / 'configs.ini')
|
||||
om = OptionsManager(ini_path)
|
||||
|
||||
self._headers = None
|
||||
self._cookies = None
|
||||
self._auth = None
|
||||
self._proxies = None
|
||||
self._hooks = None
|
||||
self._params = None
|
||||
self._verify = None
|
||||
self._cert = None
|
||||
self._adapters = None
|
||||
self._stream = None
|
||||
self._trust_env = None
|
||||
self._max_redirects = None
|
||||
|
||||
options = om.session_options
|
||||
if options.get('headers', None) is not None:
|
||||
self.set_headers(options['headers'])
|
||||
|
||||
if options.get('cookies', None) is not None:
|
||||
self.set_cookies(options['cookies'])
|
||||
|
||||
if options.get('auth', None) is not None:
|
||||
self._auth = options['auth']
|
||||
|
||||
if options.get('params', None) is not None:
|
||||
self._params = options['params']
|
||||
|
||||
if options.get('verify', None) is not None:
|
||||
self._verify = options['verify']
|
||||
|
||||
if options.get('cert', None) is not None:
|
||||
self._cert = options['cert']
|
||||
|
||||
if options.get('stream', None) is not None:
|
||||
self._stream = options['stream']
|
||||
|
||||
if options.get('trust_env', None) is not None:
|
||||
self._trust_env = options['trust_env']
|
||||
|
||||
if options.get('max_redirects', None) is not None:
|
||||
self._max_redirects = options['max_redirects']
|
||||
|
||||
self.set_proxies(om.proxies.get('http', None), om.proxies.get('https', None))
|
||||
self._timeout = om.timeouts.get('base', 10)
|
||||
self._download_path = om.paths.get('download_path', '.') or '.'
|
||||
|
||||
others = om.others
|
||||
self._retry_times = others.get('retry_times', 3)
|
||||
self._retry_interval = others.get('retry_interval', 2)
|
||||
|
||||
def __repr__(self):
|
||||
return f'<SessionOptions at {id(self)}>'
|
||||
|
||||
# ===========须独立处理的项开始============
|
||||
@property
|
||||
def download_path(self):
|
||||
return self._download_path
|
||||
|
||||
def set_download_path(self, path):
|
||||
self._download_path = '.' if path is None else str(path)
|
||||
return self
|
||||
|
||||
@property
|
||||
def timeout(self):
|
||||
return self._timeout
|
||||
|
||||
def set_timeout(self, second):
|
||||
self._timeout = second
|
||||
return self
|
||||
|
||||
@property
|
||||
def proxies(self):
|
||||
if self._proxies is None:
|
||||
self._proxies = {}
|
||||
return self._proxies
|
||||
|
||||
def set_proxies(self, http=None, https=None):
|
||||
self._sets('proxies', {'http': http, 'https': https})
|
||||
return self
|
||||
|
||||
@property
|
||||
def retry_times(self):
|
||||
return self._retry_times
|
||||
|
||||
@property
|
||||
def retry_interval(self):
|
||||
return self._retry_interval
|
||||
|
||||
def set_retry(self, times=None, interval=None):
|
||||
if times is not None:
|
||||
self._retry_times = times
|
||||
if interval is not None:
|
||||
self._retry_interval = interval
|
||||
return self
|
||||
|
||||
# ===========须独立处理的项结束============
|
||||
|
||||
@property
|
||||
def headers(self):
|
||||
if self._headers is None:
|
||||
self._headers = {}
|
||||
return self._headers
|
||||
|
||||
def set_headers(self, headers):
|
||||
if headers is None:
|
||||
self._headers = None
|
||||
self._del_set.add('headers')
|
||||
else:
|
||||
headers = format_headers(headers)
|
||||
self._headers = {key.lower(): headers[key] for key in headers}
|
||||
return self
|
||||
|
||||
def set_a_header(self, name, value):
|
||||
if self._headers is None:
|
||||
self._headers = {}
|
||||
|
||||
self._headers[name.lower()] = value
|
||||
return self
|
||||
|
||||
def remove_a_header(self, name):
|
||||
if self._headers is None:
|
||||
return self
|
||||
|
||||
self._headers.pop(name.lower(), None)
|
||||
|
||||
return self
|
||||
|
||||
def clear_headers(self):
|
||||
self._headers = None
|
||||
self._del_set.add('headers')
|
||||
|
||||
@property
|
||||
def cookies(self):
|
||||
if self._cookies is None:
|
||||
self._cookies = []
|
||||
return self._cookies
|
||||
|
||||
def set_cookies(self, cookies):
|
||||
cookies = cookies if cookies is None else list(cookies_to_tuple(cookies))
|
||||
self._sets('cookies', cookies)
|
||||
return self
|
||||
|
||||
@property
|
||||
def auth(self):
|
||||
return self._auth
|
||||
|
||||
def set_auth(self, auth):
|
||||
self._sets('auth', auth)
|
||||
return self
|
||||
|
||||
@property
|
||||
def hooks(self):
|
||||
if self._hooks is None:
|
||||
self._hooks = {}
|
||||
return self._hooks
|
||||
|
||||
def set_hooks(self, hooks):
|
||||
self._hooks = hooks
|
||||
return self
|
||||
|
||||
@property
|
||||
def params(self):
|
||||
if self._params is None:
|
||||
self._params = {}
|
||||
return self._params
|
||||
|
||||
def set_params(self, params):
|
||||
self._sets('params', params)
|
||||
return self
|
||||
|
||||
@property
|
||||
def verify(self):
|
||||
return self._verify
|
||||
|
||||
def set_verify(self, on_off):
|
||||
self._sets('verify', on_off)
|
||||
return self
|
||||
|
||||
@property
|
||||
def cert(self):
|
||||
return self._cert
|
||||
|
||||
def set_cert(self, cert):
|
||||
self._sets('cert', cert)
|
||||
return self
|
||||
|
||||
@property
|
||||
def adapters(self):
|
||||
if self._adapters is None:
|
||||
self._adapters = []
|
||||
return self._adapters
|
||||
|
||||
def add_adapter(self, url, adapter):
|
||||
self._adapters.append((url, adapter))
|
||||
return self
|
||||
|
||||
@property
|
||||
def stream(self):
|
||||
return self._stream
|
||||
|
||||
def set_stream(self, on_off):
|
||||
self._sets('stream', on_off)
|
||||
return self
|
||||
|
||||
@property
|
||||
def trust_env(self):
|
||||
return self._trust_env
|
||||
|
||||
def set_trust_env(self, on_off):
|
||||
self._sets('trust_env', on_off)
|
||||
return self
|
||||
|
||||
@property
|
||||
def max_redirects(self):
|
||||
return self._max_redirects
|
||||
|
||||
def set_max_redirects(self, times):
|
||||
self._sets('max_redirects', times)
|
||||
return self
|
||||
|
||||
def _sets(self, arg, val):
|
||||
if val is None:
|
||||
self.__setattr__(f'_{arg}', None)
|
||||
self._del_set.add(arg)
|
||||
else:
|
||||
self.__setattr__(f'_{arg}', val)
|
||||
if arg in self._del_set:
|
||||
self._del_set.remove(arg)
|
||||
|
||||
def save(self, path=None):
|
||||
if path == 'default':
|
||||
path = (Path(__file__).parent / 'configs.ini').absolute()
|
||||
|
||||
elif path is None:
|
||||
if self.ini_path:
|
||||
path = Path(self.ini_path).absolute()
|
||||
else:
|
||||
path = (Path(__file__).parent / 'configs.ini').absolute()
|
||||
|
||||
else:
|
||||
path = Path(path).absolute()
|
||||
|
||||
path = path / 'config.ini' if path.is_dir() else path
|
||||
|
||||
if path.exists():
|
||||
om = OptionsManager(path)
|
||||
else:
|
||||
om = OptionsManager(self.ini_path or (Path(__file__).parent / 'configs.ini'))
|
||||
|
||||
options = session_options_to_dict(self)
|
||||
|
||||
for i in options:
|
||||
if i not in ('download_path', 'timeout', 'proxies'):
|
||||
om.set_item('session_options', i, options[i])
|
||||
|
||||
om.set_item('paths', 'download_path', self.download_path or '')
|
||||
om.set_item('timeouts', 'base', self.timeout)
|
||||
om.set_item('proxies', 'http', self.proxies.get('http', ''))
|
||||
om.set_item('proxies', 'https', self.proxies.get('https', ''))
|
||||
om.set_item('others', 'retry_times', self.retry_times)
|
||||
om.set_item('others', 'retry_interval', self.retry_interval)
|
||||
|
||||
for i in self._del_set:
|
||||
if i == 'download_path':
|
||||
om.set_item('paths', 'download_path', '')
|
||||
elif i == 'proxies':
|
||||
om.set_item('proxies', 'http', '')
|
||||
om.set_item('proxies', 'https', '')
|
||||
else:
|
||||
om.remove_item('session_options', i)
|
||||
|
||||
path = str(path)
|
||||
om.save(path)
|
||||
|
||||
return path
|
||||
|
||||
def save_to_default(self):
|
||||
return self.save('default')
|
||||
|
||||
def as_dict(self):
|
||||
return session_options_to_dict(self)
|
||||
|
||||
def make_session(self):
|
||||
s = Session()
|
||||
h = CaseInsensitiveDict(self.headers) if self.headers else CaseInsensitiveDict()
|
||||
|
||||
if self.cookies:
|
||||
set_session_cookies(s, self.cookies)
|
||||
if self.adapters:
|
||||
for url, adapter in self.adapters:
|
||||
s.mount(url, adapter)
|
||||
|
||||
for i in ['auth', 'proxies', 'hooks', 'params', 'verify', 'cert', 'stream', 'trust_env', 'max_redirects']:
|
||||
attr = self.__getattribute__(i)
|
||||
if attr:
|
||||
s.__setattr__(i, attr)
|
||||
|
||||
return s, h
|
||||
|
||||
def from_session(self, session, headers=None):
|
||||
self._headers = CaseInsensitiveDict(copy(session.headers).update(headers)) if headers else session.headers
|
||||
self._cookies = session.cookies
|
||||
self._auth = session.auth
|
||||
self._proxies = session.proxies
|
||||
self._hooks = session.hooks
|
||||
self._params = session.params
|
||||
self._verify = session.verify
|
||||
self._cert = session.cert
|
||||
self._stream = session.stream
|
||||
self._trust_env = session.trust_env
|
||||
self._max_redirects = session.max_redirects
|
||||
if session.adapters:
|
||||
self._adapters = [(k, i) for k, i in session.adapters.items()]
|
||||
return self
|
||||
|
||||
|
||||
def session_options_to_dict(options):
|
||||
if options in (False, None):
|
||||
return SessionOptions(read_file=False).as_dict()
|
||||
|
||||
if isinstance(options, dict):
|
||||
return options
|
||||
|
||||
re_dict = dict()
|
||||
attrs = ['headers', 'cookies', 'proxies', 'params', 'verify', 'stream', 'trust_env', 'cert',
|
||||
'max_redirects', 'timeout', 'download_path']
|
||||
|
||||
for attr in attrs:
|
||||
val = options.__getattribute__(f'_{attr}')
|
||||
if val is not None:
|
||||
re_dict[attr] = val
|
||||
|
||||
return re_dict
|
||||
@@ -0,0 +1,298 @@
|
||||
# -*- coding:utf-8 -*-
|
||||
"""
|
||||
@Author : g1879
|
||||
@Contact : g1879@qq.com
|
||||
@Website : https://DrissionPage.cn
|
||||
@Copyright: (c) 2020 by g1879, Inc. All Rights Reserved.
|
||||
"""
|
||||
from http.cookiejar import CookieJar, Cookie
|
||||
from pathlib import Path
|
||||
from typing import Any, Union, Tuple, Optional
|
||||
|
||||
from requests import Session
|
||||
from requests.adapters import HTTPAdapter
|
||||
from requests.auth import HTTPBasicAuth
|
||||
from requests.cookies import RequestsCookieJar
|
||||
from requests.structures import CaseInsensitiveDict
|
||||
|
||||
|
||||
class SessionOptions(object):
|
||||
"""requests的Session对象配置类"""
|
||||
|
||||
ini_path: Optional[str] = ...
|
||||
_download_path: str = ...
|
||||
_headers: Union[dict, CaseInsensitiveDict, None] = ...
|
||||
_cookies: Union[list, RequestsCookieJar, None] = ...
|
||||
_auth: Optional[tuple] = ...
|
||||
_proxies: Optional[dict] = ...
|
||||
_hooks: Optional[dict] = ...
|
||||
_params: Union[dict, None] = ...
|
||||
_verify: Optional[bool] = ...
|
||||
_cert: Union[str, tuple, None] = ...
|
||||
_adapters: Optional[list] = ...
|
||||
_stream: Optional[bool] = ...
|
||||
_trust_env: Optional[bool] = ...
|
||||
_max_redirects: Optional[int] = ...
|
||||
_timeout: float = ...
|
||||
_del_set: set = ...
|
||||
_retry_times: int = ...
|
||||
_retry_interval: float = ...
|
||||
|
||||
def __init__(self,
|
||||
read_file: [bool, None] = True,
|
||||
ini_path: Union[str, Path] = None):
|
||||
"""
|
||||
:param read_file: 是否从文件读取配置
|
||||
:param ini_path: ini文件路径
|
||||
"""
|
||||
...
|
||||
|
||||
@property
|
||||
def download_path(self) -> str:
|
||||
"""返回默认下载路径属性信息"""
|
||||
...
|
||||
|
||||
def set_download_path(self, path: Union[str, Path]) -> SessionOptions:
|
||||
"""设置默认下载路径
|
||||
:param path: 下载路径
|
||||
:return: 返回当前对象
|
||||
"""
|
||||
...
|
||||
|
||||
@property
|
||||
def timeout(self) -> float:
|
||||
"""返回timeout属性信息"""
|
||||
...
|
||||
|
||||
def set_timeout(self, second: float) -> SessionOptions:
|
||||
"""设置超时信息
|
||||
:param second: 秒数
|
||||
:return: 返回当前对象
|
||||
"""
|
||||
...
|
||||
|
||||
@property
|
||||
def proxies(self) -> dict:
|
||||
"""返回proxies设置信息"""
|
||||
...
|
||||
|
||||
def set_proxies(self, http: Union[str, None], https: Union[str, None] = None) -> SessionOptions:
|
||||
"""设置proxies参数
|
||||
:param http: http代理地址
|
||||
:param https: https代理地址
|
||||
:return: 返回当前对象
|
||||
"""
|
||||
...
|
||||
|
||||
@property
|
||||
def retry_times(self) -> int:
|
||||
"""返回连接失败时的重试次数"""
|
||||
...
|
||||
|
||||
@property
|
||||
def retry_interval(self) -> float:
|
||||
"""返回连接失败时的重试间隔(秒)"""
|
||||
...
|
||||
|
||||
def set_retry(self, times: int = None, interval: float = None) -> SessionOptions:
|
||||
"""设置连接失败时的重试操作
|
||||
:param times: 重试次数
|
||||
:param interval: 重试间隔
|
||||
:return: 当前对象
|
||||
"""
|
||||
...
|
||||
|
||||
@property
|
||||
def headers(self) -> dict:
|
||||
"""返回headers设置信息"""
|
||||
...
|
||||
|
||||
def set_headers(self, headers: Union[dict, str, None]) -> SessionOptions:
|
||||
"""设置headers参数
|
||||
:param headers: 参数值,传入None可在ini文件标记删除
|
||||
:return: 返回当前对象
|
||||
"""
|
||||
...
|
||||
|
||||
def set_a_header(self, name: str, value: str) -> SessionOptions:
|
||||
"""设置headers中一个项
|
||||
:param name: 设置名称
|
||||
:param value: 设置值
|
||||
:return: 返回当前对象
|
||||
"""
|
||||
...
|
||||
|
||||
def remove_a_header(self, name: str) -> SessionOptions:
|
||||
"""从headers中删除一个设置
|
||||
:param name: 要删除的设置
|
||||
:return: 返回当前对象
|
||||
"""
|
||||
...
|
||||
|
||||
def clear_headers(self) -> SessionOptions:
|
||||
"""清空已设置的header参数"""
|
||||
...
|
||||
|
||||
@property
|
||||
def cookies(self) -> list:
|
||||
"""以list形式返回cookies"""
|
||||
...
|
||||
|
||||
def set_cookies(self, cookies: Union[Cookie, CookieJar, list, tuple, str, dict, None]) -> SessionOptions:
|
||||
"""设置一个或多个cookies信息
|
||||
:param cookies: cookies,可为Cookie, CookieJar, list, tuple, str, dict,传入None可在ini文件标记删除
|
||||
:return: 返回当前对象
|
||||
"""
|
||||
...
|
||||
|
||||
@property
|
||||
def auth(self) -> Union[Tuple[str, str], HTTPBasicAuth]:
|
||||
"""返回认证设置信息"""
|
||||
...
|
||||
|
||||
def set_auth(self, auth: Union[Tuple[str, str], HTTPBasicAuth, None]) -> SessionOptions:
|
||||
"""设置认证元组或对象
|
||||
:param auth: 认证元组或对象
|
||||
:return: 返回当前对象
|
||||
"""
|
||||
...
|
||||
|
||||
@property
|
||||
def hooks(self) -> dict:
|
||||
"""返回回调方法"""
|
||||
...
|
||||
|
||||
def set_hooks(self, hooks: Union[dict, None]) -> SessionOptions:
|
||||
"""设置回调方法
|
||||
:param hooks: 回调方法
|
||||
:return: 返回当前对象
|
||||
"""
|
||||
...
|
||||
|
||||
@property
|
||||
def params(self) -> dict:
|
||||
"""返回连接参数设置信息"""
|
||||
...
|
||||
|
||||
def set_params(self, params: Union[dict, None]) -> SessionOptions:
|
||||
"""设置查询参数字典
|
||||
:param params: 查询参数字典
|
||||
:return: 返回当前对象
|
||||
"""
|
||||
...
|
||||
|
||||
@property
|
||||
def verify(self) -> bool:
|
||||
"""返回是否验证SSL证书设置"""
|
||||
...
|
||||
|
||||
def set_verify(self, on_off: Union[bool, None]) -> SessionOptions:
|
||||
"""设置是否验证SSL证书
|
||||
:param on_off: 是否验证 SSL 证书
|
||||
:return: 返回当前对象
|
||||
"""
|
||||
...
|
||||
|
||||
@property
|
||||
def cert(self) -> Union[str, tuple]:
|
||||
"""返回SSL证书设置信息"""
|
||||
...
|
||||
|
||||
def set_cert(self, cert: Union[str, Tuple[str, str], None]) -> SessionOptions:
|
||||
"""SSL客户端证书文件的路径(.pem格式),或('cert', 'key')元组
|
||||
:param cert: 证书路径或元组
|
||||
:return: 返回当前对象
|
||||
"""
|
||||
...
|
||||
|
||||
@property
|
||||
def adapters(self) -> list:
|
||||
"""返回适配器设置信息"""
|
||||
...
|
||||
|
||||
def add_adapter(self, url: str, adapter: HTTPAdapter) -> SessionOptions:
|
||||
"""添加适配器
|
||||
:param url: 适配器对应url
|
||||
:param adapter: 适配器对象
|
||||
:return: 返回当前对象
|
||||
"""
|
||||
...
|
||||
|
||||
@property
|
||||
def stream(self) -> bool:
|
||||
"""返回是否使用流式响应内容设置信息"""
|
||||
...
|
||||
|
||||
def set_stream(self, on_off: Union[bool, None]) -> SessionOptions:
|
||||
"""设置是否使用流式响应内容
|
||||
:param on_off: 是否使用流式响应内容
|
||||
:return: 返回当前对象
|
||||
"""
|
||||
...
|
||||
|
||||
@property
|
||||
def trust_env(self) -> bool:
|
||||
"""返回是否信任环境设置信息"""
|
||||
...
|
||||
|
||||
def set_trust_env(self, on_off: Union[bool, None]) -> SessionOptions:
|
||||
"""设置是否信任环境
|
||||
:param on_off: 是否信任环境
|
||||
:return: 返回当前对象
|
||||
"""
|
||||
...
|
||||
|
||||
@property
|
||||
def max_redirects(self) -> int:
|
||||
"""返回最大重定向次数"""
|
||||
...
|
||||
|
||||
def set_max_redirects(self, times: Union[int, None]) -> SessionOptions:
|
||||
"""设置最大重定向次数
|
||||
:param times: 最大重定向次数
|
||||
:return: 返回当前对象
|
||||
"""
|
||||
...
|
||||
|
||||
def _sets(self, arg: str, val: Any) -> None:
|
||||
"""给属性赋值或标记删除
|
||||
:param arg: 属性名称
|
||||
:param val: 参数值
|
||||
:return: None
|
||||
"""
|
||||
...
|
||||
|
||||
def save(self, path: str = None) -> str:
|
||||
"""保存设置到文件
|
||||
:param path: ini文件的路径,传入 'default' 保存到默认ini文件
|
||||
:return: 保存文件的绝对路径
|
||||
"""
|
||||
...
|
||||
|
||||
def save_to_default(self) -> str:
|
||||
"""保存当前配置到默认ini文件"""
|
||||
...
|
||||
|
||||
def as_dict(self) -> dict:
|
||||
"""以字典形式返回本对象"""
|
||||
...
|
||||
|
||||
def make_session(self) -> Tuple[Session, Optional[CaseInsensitiveDict]]:
|
||||
"""根据内在的配置生成Session对象,headers从对象中分离"""
|
||||
...
|
||||
|
||||
def from_session(self, session: Session, headers: CaseInsensitiveDict = None) -> SessionOptions:
|
||||
"""从Session对象中读取配置
|
||||
:param session: Session对象
|
||||
:param headers: headers
|
||||
:return: 当前对象
|
||||
"""
|
||||
...
|
||||
|
||||
|
||||
def session_options_to_dict(options: Union[dict, SessionOptions, None]) -> Union[dict, None]:
|
||||
"""把session配置对象转换为字典
|
||||
:param options: session配置对象或字典
|
||||
:return: 配置字典
|
||||
"""
|
||||
...
|
||||
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,980 @@
|
||||
# -*- coding:utf-8 -*-
|
||||
"""
|
||||
@Author : g1879
|
||||
@Contact : g1879@qq.com
|
||||
@Website : https://DrissionPage.cn
|
||||
@Copyright: (c) 2020 by g1879, Inc. All Rights Reserved.
|
||||
"""
|
||||
from pathlib import Path
|
||||
from typing import Union, Tuple, List, Any, Literal, Optional
|
||||
|
||||
from .._base.base import DrissionElement, BaseElement
|
||||
from .._elements.session_element import SessionElement
|
||||
from .._functions.elements import SessionElementsList, ChromiumElementsList
|
||||
from .._pages.chromium_base import ChromiumBase
|
||||
from .._pages.chromium_frame import ChromiumFrame
|
||||
from .._pages.chromium_page import ChromiumPage
|
||||
from .._pages.chromium_tab import ChromiumTab
|
||||
from .._pages.web_page import WebPage
|
||||
from .._units.clicker import Clicker
|
||||
from .._units.rect import ElementRect
|
||||
from .._units.scroller import ElementScroller
|
||||
from .._units.selector import SelectElement
|
||||
from .._units.setter import ChromiumElementSetter
|
||||
from .._units.states import ShadowRootStates, ElementStates
|
||||
from .._units.waiter import ElementWaiter
|
||||
|
||||
PIC_TYPE = Literal['jpg', 'jpeg', 'png', 'webp', True]
|
||||
|
||||
|
||||
class ChromiumElement(DrissionElement):
|
||||
_tag: Optional[str] = ...
|
||||
owner: ChromiumBase = ...
|
||||
page: Union[ChromiumPage, WebPage] = ...
|
||||
tab: Union[ChromiumPage, ChromiumTab] = ...
|
||||
_node_id: int = ...
|
||||
_obj_id: str = ...
|
||||
_backend_id: int = ...
|
||||
_doc_id: Optional[str] = ...
|
||||
_scroll: Optional[ElementScroller] = ...
|
||||
_clicker: Optional[Clicker] = ...
|
||||
_select: Union[SelectElement, None, False] = ...
|
||||
_wait: Optional[ElementWaiter] = ...
|
||||
_rect: Optional[ElementRect] = ...
|
||||
_set: Optional[ChromiumElementSetter] = ...
|
||||
_states: Optional[ElementStates] = ...
|
||||
_pseudo: Optional[Pseudo] = ...
|
||||
|
||||
def __init__(self,
|
||||
owner: ChromiumBase,
|
||||
node_id: int = None,
|
||||
obj_id: str = None,
|
||||
backend_id: int = None):
|
||||
"""node_id、obj_id和backend_id必须至少传入一个
|
||||
:param owner: 元素所在页面对象
|
||||
:param node_id: cdp中的node id
|
||||
:param obj_id: js中的object id
|
||||
:param backend_id: backend id
|
||||
"""
|
||||
...
|
||||
|
||||
def __call__(self,
|
||||
locator: Union[Tuple[str, str], str],
|
||||
index: int = 1,
|
||||
timeout: float = None) -> ChromiumElement:
|
||||
"""在内部查找元素
|
||||
:param locator: 元素的定位信息,可以是loc元组,或查询字符串
|
||||
:param timeout: 超时时间(秒)
|
||||
:return: ChromiumElement对象或属性、文本
|
||||
"""
|
||||
...
|
||||
|
||||
def __repr__(self) -> str: ...
|
||||
|
||||
def __eq__(self, other: ChromiumElement) -> bool: ...
|
||||
|
||||
@property
|
||||
def tag(self) -> str:
|
||||
"""返回元素tag"""
|
||||
...
|
||||
|
||||
@property
|
||||
def html(self) -> str:
|
||||
"""返回元素outerHTML文本"""
|
||||
...
|
||||
|
||||
@property
|
||||
def inner_html(self) -> str:
|
||||
"""返回元素innerHTML文本"""
|
||||
...
|
||||
|
||||
@property
|
||||
def attrs(self) -> dict:
|
||||
"""返回元素所有attribute属性"""
|
||||
...
|
||||
|
||||
@property
|
||||
def text(self) -> str:
|
||||
"""返回元素内所有文本,文本已格式化"""
|
||||
...
|
||||
|
||||
@property
|
||||
def raw_text(self) -> str:
|
||||
"""返回未格式化处理的元素内文本"""
|
||||
...
|
||||
|
||||
@property
|
||||
def set(self) -> ChromiumElementSetter:
|
||||
"""返回用于设置元素属性的对象"""
|
||||
...
|
||||
|
||||
@property
|
||||
def states(self) -> ElementStates:
|
||||
"""返回用于获取元素状态的对象"""
|
||||
...
|
||||
|
||||
@property
|
||||
def pseudo(self) -> Pseudo:
|
||||
"""返回用于获取伪元素内容的对象"""
|
||||
...
|
||||
|
||||
@property
|
||||
def rect(self) -> ElementRect:
|
||||
"""返回用于获取元素位置的对象"""
|
||||
...
|
||||
|
||||
@property
|
||||
def shadow_root(self) -> Union[None, ShadowRoot]:
|
||||
"""返回当前元素的shadow_root元素对象"""
|
||||
...
|
||||
|
||||
@property
|
||||
def sr(self) -> Union[None, ShadowRoot]:
|
||||
"""返回当前元素的shadow_root元素对象"""
|
||||
...
|
||||
|
||||
@property
|
||||
def scroll(self) -> ElementScroller:
|
||||
"""用于滚动滚动条的对象"""
|
||||
...
|
||||
|
||||
@property
|
||||
def click(self) -> Clicker:
|
||||
"""返回用于点击的对象"""
|
||||
...
|
||||
|
||||
@property
|
||||
def wait(self) -> ElementWaiter:
|
||||
"""返回用于等待的对象"""
|
||||
...
|
||||
|
||||
@property
|
||||
def select(self) -> Union[SelectElement, False]:
|
||||
"""返回专门处理下拉列表的Select类,非<select>元素返回False"""
|
||||
...
|
||||
|
||||
@property
|
||||
def value(self) -> str:
|
||||
"""返回元素property属性的value值"""
|
||||
...
|
||||
|
||||
def parent(self,
|
||||
level_or_loc: Union[tuple, str, int] = 1,
|
||||
index: int = 1,
|
||||
timeout: float = 0) -> ChromiumElement:
|
||||
"""返回上面某一级父元素,可指定层数或用查询语法定位
|
||||
:param level_or_loc: 第几级父元素,1开始,或定位符
|
||||
:param index: 当level_or_loc传入定位符,使用此参数选择第几个结果,1开始
|
||||
:param timeout: 查找超时时间(秒)
|
||||
:return: 上级元素对象
|
||||
"""
|
||||
...
|
||||
|
||||
def child(self,
|
||||
locator: Union[Tuple[str, str], str, int] = '',
|
||||
index: int = 1,
|
||||
timeout: float = None,
|
||||
ele_only: bool = True) -> Union[ChromiumElement, str]:
|
||||
"""返回当前元素的一个符合条件的直接子元素,可用查询语法筛选,可指定返回筛选结果的第几个
|
||||
:param locator: 用于筛选的查询语法
|
||||
:param index: 第几个查询结果,1开始
|
||||
:param timeout: 查找节点的超时时间(秒)
|
||||
:param ele_only: 是否只获取元素,为False时把文本、注释节点也纳入
|
||||
:return: 直接子元素或节点文本
|
||||
"""
|
||||
...
|
||||
|
||||
def prev(self,
|
||||
locator: Union[Tuple[str, str], str, int] = '',
|
||||
index: int = 1,
|
||||
timeout: float = None,
|
||||
ele_only: bool = True) -> Union[ChromiumElement, str]:
|
||||
"""返回当前元素前面一个符合条件的同级元素,可用查询语法筛选,可指定返回筛选结果的第几个
|
||||
:param locator: 用于筛选的查询语法
|
||||
:param index: 前面第几个查询结果,1开始
|
||||
:param timeout: 查找节点的超时时间(秒)
|
||||
:param ele_only: 是否只获取元素,为False时把文本、注释节点也纳入
|
||||
:return: 兄弟元素或节点文本
|
||||
"""
|
||||
...
|
||||
|
||||
def next(self,
|
||||
locator: Union[Tuple[str, str], str, int] = '',
|
||||
index: int = 1,
|
||||
timeout: float = None,
|
||||
ele_only: bool = True) -> Union[ChromiumElement, str]:
|
||||
"""返回当前元素后面一个符合条件的同级元素,可用查询语法筛选,可指定返回筛选结果的第几个
|
||||
:param locator: 用于筛选的查询语法
|
||||
:param index: 第几个查询结果,1开始
|
||||
:param timeout: 查找节点的超时时间(秒)
|
||||
:param ele_only: 是否只获取元素,为False时把文本、注释节点也纳入
|
||||
:return: 兄弟元素或节点文本
|
||||
"""
|
||||
...
|
||||
|
||||
def before(self,
|
||||
locator: Union[Tuple[str, str], str, int] = '',
|
||||
index: int = 1,
|
||||
timeout: float = None,
|
||||
ele_only: bool = True) -> Union[ChromiumElement, str]:
|
||||
"""返回文档中当前元素前面符合条件的一个元素,可用查询语法筛选,可指定返回筛选结果的第几个
|
||||
查找范围不限同级元素,而是整个DOM文档
|
||||
:param locator: 用于筛选的查询语法
|
||||
:param index: 前面第几个查询结果,1开始
|
||||
:param timeout: 查找节点的超时时间(秒)
|
||||
:param ele_only: 是否只获取元素,为False时把文本、注释节点也纳入
|
||||
:return: 本元素前面的某个元素或节点
|
||||
"""
|
||||
...
|
||||
|
||||
def after(self,
|
||||
locator: Union[Tuple[str, str], str, int] = '',
|
||||
index: int = 1,
|
||||
timeout: float = None,
|
||||
ele_only: bool = True) -> Union[ChromiumElement, str]:
|
||||
"""返回文档中此当前元素后面符合条件的一个元素,可用查询语法筛选,可指定返回筛选结果的第几个
|
||||
查找范围不限同级元素,而是整个DOM文档
|
||||
:param locator: 用于筛选的查询语法
|
||||
:param index: 第几个查询结果,1开始
|
||||
:param timeout: 查找节点的超时时间(秒)
|
||||
:param ele_only: 是否只获取元素,为False时把文本、注释节点也纳入
|
||||
:return: 本元素后面的某个元素或节点
|
||||
"""
|
||||
...
|
||||
|
||||
def children(self,
|
||||
locator: Union[Tuple[str, str], str] = '',
|
||||
timeout: float = None,
|
||||
ele_only: bool = True) -> Union[ChromiumElementsList, List[Union[ChromiumElement, str]]]:
|
||||
"""返回当前元素符合条件的直接子元素或节点组成的列表,可用查询语法筛选
|
||||
:param locator: 用于筛选的查询语法
|
||||
:param timeout: 查找节点的超时时间(秒)
|
||||
:param ele_only: 是否只获取元素,为False时把文本、注释节点也纳入
|
||||
:return: 直接子元素或节点文本组成的列表
|
||||
"""
|
||||
...
|
||||
|
||||
def prevs(self,
|
||||
locator: Union[Tuple[str, str], str] = '',
|
||||
timeout: float = None,
|
||||
ele_only: bool = True) -> Union[ChromiumElementsList, List[Union[ChromiumElement, str]]]:
|
||||
"""返回当前元素前面符合条件的同级元素或节点组成的列表,可用查询语法筛选
|
||||
:param locator: 用于筛选的查询语法
|
||||
:param timeout: 查找节点的超时时间(秒)
|
||||
:param ele_only: 是否只获取元素,为False时把文本、注释节点也纳入
|
||||
:return: 兄弟元素或节点文本组成的列表
|
||||
"""
|
||||
...
|
||||
|
||||
def nexts(self,
|
||||
locator: Union[Tuple[str, str], str] = '',
|
||||
timeout: float = None,
|
||||
ele_only: bool = True) -> Union[ChromiumElementsList, List[Union[ChromiumElement, str]]]:
|
||||
"""返回当前元素后面符合条件的同级元素或节点组成的列表,可用查询语法筛选
|
||||
:param locator: 用于筛选的查询语法
|
||||
:param timeout: 查找节点的超时时间(秒)
|
||||
:param ele_only: 是否只获取元素,为False时把文本、注释节点也纳入
|
||||
:return: 兄弟元素或节点文本组成的列表
|
||||
"""
|
||||
...
|
||||
|
||||
def befores(self,
|
||||
locator: Union[Tuple[str, str], str] = '',
|
||||
timeout: float = None,
|
||||
ele_only: bool = True) -> Union[ChromiumElementsList, List[Union[ChromiumElement, str]]]:
|
||||
"""返回文档中当前元素前面符合条件的元素或节点组成的列表,可用查询语法筛选
|
||||
查找范围不限同级元素,而是整个DOM文档
|
||||
:param locator: 用于筛选的查询语法
|
||||
:param timeout: 查找节点的超时时间(秒)
|
||||
:param ele_only: 是否只获取元素,为False时把文本、注释节点也纳入
|
||||
:return: 本元素前面的元素或节点组成的列表
|
||||
"""
|
||||
...
|
||||
|
||||
def afters(self,
|
||||
locator: Union[Tuple[str, str], str] = '',
|
||||
timeout: float = None,
|
||||
ele_only: bool = True) -> Union[ChromiumElementsList, List[Union[ChromiumElement, str]]]:
|
||||
"""返回文档中当前元素后面符合条件的元素或节点组成的列表,可用查询语法筛选
|
||||
查找范围不限同级元素,而是整个DOM文档
|
||||
:param locator: 用于筛选的查询语法
|
||||
:param timeout: 查找节点的超时时间(秒)
|
||||
:param ele_only: 是否只获取元素,为False时把文本、注释节点也纳入
|
||||
:return: 本元素后面的元素或节点组成的列表
|
||||
"""
|
||||
...
|
||||
|
||||
def over(self, timeout: float = None) -> ChromiumElement:
|
||||
"""获取覆盖在本元素上最上层的元素
|
||||
:param timeout: 等待元素出现的超时时间(秒)
|
||||
:return: 元素对象
|
||||
"""
|
||||
...
|
||||
|
||||
def offset(self,
|
||||
locator: Optional[str] = None,
|
||||
x: int = None,
|
||||
y: int = None,
|
||||
timeout: float = None) -> ChromiumElement:
|
||||
"""获取相对本元素左上角左边指定偏移量位置的元素,如果offset_x和offset_y都是None,定位到元素中间点
|
||||
:param locator: 定位符,只支持str,且不支持xpath和css方式
|
||||
:param x: 横坐标偏移量,向右为正
|
||||
:param y: 纵坐标偏移量,向下为正
|
||||
:param timeout: 超时时间(秒),为None使用所在页面设置
|
||||
:return: 元素对象
|
||||
"""
|
||||
...
|
||||
|
||||
def east(self, loc_or_pixel: Union[str, int, None] = None, index: int = 1) -> ChromiumElement:
|
||||
"""获取元素右边某个指定元素
|
||||
:param loc_or_pixel: 定位符,只支持str或int,且不支持xpath和css方式,传入int按像素距离获取
|
||||
:param index: 第几个,从1开始
|
||||
:return: 获取到的元素对象
|
||||
"""
|
||||
...
|
||||
|
||||
def south(self, loc_or_pixel: Union[str, int, None] = None, index: int = 1) -> ChromiumElement:
|
||||
"""获取元素下方某个指定元素
|
||||
:param loc_or_pixel: 定位符,只支持str或int,且不支持xpath和css方式,传入int按像素距离获取
|
||||
:param index: 第几个,从1开始
|
||||
:return: 获取到的元素对象
|
||||
"""
|
||||
...
|
||||
|
||||
def west(self, loc_or_pixel: Union[str, int, None] = None, index: int = 1) -> ChromiumElement:
|
||||
"""获取元素左边某个指定元素
|
||||
:param loc_or_pixel: 定位符,只支持str或int,且不支持xpath和css方式,传入int按像素距离获取
|
||||
:param index: 第几个,从1开始
|
||||
:return: 获取到的元素对象
|
||||
"""
|
||||
...
|
||||
|
||||
def north(self, loc_or_pixel: Union[str, int, None] = None, index: int = 1) -> ChromiumElement:
|
||||
"""获取元素上方某个指定元素
|
||||
:param loc_or_pixel: 定位符,只支持str或int,且不支持xpath和css方式,传入int按像素距离获取
|
||||
:param index: 第几个,从1开始
|
||||
:return: 获取到的元素对象
|
||||
"""
|
||||
...
|
||||
|
||||
def _get_relative_eles(self,
|
||||
mode: str = 'north',
|
||||
locator: Union[int, str] = None,
|
||||
index: int = 1) -> ChromiumElement:
|
||||
"""获取元素下方某个指定元素
|
||||
:param locator: 定位符,只支持str或int,且不支持xpath和css方式
|
||||
:param index: 第几个,从1开始
|
||||
:return: 获取到的元素对象
|
||||
"""
|
||||
...
|
||||
|
||||
def check(self, uncheck: bool = False, by_js: bool = False) -> None:
|
||||
"""选中或取消选中当前元素
|
||||
:param uncheck: 是否取消选中
|
||||
:param by_js: 是否用js执行
|
||||
:return: None
|
||||
"""
|
||||
...
|
||||
|
||||
def attr(self, name: str) -> Union[str, None]:
|
||||
"""返回一个attribute属性值
|
||||
:param name: 属性名
|
||||
:return: 属性值文本,没有该属性返回None
|
||||
"""
|
||||
...
|
||||
|
||||
def remove_attr(self, name: str) -> ChromiumElement:
|
||||
"""删除元素一个attribute属性
|
||||
:param name: 属性名
|
||||
:return: None
|
||||
"""
|
||||
...
|
||||
|
||||
def property(self, name: str) -> Union[str, int, None]:
|
||||
"""获取一个property属性值
|
||||
:param name: 属性名
|
||||
:return: 属性值文本
|
||||
"""
|
||||
...
|
||||
|
||||
def run_js(self, script: str, *args, as_expr: bool = False, timeout: float = None) -> Any:
|
||||
"""对本元素执行javascript代码
|
||||
:param script: js文本,文本中用this表示本元素
|
||||
:param args: 参数,按顺序在js文本中对应arguments[0]、arguments[1]...
|
||||
:param as_expr: 是否作为表达式运行,为True时args无效
|
||||
:param timeout: js超时时间(秒),为None则使用页面timeouts.script设置
|
||||
:return: 运行的结果
|
||||
"""
|
||||
...
|
||||
|
||||
def _run_js(self, script: str, *args, as_expr: bool = False, timeout: float = None) -> Any:
|
||||
"""对本元素执行javascript代码
|
||||
:param script: js文本,文本中用this表示本元素
|
||||
:param args: 参数,按顺序在js文本中对应arguments[0]、arguments[1]...
|
||||
:param as_expr: 是否作为表达式运行,为True时args无效
|
||||
:param timeout: js超时时间(秒),为None则使用页面timeouts.script设置
|
||||
:return: 运行的结果
|
||||
"""
|
||||
...
|
||||
|
||||
def run_async_js(self, script: str, *args, as_expr: bool = False) -> None:
|
||||
"""以异步方式对本元素执行javascript代码
|
||||
:param script: js文本,文本中用this表示本元素
|
||||
:param args: 参数,按顺序在js文本中对应arguments[0]、arguments[1]...
|
||||
:param as_expr: 是否作为表达式运行,为True时args无效
|
||||
:return: None
|
||||
"""
|
||||
...
|
||||
|
||||
def ele(self,
|
||||
locator: Union[Tuple[str, str], str],
|
||||
index: int = 1,
|
||||
timeout: float = None) -> ChromiumElement:
|
||||
"""返回当前元素下级符合条件的一个元素、属性或节点文本
|
||||
:param locator: 元素的定位信息,可以是loc元组,或查询字符串
|
||||
:param index: 获取第几个元素,从1开始,可传入负数获取倒数第几个
|
||||
:param timeout: 查找元素超时时间(秒),默认与元素所在页面等待时间一致
|
||||
:return: ChromiumElement对象或属性、文本
|
||||
"""
|
||||
...
|
||||
|
||||
def eles(self,
|
||||
locator: Union[Tuple[str, str], str],
|
||||
timeout: float = None) -> ChromiumElementsList:
|
||||
"""返回当前元素下级所有符合条件的子元素、属性或节点文本
|
||||
:param locator: 元素的定位信息,可以是loc元组,或查询字符串
|
||||
:param timeout: 查找元素超时时间(秒),默认与元素所在页面等待时间一致
|
||||
:return: ChromiumElement对象或属性、文本组成的列表
|
||||
"""
|
||||
...
|
||||
|
||||
def s_ele(self,
|
||||
locator: Union[Tuple[str, str], str] = None,
|
||||
index: int = 1,
|
||||
timeout: float = None) -> SessionElement:
|
||||
"""查找一个符合条件的元素,以SessionElement形式返回
|
||||
:param locator: 元素的定位信息,可以是loc元组,或查询字符串
|
||||
:param index: 获取第几个,从1开始,可传入负数获取倒数第几个
|
||||
:param timeout: 查找元素超时时间(秒),默认与元素所在页面等待时间一致
|
||||
:return: SessionElement对象或属性、文本
|
||||
"""
|
||||
...
|
||||
|
||||
def s_eles(self,
|
||||
locator: Union[Tuple[str, str], str] = None,
|
||||
timeout: float = None) -> SessionElementsList:
|
||||
"""查找所有符合条件的元素,以SessionElement列表形式返回
|
||||
:param locator: 定位符
|
||||
:param timeout: 查找元素超时时间(秒),默认与元素所在页面等待时间一致
|
||||
:return: SessionElement或属性、文本组成的列表
|
||||
"""
|
||||
...
|
||||
|
||||
def _find_elements(self,
|
||||
locator: Union[Tuple[str, str], str],
|
||||
timeout: float,
|
||||
index: Optional[int] = 1,
|
||||
relative: bool = False,
|
||||
raise_err: bool = False) -> Union[ChromiumElement, ChromiumFrame, ChromiumElementsList]:
|
||||
"""返回当前元素下级符合条件的子元素、属性或节点文本,默认返回第一个
|
||||
:param locator: 元素的定位信息,可以是loc元组,或查询字符串
|
||||
:param timeout: 查找元素超时时间(秒)
|
||||
:param index: 第几个结果,从1开始,可传入负数获取倒数第几个,为None返回所有
|
||||
:param relative: MixTab用的表示是否相对定位的参数
|
||||
:param raise_err: 找不到元素是是否抛出异常,为None时根据全局设置
|
||||
:return: ChromiumElement对象或文本、属性或其组成的列表
|
||||
"""
|
||||
...
|
||||
|
||||
def style(self, style: str, pseudo_ele: str = '') -> str:
|
||||
"""返回元素样式属性值,可获取伪元素属性值
|
||||
:param style: 样式属性名称
|
||||
:param pseudo_ele: 伪元素名称(如有)
|
||||
:return: 样式属性的值
|
||||
"""
|
||||
...
|
||||
|
||||
def src(self, timeout: float = None, base64_to_bytes: bool = True) -> Union[bytes, str, None]:
|
||||
"""返回元素src资源,base64的可转为bytes返回,其它返回str
|
||||
:param timeout: 等待资源加载的超时时间(秒)
|
||||
:param base64_to_bytes: 为True时,如果是base64数据,转换为bytes格式
|
||||
:return: 资源内容
|
||||
"""
|
||||
...
|
||||
|
||||
def save(self,
|
||||
path: [str, bool] = None,
|
||||
name: str = None,
|
||||
timeout: float = None,
|
||||
rename: bool = True) -> str:
|
||||
"""保存图片或其它有src属性的元素的资源
|
||||
:param path: 文件保存路径,为None时保存到当前文件夹
|
||||
:param name: 文件名称,为None时从资源url获取
|
||||
:param timeout: 等待资源加载的超时时间(秒)
|
||||
:param rename: 遇到重名文件时是否自动重命名
|
||||
:return: 返回保存路径
|
||||
"""
|
||||
...
|
||||
|
||||
def get_screenshot(self,
|
||||
path: [str, Path] = None,
|
||||
name: str = None,
|
||||
as_bytes: PIC_TYPE = None,
|
||||
as_base64: PIC_TYPE = None,
|
||||
scroll_to_center: bool = True) -> Union[str, bytes]:
|
||||
"""对当前元素截图,可保存到文件,或以字节方式返回
|
||||
:param path: 文件保存路径
|
||||
:param name: 完整文件名,后缀可选 'jpg','jpeg','png','webp'
|
||||
:param as_bytes: 是否以字节形式返回图片,可选 'jpg','jpeg','png','webp',生效时path参数和as_base64参数无效
|
||||
:param as_base64: 是否以base64字符串形式返回图片,可选 'jpg','jpeg','png','webp',生效时path参数无效
|
||||
:param scroll_to_center: 截图前是否滚动到视口中央
|
||||
:return: 图片完整路径或字节文本
|
||||
"""
|
||||
...
|
||||
|
||||
def input(self, vals: Any, clear: bool = False, by_js: bool = False) -> ChromiumElement:
|
||||
"""输入文本或组合键,也可用于输入文件路径到input元素(路径间用\n间隔)
|
||||
:param vals: 文本值或按键组合
|
||||
:param clear: 输入前是否清空文本框
|
||||
:param by_js: 是否用js方式输入,不能输入组合键
|
||||
:return: None
|
||||
"""
|
||||
...
|
||||
|
||||
def clear(self, by_js: bool = False) -> ChromiumElement:
|
||||
"""清空元素文本
|
||||
:param by_js: 是否用js方式清空,为False则用全选+del模拟输入删除
|
||||
:return: None
|
||||
"""
|
||||
...
|
||||
|
||||
def _input_focus(self) -> None:
|
||||
"""输入前使元素获取焦点"""
|
||||
...
|
||||
|
||||
def focus(self) -> ChromiumElement:
|
||||
"""使元素获取焦点"""
|
||||
...
|
||||
|
||||
def hover(self, offset_x: int = None, offset_y: int = None) -> ChromiumElement:
|
||||
"""鼠标悬停,可接受偏移量,偏移量相对于元素左上角坐标。不传入offset_x和offset_y值时悬停在元素中点
|
||||
:param offset_x: 相对元素左上角坐标的x轴偏移量
|
||||
:param offset_y: 相对元素左上角坐标的y轴偏移量
|
||||
:return: None
|
||||
"""
|
||||
...
|
||||
|
||||
def drag(self, offset_x: int = 0, offset_y: int = 0, duration: float = 0.5) -> ChromiumElement:
|
||||
"""拖拽当前元素到相对位置
|
||||
:param offset_x: x变化值
|
||||
:param offset_y: y变化值
|
||||
:param duration: 拖动用时,传入0即瞬间到达
|
||||
:return: None
|
||||
"""
|
||||
...
|
||||
|
||||
def drag_to(self,
|
||||
ele_or_loc: Union[Tuple[int, int], str, ChromiumElement],
|
||||
duration: float = 0.5) -> ChromiumElement:
|
||||
"""拖拽当前元素,目标为另一个元素或坐标元组(x, y)
|
||||
:param ele_or_loc: 另一个元素或坐标元组,坐标为元素中点的坐标
|
||||
:param duration: 拖动用时,传入0即瞬间到达
|
||||
:return: None
|
||||
"""
|
||||
...
|
||||
|
||||
def _get_obj_id(self, node_id: int = None, backend_id: int = None) -> str: ...
|
||||
|
||||
def _get_node_id(self, obj_id: str = None, backend_id: int = None) -> int: ...
|
||||
|
||||
def _get_backend_id(self, node_id: int) -> int: ...
|
||||
|
||||
def _refresh_id(self) -> None:
|
||||
"""根据backend id刷新其它id"""
|
||||
...
|
||||
|
||||
def _get_ele_path(self, xpath: bool = True) -> str:
|
||||
"""返获取绝对的css路径或xpath路径"""
|
||||
...
|
||||
|
||||
def _set_file_input(self, files: Union[str, list, tuple]) -> None:
|
||||
"""对上传控件写入路径
|
||||
:param files: 文件路径列表或字符串,字符串时多个文件用回车分隔
|
||||
:return: None
|
||||
"""
|
||||
...
|
||||
|
||||
|
||||
class ShadowRoot(BaseElement):
|
||||
owner: ChromiumBase = ...
|
||||
tab: Union[ChromiumPage, ChromiumTab] = ...
|
||||
_obj_id: str = ...
|
||||
_node_id: int = ...
|
||||
_backend_id: int = ...
|
||||
parent_ele: ChromiumElement = ...
|
||||
_states: Optional[ShadowRootStates] = ...
|
||||
|
||||
def __init__(self, parent_ele: ChromiumElement, obj_id: str = None, backend_id: int = None):
|
||||
"""
|
||||
:param parent_ele: shadow root 所在父元素
|
||||
:param obj_id: js中的object id
|
||||
:param backend_id: cdp中的backend id
|
||||
"""
|
||||
...
|
||||
|
||||
def __call__(self,
|
||||
locator: Union[Tuple[str, str], str],
|
||||
index: int = 1,
|
||||
timeout: float = None) -> ChromiumElement:
|
||||
"""在内部查找元素
|
||||
例:ele2 = ele1('@id=ele_id')
|
||||
:param locator: 元素的定位信息,可以是loc元组,或查询字符串
|
||||
:param index: 获取第几个,从1开始,可传入负数获取倒数第几个
|
||||
:param timeout: 超时时间(秒)
|
||||
:return: 元素对象或属性、文本
|
||||
"""
|
||||
...
|
||||
|
||||
def __repr__(self) -> str: ...
|
||||
|
||||
def __eq__(self, other: ShadowRoot) -> bool: ...
|
||||
|
||||
@property
|
||||
def tag(self) -> str:
|
||||
"""返回元素标签名"""
|
||||
...
|
||||
|
||||
@property
|
||||
def html(self) -> str:
|
||||
"""返回outerHTML文本"""
|
||||
...
|
||||
|
||||
@property
|
||||
def inner_html(self) -> str:
|
||||
"""返回内部的html文本"""
|
||||
...
|
||||
|
||||
@property
|
||||
def states(self) -> ShadowRootStates:
|
||||
"""返回用于获取元素状态的对象"""
|
||||
...
|
||||
|
||||
def run_js(self,
|
||||
script: str,
|
||||
*args,
|
||||
as_expr: bool = False,
|
||||
timeout: float = None) -> Any:
|
||||
"""运行javascript代码
|
||||
:param script: js文本
|
||||
:param args: 参数,按顺序在js文本中对应arguments[0]、arguments[1]...
|
||||
:param as_expr: 是否作为表达式运行,为True时args无效
|
||||
:param timeout: js超时时间(秒),为None则使用页面timeouts.script设置
|
||||
:return: 运行的结果
|
||||
"""
|
||||
...
|
||||
|
||||
def _run_js(self,
|
||||
script: str,
|
||||
*args,
|
||||
as_expr: bool = False,
|
||||
timeout: float = None) -> Any:
|
||||
"""运行javascript代码
|
||||
:param script: js文本
|
||||
:param args: 参数,按顺序在js文本中对应arguments[0]、arguments[1]...
|
||||
:param as_expr: 是否作为表达式运行,为True时args无效
|
||||
:param timeout: js超时时间(秒),为None则使用页面timeouts.script设置
|
||||
:return: 运行的结果
|
||||
"""
|
||||
...
|
||||
|
||||
def run_async_js(self,
|
||||
script: str,
|
||||
*args,
|
||||
as_expr: bool = False,
|
||||
timeout: float = None) -> None:
|
||||
"""以异步方式执行js代码
|
||||
:param script: js文本
|
||||
:param args: 参数,按顺序在js文本中对应arguments[0]、arguments[1]...
|
||||
:param as_expr: 是否作为表达式运行,为True时args无效
|
||||
:param timeout: js超时时间(秒),为None则使用页面timeouts.script设置
|
||||
:return: None
|
||||
"""
|
||||
...
|
||||
|
||||
def parent(self,
|
||||
level_or_loc: Union[str, int] = 1,
|
||||
index: int = 1,
|
||||
timeout: float = 0) -> ChromiumElement:
|
||||
"""返回上面某一级父元素,可指定层数或用查询语法定位
|
||||
:param level_or_loc: 第几级父元素,或定位符
|
||||
:param index: 当level_or_loc传入定位符,使用此参数选择第几个结果
|
||||
:param timeout: 查找超时时间(秒)
|
||||
:return: ChromiumElement对象
|
||||
"""
|
||||
...
|
||||
|
||||
def child(self,
|
||||
locator: Union[Tuple[str, str], str] = '',
|
||||
index: int = 1, timeout: float = None) -> ChromiumElement:
|
||||
"""返回直接子元素元素或节点组成的列表,可用查询语法筛选
|
||||
:param locator: 用于筛选的查询语法
|
||||
:param index: 第几个查询结果,1开始
|
||||
:param timeout: 查找超时时间(秒)
|
||||
:return: 直接子元素或节点文本组成的列表
|
||||
"""
|
||||
...
|
||||
|
||||
def next(self,
|
||||
locator: Union[Tuple[str, str], str] = '',
|
||||
index: int = 1, timeout: float = None) -> ChromiumElement:
|
||||
"""返回当前元素后面一个符合条件的同级元素,可用查询语法筛选,可指定返回筛选结果的第几个
|
||||
:param locator: 用于筛选的查询语法
|
||||
:param index: 第几个查询结果,1开始
|
||||
:param timeout: 查找超时时间(秒)
|
||||
:return: ChromiumElement对象
|
||||
"""
|
||||
...
|
||||
|
||||
def before(self,
|
||||
locator: Union[Tuple[str, str], str] = '',
|
||||
index: int = 1, timeout: float = None) -> ChromiumElement:
|
||||
"""返回文档中当前元素前面符合条件的一个元素,可用查询语法筛选,可指定返回筛选结果的第几个
|
||||
查找范围不限同级元素,而是整个DOM文档
|
||||
:param locator: 用于筛选的查询语法
|
||||
:param index: 前面第几个查询结果,1开始
|
||||
:param timeout: 查找超时时间(秒)
|
||||
:return: 本元素前面的某个元素或节点
|
||||
"""
|
||||
...
|
||||
|
||||
def after(self,
|
||||
locator: Union[Tuple[str, str], str] = '',
|
||||
index: int = 1, timeout: float = None) -> ChromiumElement:
|
||||
"""返回文档中此当前元素后面符合条件的一个元素,可用查询语法筛选,可指定返回筛选结果的第几个
|
||||
查找范围不限同级元素,而是整个DOM文档
|
||||
:param locator: 用于筛选的查询语法
|
||||
:param index: 后面第几个查询结果,1开始
|
||||
:param timeout: 查找超时时间(秒)
|
||||
:return: 本元素后面的某个元素或节点
|
||||
"""
|
||||
...
|
||||
|
||||
def children(self, locator: Union[Tuple[str, str], str] = '', timeout: float = None) -> List[ChromiumElement]:
|
||||
"""返回当前元素符合条件的直接子元素或节点组成的列表,可用查询语法筛选
|
||||
:param locator: 用于筛选的查询语法
|
||||
:param timeout: 查找超时时间(秒)
|
||||
:return: 直接子元素或节点文本组成的列表
|
||||
"""
|
||||
...
|
||||
|
||||
def nexts(self, locator: Union[Tuple[str, str], str] = '', timeout: float = None) -> List[ChromiumElement]:
|
||||
"""返回当前元素后面符合条件的同级元素或节点组成的列表,可用查询语法筛选
|
||||
:param locator: 用于筛选的查询语法
|
||||
:param timeout: 查找超时时间(秒)
|
||||
:return: ChromiumElement对象组成的列表
|
||||
"""
|
||||
...
|
||||
|
||||
def befores(self, locator: Union[Tuple[str, str], str] = '', timeout: float = None) -> List[ChromiumElement]:
|
||||
"""返回文档中当前元素前面符合条件的元素或节点组成的列表,可用查询语法筛选
|
||||
查找范围不限同级元素,而是整个DOM文档
|
||||
:param locator: 用于筛选的查询语法
|
||||
:param timeout: 查找超时时间(秒)
|
||||
:return: 本元素前面的元素或节点组成的列表
|
||||
"""
|
||||
...
|
||||
|
||||
def afters(self, locator: Union[Tuple[str, str], str] = '', timeout: float = None) -> List[ChromiumElement]:
|
||||
"""返回文档中当前元素后面符合条件的元素或节点组成的列表,可用查询语法筛选
|
||||
查找范围不限同级元素,而是整个DOM文档
|
||||
:param locator: 用于筛选的查询语法
|
||||
:param timeout: 查找超时时间(秒)
|
||||
:return: 本元素后面的元素或节点组成的列表
|
||||
"""
|
||||
...
|
||||
|
||||
def ele(self,
|
||||
locator: Union[Tuple[str, str], str],
|
||||
index: int = 1,
|
||||
timeout: float = None) -> ChromiumElement:
|
||||
"""返回当前元素下级符合条件的一个元素
|
||||
:param locator: 元素的定位信息,可以是loc元组,或查询字符串
|
||||
:param index: 获取第几个元素,从1开始,可传入负数获取倒数第几个
|
||||
:param timeout: 查找元素超时时间(秒),默认与元素所在页面等待时间一致
|
||||
:return: ChromiumElement对象
|
||||
"""
|
||||
...
|
||||
|
||||
def eles(self,
|
||||
locator: Union[Tuple[str, str], str],
|
||||
timeout: float = None) -> ChromiumElementsList:
|
||||
"""返回当前元素下级所有符合条件的子元素
|
||||
:param locator: 元素的定位信息,可以是loc元组,或查询字符串
|
||||
:param timeout: 查找元素超时时间(秒),默认与元素所在页面等待时间一致
|
||||
:return: ChromiumElement对象组成的列表
|
||||
"""
|
||||
...
|
||||
|
||||
def s_ele(self,
|
||||
locator: Union[Tuple[str, str], str] = None,
|
||||
index: int = 1,
|
||||
timeout: float = None) -> SessionElement:
|
||||
"""查找一个符合条件的元素以SessionElement形式返回,处理复杂页面时效率很高
|
||||
:param locator: 元素的定位信息,可以是loc元组,或查询字符串
|
||||
:param index: 获取第几个,从1开始,可传入负数获取倒数第几个
|
||||
:param timeout: 查找元素超时时间(秒),默认与元素所在页面等待时间一致
|
||||
:return: SessionElement对象或属性、文本
|
||||
"""
|
||||
...
|
||||
|
||||
def s_eles(self, locator: Union[Tuple[str, str], str], timeout: float = None) -> SessionElementsList:
|
||||
"""查找所有符合条件的元素以SessionElement列表形式返回,处理复杂页面时效率很高
|
||||
:param locator: 元素的定位信息,可以是loc元组,或查询字符串
|
||||
:param timeout: 查找元素超时时间(秒),默认与元素所在页面等待时间一致
|
||||
:return: SessionElement对象
|
||||
"""
|
||||
...
|
||||
|
||||
def _find_elements(self,
|
||||
locator: Union[Tuple[str, str], str],
|
||||
timeout: float,
|
||||
index: Optional[int] = 1,
|
||||
relative: bool = False,
|
||||
raise_err: bool = None) -> Union[ChromiumElement, ChromiumFrame, str, ChromiumElementsList]:
|
||||
"""返回当前元素下级符合条件的子元素、属性或节点文本,默认返回第一个
|
||||
:param locator: 元素的定位信息,可以是loc元组,或查询字符串
|
||||
:param timeout: 查找元素超时时间(秒)
|
||||
:param index: 第几个结果,从1开始,可传入负数获取倒数第几个,为None返回所有
|
||||
:param relative: MixTab用的表示是否相对定位的参数
|
||||
:param raise_err: 找不到元素是是否抛出异常,为None时根据全局设置
|
||||
:return: ChromiumElement对象或其组成的列表
|
||||
"""
|
||||
...
|
||||
|
||||
def _get_node_id(self, obj_id: str) -> int: ...
|
||||
|
||||
def _get_obj_id(self, back_id: int) -> str: ...
|
||||
|
||||
def _get_backend_id(self, node_id: int) -> int: ...
|
||||
|
||||
|
||||
def find_in_chromium_ele(ele: ChromiumElement,
|
||||
locator: Union[str, Tuple[str, str]],
|
||||
index: Optional[int] = 1,
|
||||
timeout: float = None,
|
||||
relative: bool = True) -> Union[ChromiumElement, List[ChromiumElement]]:
|
||||
"""在chromium元素中查找
|
||||
:param ele: ChromiumElement对象
|
||||
:param locator: 元素定位元组
|
||||
:param index: 第几个结果,从1开始,可传入负数获取倒数第几个,为None返回所有
|
||||
:param timeout: 查找元素超时时间(秒)
|
||||
:param relative: MixTab用于标记是否相对定位使用
|
||||
:return: 返回ChromiumElement元素或它们组成的列表
|
||||
"""
|
||||
...
|
||||
|
||||
|
||||
def find_by_xpath(ele: ChromiumElement,
|
||||
xpath: str,
|
||||
index: Optional[int],
|
||||
timeout: float,
|
||||
relative: bool = True) -> Union[ChromiumElement, List[ChromiumElement]]:
|
||||
"""执行用xpath在元素中查找元素
|
||||
:param ele: 在此元素中查找
|
||||
:param xpath: 查找语句
|
||||
:param index: 第几个结果,从1开始,可传入负数获取倒数第几个,为None返回所有
|
||||
:param timeout: 超时时间(秒)
|
||||
:param relative: 是否相对定位
|
||||
:return: ChromiumElement或其组成的列表
|
||||
"""
|
||||
...
|
||||
|
||||
|
||||
def find_by_css(ele: ChromiumElement,
|
||||
selector: str,
|
||||
index: Optional[int],
|
||||
timeout: float) -> Union[ChromiumElement, List[ChromiumElement],]:
|
||||
"""执行用css selector在元素中查找元素
|
||||
:param ele: 在此元素中查找
|
||||
:param selector: 查找语句
|
||||
:param index: 第几个结果,从1开始,可传入负数获取倒数第几个,为None返回所有
|
||||
:param timeout: 超时时间(秒)
|
||||
:return: ChromiumElement或其组成的列表
|
||||
"""
|
||||
...
|
||||
|
||||
|
||||
def make_chromium_eles(page: Union[ChromiumBase, ChromiumPage, WebPage, ChromiumTab, ChromiumFrame],
|
||||
_ids: Union[tuple, list, str, int],
|
||||
index: Optional[int] = 1,
|
||||
is_obj_id: bool = True,
|
||||
ele_only: bool = False
|
||||
) -> Union[ChromiumElement, ChromiumFrame, ChromiumElementsList]:
|
||||
"""根据node id或object id生成相应元素对象
|
||||
:param page: ChromiumPage对象
|
||||
:param _ids: 元素的id列表
|
||||
:param index: 获取第几个,为None返回全部
|
||||
:param is_obj_id: 传入的id是obj id还是node id
|
||||
:param ele_only: 是否只返回ele,在页面查找元素时生效
|
||||
:return: 浏览器元素对象或它们组成的列表,生成失败返回False
|
||||
"""
|
||||
...
|
||||
|
||||
|
||||
def make_js_for_find_ele_by_xpath(xpath: str, type_txt: str, node_txt: str) -> str:
|
||||
"""生成用xpath在元素中查找元素的js文本
|
||||
:param xpath: xpath文本
|
||||
:param type_txt: 查找类型
|
||||
:param node_txt: 节点类型
|
||||
:return: js文本
|
||||
"""
|
||||
...
|
||||
|
||||
|
||||
def run_js(page_or_ele: Union[ChromiumBase, ChromiumElement, ShadowRoot],
|
||||
script: str,
|
||||
as_expr: bool,
|
||||
timeout: float,
|
||||
args: tuple = ...) -> Any:
|
||||
"""运行javascript代码
|
||||
:param page_or_ele: 页面对象或元素对象
|
||||
:param script: js文本
|
||||
:param as_expr: 是否作为表达式运行,为True时args无效
|
||||
:param timeout: 超时时间(秒)
|
||||
:param args: 参数,按顺序在js文本中对应arguments[0]、arguments[1]...
|
||||
:return: js执行结果
|
||||
"""
|
||||
...
|
||||
|
||||
|
||||
def parse_js_result(page: ChromiumBase,
|
||||
ele: ChromiumElement,
|
||||
result: dict,
|
||||
end_time: float):
|
||||
"""解析js返回的结果"""
|
||||
...
|
||||
|
||||
|
||||
def convert_argument(arg: Any) -> dict:
|
||||
"""把参数转换成js能够接收的形式"""
|
||||
...
|
||||
|
||||
|
||||
class Pseudo(object):
|
||||
_ele: ChromiumElement = ...
|
||||
|
||||
def __init__(self, ele: ChromiumElement):
|
||||
"""
|
||||
:param ele: ChromiumElement
|
||||
"""
|
||||
...
|
||||
|
||||
@property
|
||||
def before(self) -> str:
|
||||
"""返回当前元素的::before伪元素文本内容"""
|
||||
...
|
||||
|
||||
@property
|
||||
def after(self) -> str:
|
||||
"""返回当前元素的::after伪元素文本内容"""
|
||||
...
|
||||
@@ -0,0 +1,52 @@
|
||||
# -*- coding:utf-8 -*-
|
||||
"""
|
||||
@Author : g1879
|
||||
@Contact : g1879@qq.com
|
||||
@Website : https://DrissionPage.cn
|
||||
@Copyright: (c) 2020 by g1879, Inc. All Rights Reserved.
|
||||
"""
|
||||
from .._functions.settings import Settings
|
||||
from ..errors import ElementNotFoundError
|
||||
|
||||
|
||||
class NoneElement(object):
|
||||
def __init__(self, page=None, method=None, args=None):
|
||||
if method and Settings.raise_when_ele_not_found: # 无传入method时不自动抛出,由调用者处理
|
||||
raise ElementNotFoundError(METHOD=method, ARGS=args)
|
||||
|
||||
if page:
|
||||
self._none_ele_value = page._none_ele_value
|
||||
self._none_ele_return_value = page._none_ele_return_value
|
||||
else:
|
||||
self._none_ele_value = None
|
||||
self._none_ele_return_value = False
|
||||
self.method = method
|
||||
self.args = {} if args is None else args
|
||||
|
||||
def __call__(self, *args, **kwargs):
|
||||
if not self._none_ele_return_value:
|
||||
raise ElementNotFoundError(METHOD=self.method, ARGS=self.args)
|
||||
else:
|
||||
return self
|
||||
|
||||
def __repr__(self):
|
||||
return f'<NoneElement method={self.method}, {", ".join([f"{k}={v}" for k, v in self.args.items()])}>'
|
||||
|
||||
def __getattr__(self, item):
|
||||
if not self._none_ele_return_value:
|
||||
raise ElementNotFoundError(METHOD=self.method, ARGS=self.args)
|
||||
elif item in ('ele', 's_ele', 'parent', 'child', 'next', 'prev', 'before', 'east', 'north', 'south', 'west',
|
||||
'offset', 'over', 'after', 'get_frame', 'shadow_root', 'sr'):
|
||||
return self
|
||||
else:
|
||||
if item in ('size', 'link', 'css_path', 'xpath', 'comments', 'texts', 'tag', 'html', 'inner_html',
|
||||
'attrs', 'text', 'raw_text', 'value', 'attr', 'style', 'src', 'property'):
|
||||
return self._none_ele_value
|
||||
else:
|
||||
raise ElementNotFoundError(METHOD=self.method, ARGS=self.args)
|
||||
|
||||
def __eq__(self, other):
|
||||
return other is None
|
||||
|
||||
def __bool__(self):
|
||||
return False
|
||||
@@ -0,0 +1,38 @@
|
||||
# -*- coding:utf-8 -*-
|
||||
"""
|
||||
@Author : g1879
|
||||
@Contact : g1879@qq.com
|
||||
@Website : https://DrissionPage.cn
|
||||
@Copyright: (c) 2020 by g1879, Inc. All Rights Reserved.
|
||||
"""
|
||||
from typing import Any, Optional
|
||||
|
||||
from .._base.base import BasePage
|
||||
|
||||
|
||||
class NoneElement(object):
|
||||
_none_ele_value: Any = ...
|
||||
_none_ele_return_value: Any = ...
|
||||
method: Optional[str] = ...
|
||||
args: Optional[dict] = ...
|
||||
|
||||
def __init__(self,
|
||||
page: BasePage = None,
|
||||
method: str = None,
|
||||
args: dict = None):
|
||||
"""
|
||||
:param page: 元素所在页面
|
||||
:param method: 查找元素的方法
|
||||
:param args: 查找元素的参数
|
||||
"""
|
||||
...
|
||||
|
||||
def __call__(self, *args, **kwargs) -> NoneElement: ...
|
||||
|
||||
def __repr__(self) -> str: ...
|
||||
|
||||
def __getattr__(self, item: str) -> str: ...
|
||||
|
||||
def __eq__(self, other: Any) -> bool: ...
|
||||
|
||||
def __bool__(self) -> bool: ...
|
||||
@@ -0,0 +1,298 @@
|
||||
# -*- coding:utf-8 -*-
|
||||
"""
|
||||
@Author : g1879
|
||||
@Contact : g1879@qq.com
|
||||
@Website : https://DrissionPage.cn
|
||||
@Copyright: (c) 2020 by g1879, Inc. All Rights Reserved.
|
||||
"""
|
||||
from html import unescape
|
||||
from re import match, sub, DOTALL, search
|
||||
|
||||
from lxml.etree import tostring
|
||||
from lxml.html import HtmlElement, fromstring
|
||||
|
||||
from .none_element import NoneElement
|
||||
from .._base.base import DrissionElement, BasePage, BaseElement
|
||||
from .._functions.elements import SessionElementsList
|
||||
from .._functions.locator import get_loc
|
||||
from .._functions.settings import Settings as _S
|
||||
from .._functions.web import get_ele_txt, make_absolute_link
|
||||
from ..errors import LocatorError
|
||||
|
||||
|
||||
class SessionElement(DrissionElement):
|
||||
|
||||
def __init__(self, ele, owner=None):
|
||||
"""初始化对象
|
||||
:param ele: 被包装的HtmlElement元素
|
||||
:param owner: 元素所在页面对象,如果是从 html 文本生成的元素,则为 None
|
||||
"""
|
||||
super().__init__(owner)
|
||||
self._inner_ele = ele
|
||||
self._type = 'SessionElement'
|
||||
|
||||
def __repr__(self):
|
||||
attrs = [f"{k}='{v}'" for k, v in self.attrs.items()]
|
||||
return f'<SessionElement {self.tag} {" ".join(attrs)}>'
|
||||
|
||||
def __call__(self, locator, index=1, timeout=None):
|
||||
return self.ele(locator, index=index)
|
||||
|
||||
def __eq__(self, other):
|
||||
return self.xpath == getattr(other, 'xpath', None)
|
||||
|
||||
@property
|
||||
def inner_ele(self):
|
||||
return self._inner_ele
|
||||
|
||||
@property
|
||||
def tag(self):
|
||||
return self._inner_ele.tag
|
||||
|
||||
@property
|
||||
def html(self):
|
||||
html = tostring(self._inner_ele, method="html").decode()
|
||||
return unescape(html[:html.rfind('>') + 1]) # tostring()会把跟紧元素的文本节点也带上,因此要去掉
|
||||
|
||||
@property
|
||||
def inner_html(self):
|
||||
r = match(r'<.*?>(.*)</.*?>', self.html, flags=DOTALL)
|
||||
return '' if not r else r.group(1)
|
||||
|
||||
@property
|
||||
def attrs(self):
|
||||
r = {}
|
||||
for attr, val in self.inner_ele.items():
|
||||
r[attr] = val if attr.lower in ('href', 'src') else self.attr(attr)
|
||||
return r
|
||||
|
||||
@property
|
||||
def text(self):
|
||||
return get_ele_txt(self)
|
||||
|
||||
@property
|
||||
def raw_text(self):
|
||||
return str(self._inner_ele.text_content())
|
||||
|
||||
def parent(self, level_or_loc=1, index=1, timeout: float = None):
|
||||
return super().parent(level_or_loc, index)
|
||||
|
||||
def child(self, locator='', index=1, timeout=None, ele_only=True):
|
||||
return super().child(locator, index, timeout, ele_only=ele_only)
|
||||
|
||||
def prev(self, locator='', index=1, timeout=None, ele_only=True):
|
||||
return super().prev(locator, index, timeout, ele_only=ele_only)
|
||||
|
||||
def next(self, locator='', index=1, timeout=None, ele_only=True):
|
||||
return super().next(locator, index, timeout, ele_only=ele_only)
|
||||
|
||||
def before(self, locator='', index=1, timeout=None, ele_only=True):
|
||||
return super().before(locator, index, timeout, ele_only=ele_only)
|
||||
|
||||
def after(self, locator='', index=1, timeout=None, ele_only=True):
|
||||
return super().after(locator, index, timeout, ele_only=ele_only)
|
||||
|
||||
def children(self, locator='', timeout=0, ele_only=True):
|
||||
return SessionElementsList(self.owner, super().children(locator, timeout, ele_only=ele_only))
|
||||
|
||||
def prevs(self, locator='', timeout=None, ele_only=True):
|
||||
return SessionElementsList(self.owner, super().prevs(locator, timeout, ele_only=ele_only))
|
||||
|
||||
def nexts(self, locator='', timeout=None, ele_only=True):
|
||||
return SessionElementsList(self.owner, super().nexts(locator, timeout, ele_only=ele_only))
|
||||
|
||||
def befores(self, locator='', timeout=None, ele_only=True):
|
||||
return SessionElementsList(self.owner, super().befores(locator, timeout, ele_only=ele_only))
|
||||
|
||||
def afters(self, locator='', timeout=None, ele_only=True):
|
||||
return SessionElementsList(self.owner, super().afters(locator, timeout, ele_only=ele_only))
|
||||
|
||||
def attr(self, name):
|
||||
if name == 'href':
|
||||
link = self.inner_ele.get('href')
|
||||
if not link or link.lower().startswith(('javascript:', 'mailto:')):
|
||||
return link
|
||||
else:
|
||||
return make_absolute_link(link, self.owner.url) if self.owner else link
|
||||
|
||||
elif name == 'src':
|
||||
return make_absolute_link(self.inner_ele.get('src'),
|
||||
self.owner.url) if self.owner else self.inner_ele.get('src')
|
||||
|
||||
elif name == 'text':
|
||||
return self.text
|
||||
|
||||
elif name == 'innerText':
|
||||
return self.raw_text
|
||||
|
||||
elif name in ('html', 'outerHTML'):
|
||||
return self.html
|
||||
|
||||
elif name == 'innerHTML':
|
||||
return self.inner_html
|
||||
|
||||
else:
|
||||
return self.inner_ele.get(name.lower())
|
||||
|
||||
def ele(self, locator, index=1, timeout=None):
|
||||
return self._ele(locator, index=index, method='ele()')
|
||||
|
||||
def eles(self, locator, timeout=None):
|
||||
return self._ele(locator, index=None)
|
||||
|
||||
def s_ele(self, locator=None, index=1):
|
||||
return self._ele(locator, index=index, method='s_ele()')
|
||||
|
||||
def s_eles(self, locator):
|
||||
return self._ele(locator, index=None)
|
||||
|
||||
def _find_elements(self, locator, timeout, index=1, relative=False, raise_err=None):
|
||||
return make_session_ele(self, locator, index=index)
|
||||
|
||||
def _get_ele_path(self, xpath=True):
|
||||
if xpath:
|
||||
return self._inner_ele.getroottree().getpath(self._inner_ele)
|
||||
|
||||
path_str = ''
|
||||
ele = self
|
||||
while ele:
|
||||
id_ = ele.attr('id')
|
||||
if id_:
|
||||
path_str = f'>{ele.tag}#{id_}{path_str}'
|
||||
else:
|
||||
path_str = f'>{ele.tag}:nth-child({len(ele.eles("xpath:./preceding-sibling::*")) + 1}){path_str}'
|
||||
ele = ele.parent()
|
||||
|
||||
return path_str[1:]
|
||||
|
||||
|
||||
def make_session_ele(html_or_ele, loc=None, index=1, method=None):
|
||||
# ---------------处理定位符---------------
|
||||
if not loc:
|
||||
if isinstance(html_or_ele, SessionElement):
|
||||
return html_or_ele
|
||||
loc = ('xpath', '.')
|
||||
|
||||
elif isinstance(loc, (str, tuple)):
|
||||
loc = get_loc(loc)
|
||||
|
||||
else:
|
||||
raise LocatorError(ALLOW_VAL=_S._lang.LOC_FORMAT, CURR_VAL=loc)
|
||||
|
||||
# ---------------根据传入对象类型获取页面对象和lxml元素对象---------------
|
||||
the_type = getattr(html_or_ele, '_type', None)
|
||||
# 直接传入html文本
|
||||
if isinstance(html_or_ele, str):
|
||||
page = None
|
||||
html_or_ele = fromstring(html_or_ele)
|
||||
|
||||
# SessionElement
|
||||
elif the_type == 'SessionElement':
|
||||
page = html_or_ele.owner
|
||||
|
||||
loc_str = loc[1]
|
||||
if loc[0] == 'xpath' and loc[1].lstrip().startswith('/'):
|
||||
loc_str = f'.{loc[1]}'
|
||||
html_or_ele = html_or_ele.inner_ele
|
||||
|
||||
# 若css以>开头,表示找元素的直接子元素,要用page以绝对路径才能找到
|
||||
elif loc[0] == 'css selector' and loc[1].lstrip().startswith('>'):
|
||||
loc_str = f'{html_or_ele.css_path}{loc[1]}'
|
||||
if html_or_ele.owner:
|
||||
html_or_ele = fromstring(html_or_ele.owner.html)
|
||||
else: # 接收html文本,无page的情况
|
||||
html_or_ele = fromstring(html_or_ele('xpath:/ancestor::*').html)
|
||||
|
||||
else:
|
||||
html_or_ele = html_or_ele.inner_ele
|
||||
|
||||
loc = loc[0], loc_str
|
||||
|
||||
elif the_type == 'ChromiumElement':
|
||||
loc_str = loc[1]
|
||||
if loc[0] == 'xpath' and loc[1].lstrip().startswith('/'):
|
||||
loc_str = f'.{loc[1]}'
|
||||
elif loc[0] == 'css selector' and loc[1].lstrip().startswith('>'):
|
||||
loc_str = f'{html_or_ele.css_path}{loc[1]}'
|
||||
loc = loc[0], loc_str
|
||||
|
||||
# 获取整个页面html再定位到当前元素,以实现查找上级元素
|
||||
page = html_or_ele.owner
|
||||
xpath = html_or_ele.xpath
|
||||
# ChromiumElement,兼容传入的元素在iframe内的情况
|
||||
if html_or_ele._doc_id is None:
|
||||
doc = html_or_ele._run_js('return this.ownerDocument;')
|
||||
html_or_ele._doc_id = doc['objectId'] if doc else False
|
||||
|
||||
if html_or_ele._doc_id:
|
||||
html = html_or_ele.owner._run_cdp('DOM.getOuterHTML', objectId=html_or_ele._doc_id)['outerHTML']
|
||||
else:
|
||||
html = html_or_ele.owner.html
|
||||
html_or_ele = fromstring(html)
|
||||
html_or_ele = html_or_ele.xpath(xpath)[0]
|
||||
|
||||
elif the_type == 'ChromiumFrame':
|
||||
page = html_or_ele
|
||||
html_or_ele = fromstring(html_or_ele.inner_html)
|
||||
|
||||
# 各种页面对象
|
||||
elif isinstance(html_or_ele, BasePage):
|
||||
page = html_or_ele
|
||||
html = html_or_ele.html
|
||||
if html.startswith('<?xml '):
|
||||
html = sub(r'^<\?xml.*?>', '', html)
|
||||
html_or_ele = fromstring(html)
|
||||
|
||||
# ShadowRoot
|
||||
elif isinstance(html_or_ele, BaseElement):
|
||||
page = html_or_ele.owner
|
||||
html = html_or_ele.html
|
||||
r = search(r'^<shadow_root>[ \n]*?<html>[ \n]*?(.*?)[ \n]*?</html>[ \n]*?</shadow_root>$', html)
|
||||
if r:
|
||||
html = r.group(1)
|
||||
html_or_ele = fromstring(html)
|
||||
|
||||
else:
|
||||
raise ValueError(_S._lang.join(_S._lang.INCORRECT_TYPE_, 'html_or_ele',
|
||||
ALLOW_TYPE=_S._lang.HTML_ELE_TYPE, CURR_VAL=html_or_ele))
|
||||
|
||||
# ---------------执行查找-----------------
|
||||
try:
|
||||
if loc[0] == 'xpath': # 用lxml内置方法获取lxml的元素对象列表
|
||||
eles = html_or_ele.xpath(loc[1])
|
||||
else: # 用css selector获取元素对象列表
|
||||
eles = html_or_ele.cssselect(loc[1])
|
||||
|
||||
if not isinstance(eles, list): # 结果不是列表,如数字
|
||||
return eles
|
||||
|
||||
# 把lxml元素对象包装成SessionElement对象并按需要返回一个或全部
|
||||
if index is None:
|
||||
r = SessionElementsList(owner=page)
|
||||
for e in eles:
|
||||
if e != '\n':
|
||||
r.append(SessionElement(e, page) if isinstance(e, HtmlElement) else e)
|
||||
return r
|
||||
|
||||
else:
|
||||
eles_count = len(eles)
|
||||
if eles_count == 0 or abs(index) > eles_count:
|
||||
return NoneElement(page, method=method, args={'locator': loc, 'index': index})
|
||||
if index < 0:
|
||||
index = eles_count + index + 1
|
||||
|
||||
ele = eles[index - 1]
|
||||
if isinstance(ele, HtmlElement):
|
||||
return SessionElement(ele, page)
|
||||
elif isinstance(ele, str):
|
||||
return ele
|
||||
else:
|
||||
return NoneElement(page, method=method, args={'locator': loc, 'index': index})
|
||||
|
||||
except Exception as e:
|
||||
if 'Invalid expression' in str(e):
|
||||
raise LocatorError(_S._lang.INVALID_XPATH_, loc)
|
||||
elif 'Expected selector' in str(e):
|
||||
raise LocatorError(_S._lang.INVALID_CSS_, loc)
|
||||
|
||||
raise e
|
||||
@@ -0,0 +1,307 @@
|
||||
# -*- coding:utf-8 -*-
|
||||
"""
|
||||
@Author : g1879
|
||||
@Contact : g1879@qq.com
|
||||
@Website : https://DrissionPage.cn
|
||||
@Copyright: (c) 2020 by g1879, Inc. All Rights Reserved.
|
||||
"""
|
||||
from typing import Union, List, Tuple, Optional
|
||||
|
||||
from lxml.html import HtmlElement
|
||||
|
||||
from .._base.base import DrissionElement, BaseElement
|
||||
from .._elements.chromium_element import ChromiumElement
|
||||
from .._functions.elements import SessionElementsList
|
||||
from .._pages.chromium_base import ChromiumBase
|
||||
from .._pages.chromium_frame import ChromiumFrame
|
||||
from .._pages.session_page import SessionPage
|
||||
|
||||
|
||||
class SessionElement(DrissionElement):
|
||||
"""静态元素对象"""
|
||||
|
||||
def __init__(self, ele: HtmlElement, owner: Union[SessionPage, None] = None):
|
||||
self._inner_ele: HtmlElement = ...
|
||||
self.owner: SessionPage = ...
|
||||
self.page: SessionPage = ...
|
||||
|
||||
def __call__(self,
|
||||
locator: Union[Tuple[str, str], str],
|
||||
index: int = 1,
|
||||
timeout: float = None) -> SessionElement:
|
||||
"""在内部查找元素
|
||||
例:ele2 = ele1('@id=ele_id')
|
||||
:param locator: 元素的定位信息,可以是loc元组,或查询字符串
|
||||
:param index: 第几个元素,从1开始,可传入负数获取倒数第几个
|
||||
:param timeout: 不起实际作用
|
||||
:return: SessionElement对象或属性、文本
|
||||
"""
|
||||
...
|
||||
|
||||
def __repr__(self) -> str: ...
|
||||
|
||||
def __eq__(self, other: SessionElement) -> bool: ...
|
||||
|
||||
@property
|
||||
def inner_ele(self) -> HtmlElement: ...
|
||||
|
||||
@property
|
||||
def tag(self) -> str:
|
||||
"""返回元素类型"""
|
||||
...
|
||||
|
||||
@property
|
||||
def html(self) -> str:
|
||||
"""返回outerHTML文本"""
|
||||
...
|
||||
|
||||
@property
|
||||
def inner_html(self) -> str:
|
||||
"""返回元素innerHTML文本"""
|
||||
...
|
||||
|
||||
@property
|
||||
def attrs(self) -> dict:
|
||||
"""返回元素所有属性及值"""
|
||||
...
|
||||
|
||||
@property
|
||||
def text(self) -> str:
|
||||
"""返回元素内文本"""
|
||||
...
|
||||
|
||||
@property
|
||||
def raw_text(self) -> str:
|
||||
"""返回未格式化处理的元素内文本"""
|
||||
...
|
||||
|
||||
def parent(self,
|
||||
level_or_loc: Union[tuple, str, int] = 1,
|
||||
index: int = 1,
|
||||
timeout: float = None) -> SessionElement:
|
||||
"""返回上面某一级父元素,可指定层数或用查询语法定位
|
||||
:param level_or_loc: 第几级父元素,或定位符
|
||||
:param index: 当level_or_loc传入定位符,使用此参数选择第几个结果
|
||||
:param timeout: 此参数不起实际作用
|
||||
:return: 上级元素对象
|
||||
"""
|
||||
...
|
||||
|
||||
def child(self,
|
||||
locator: Union[Tuple[str, str], str, int] = '',
|
||||
index: int = 1,
|
||||
timeout: float = None,
|
||||
ele_only: bool = True) -> Union[SessionElement, str]:
|
||||
"""返回当前元素的一个符合条件的直接子元素,可用查询语法筛选,可指定返回筛选结果的第几个
|
||||
:param locator: 用于筛选的查询语法
|
||||
:param index: 第几个查询结果,1开始
|
||||
:param timeout: 此参数不起实际作用
|
||||
:param ele_only: 是否只获取元素,为False时把文本、注释节点也纳入
|
||||
:return: 直接子元素或节点文本
|
||||
"""
|
||||
...
|
||||
|
||||
def prev(self,
|
||||
locator: Union[Tuple[str, str], str, int] = '',
|
||||
index: int = 1,
|
||||
timeout: float = None,
|
||||
ele_only: bool = True) -> Union[SessionElement, str]:
|
||||
"""返回当前元素前面一个符合条件的同级元素,可用查询语法筛选,可指定返回筛选结果的第几个
|
||||
:param locator: 用于筛选的查询语法
|
||||
:param index: 前面第几个查询结果,1开始
|
||||
:param timeout: 此参数不起实际作用
|
||||
:param ele_only: 是否只获取元素,为False时把文本、注释节点也纳入
|
||||
:return: 同级元素
|
||||
"""
|
||||
...
|
||||
|
||||
def next(self,
|
||||
locator: Union[Tuple[str, str], str, int] = '',
|
||||
index: int = 1,
|
||||
timeout: float = None,
|
||||
ele_only: bool = True) -> Union[SessionElement, str]:
|
||||
"""返回当前元素后面一个符合条件的同级元素,可用查询语法筛选,可指定返回筛选结果的第几个
|
||||
:param locator: 用于筛选的查询语法
|
||||
:param index: 第几个查询结果,1开始
|
||||
:param timeout: 此参数不起实际作用
|
||||
:param ele_only: 是否只获取元素,为False时把文本、注释节点也纳入
|
||||
:return: 同级元素
|
||||
"""
|
||||
...
|
||||
|
||||
def before(self,
|
||||
locator: Union[Tuple[str, str], str, int] = '',
|
||||
index: int = 1,
|
||||
timeout: float = None,
|
||||
ele_only: bool = True) -> Union[SessionElement, str]:
|
||||
"""返回文档中当前元素前面符合条件的一个元素,可用查询语法筛选,可指定返回筛选结果的第几个
|
||||
查找范围不限同级元素,而是整个DOM文档
|
||||
:param locator: 用于筛选的查询语法
|
||||
:param index: 前面第几个查询结果,1开始
|
||||
:param timeout: 此参数不起实际作用
|
||||
:param ele_only: 是否只获取元素,为False时把文本、注释节点也纳入
|
||||
:return: 本元素前面的某个元素或节点
|
||||
"""
|
||||
...
|
||||
|
||||
def after(self,
|
||||
locator: Union[Tuple[str, str], str, int] = '',
|
||||
index: int = 1,
|
||||
timeout: float = None,
|
||||
ele_only: bool = True) -> Union[SessionElement, str]:
|
||||
"""返回文档中此当前元素后面符合条件的一个元素,可用查询语法筛选,可指定返回筛选结果的第几个
|
||||
查找范围不限同级元素,而是整个DOM文档
|
||||
:param locator: 用于筛选的查询语法
|
||||
:param index: 第几个查询结果,1开始
|
||||
:param timeout: 此参数不起实际作用
|
||||
:param ele_only: 是否只获取元素,为False时把文本、注释节点也纳入
|
||||
:return: 本元素后面的某个元素或节点
|
||||
"""
|
||||
...
|
||||
|
||||
def children(self,
|
||||
locator: Union[Tuple[str, str], str] = '',
|
||||
timeout: float = None,
|
||||
ele_only: bool = True) -> Union[SessionElementsList, List[Union[SessionElement, str]]]:
|
||||
"""返回当前元素符合条件的直接子元素或节点组成的列表,可用查询语法筛选
|
||||
:param locator: 用于筛选的查询语法
|
||||
:param timeout: 此参数不起实际作用
|
||||
:param ele_only: 是否只获取元素,为False时把文本、注释节点也纳入
|
||||
:return: 直接子元素或节点文本组成的列表
|
||||
"""
|
||||
...
|
||||
|
||||
def prevs(self,
|
||||
locator: Union[Tuple[str, str], str] = '',
|
||||
timeout: float = None,
|
||||
ele_only: bool = True) -> Union[SessionElementsList, List[Union[SessionElement, str]]]:
|
||||
"""返回当前元素前面符合条件的同级元素或节点组成的列表,可用查询语法筛选
|
||||
:param locator: 用于筛选的查询语法
|
||||
:param timeout: 此参数不起实际作用
|
||||
:param ele_only: 是否只获取元素,为False时把文本、注释节点也纳入
|
||||
:return: 同级元素或节点文本组成的列表
|
||||
"""
|
||||
...
|
||||
|
||||
def nexts(self,
|
||||
locator: Union[Tuple[str, str], str] = '',
|
||||
timeout: float = None,
|
||||
ele_only: bool = True) -> Union[SessionElementsList, List[Union[SessionElement, str]]]:
|
||||
"""返回当前元素后面符合条件的同级元素或节点组成的列表,可用查询语法筛选
|
||||
:param locator: 用于筛选的查询语法
|
||||
:param timeout: 此参数不起实际作用
|
||||
:param ele_only: 是否只获取元素,为False时把文本、注释节点也纳入
|
||||
:return: 同级元素或节点文本组成的列表
|
||||
"""
|
||||
...
|
||||
|
||||
def befores(self,
|
||||
locator: Union[Tuple[str, str], str] = '',
|
||||
timeout: float = None,
|
||||
ele_only: bool = True) -> Union[SessionElementsList, List[Union[SessionElement, str]]]:
|
||||
"""返回文档中当前元素前面符合条件的元素或节点组成的列表,可用查询语法筛选
|
||||
查找范围不限同级元素,而是整个DOM文档
|
||||
:param locator: 用于筛选的查询语法
|
||||
:param timeout: 此参数不起实际作用
|
||||
:param ele_only: 是否只获取元素,为False时把文本、注释节点也纳入
|
||||
:return: 本元素前面的元素或节点组成的列表
|
||||
"""
|
||||
...
|
||||
|
||||
def afters(self,
|
||||
locator: Union[Tuple[str, str], str] = '',
|
||||
timeout: float = None,
|
||||
ele_only: bool = True) -> Union[SessionElementsList, List[Union[SessionElement, str]]]:
|
||||
"""返回文档中当前元素后面符合条件的元素或节点组成的列表,可用查询语法筛选
|
||||
查找范围不限同级元素,而是整个DOM文档
|
||||
:param locator: 用于筛选的查询语法
|
||||
:param timeout: 此参数不起实际作用
|
||||
:param ele_only: 是否只获取元素,为False时把文本、注释节点也纳入
|
||||
:return: 本元素后面的元素或节点组成的列表
|
||||
"""
|
||||
...
|
||||
|
||||
def attr(self, name: str) -> Optional[str]:
|
||||
"""返回attribute属性值
|
||||
:param name: 属性名
|
||||
:return: 属性值文本,没有该属性返回None
|
||||
"""
|
||||
...
|
||||
|
||||
def ele(self,
|
||||
locator: Union[Tuple[str, str], str],
|
||||
index: int = 1,
|
||||
timeout: float = None) -> SessionElement:
|
||||
"""返回当前元素下级符合条件的一个元素、属性或节点文本
|
||||
:param locator: 元素的定位信息,可以是loc元组,或查询字符串
|
||||
:param index: 第几个元素,从1开始,可传入负数获取倒数第几个
|
||||
:param timeout: 不起实际作用
|
||||
:return: SessionElement对象或属性、文本
|
||||
"""
|
||||
...
|
||||
|
||||
def eles(self,
|
||||
locator: Union[Tuple[str, str], str],
|
||||
timeout: float = None) -> SessionElementsList:
|
||||
"""返回当前元素下级所有符合条件的子元素、属性或节点文本
|
||||
:param locator: 元素的定位信息,可以是loc元组,或查询字符串
|
||||
:param timeout: 不起实际作用
|
||||
:return: SessionElement对象或属性、文本组成的列表
|
||||
"""
|
||||
...
|
||||
|
||||
def s_ele(self,
|
||||
locator: Union[Tuple[str, str], str] = None,
|
||||
index: int = 1) -> SessionElement:
|
||||
"""返回当前元素下级符合条件的一个元素、属性或节点文本
|
||||
:param locator: 元素的定位信息,可以是loc元组,或查询字符串
|
||||
:param index: 获取第几个,从1开始,可传入负数获取倒数第几个
|
||||
:return: SessionElement对象或属性、文本
|
||||
"""
|
||||
...
|
||||
|
||||
def s_eles(self, locator: Union[Tuple[str, str], str]) -> SessionElementsList:
|
||||
"""返回当前元素下级所有符合条件的子元素、属性或节点文本
|
||||
:param locator: 元素的定位信息,可以是loc元组,或查询字符串
|
||||
:return: SessionElement对象或属性、文本组成的列表
|
||||
"""
|
||||
...
|
||||
|
||||
def _find_elements(self,
|
||||
locator: Union[Tuple[str, str], str],
|
||||
timeout: float,
|
||||
index: Optional[int] = 1,
|
||||
relative: bool = False,
|
||||
raise_err: bool = None) -> Union[SessionElement, SessionElementsList]:
|
||||
"""返回当前元素下级符合条件的子元素、属性或节点文本
|
||||
:param locator: 元素的定位信息,可以是loc元组,或查询字符串
|
||||
:param timeout: 不起实际作用,用于和父类对应
|
||||
:param index: 第几个结果,从1开始,可传入负数获取倒数第几个,为None返回所有
|
||||
:param relative: MixTab用的表示是否相对定位的参数
|
||||
:param raise_err: 找不到元素是是否抛出异常,为None时根据全局设置
|
||||
:return: SessionElement对象
|
||||
"""
|
||||
...
|
||||
|
||||
def _get_ele_path(self, xpath: bool = True) -> str:
|
||||
"""获取css路径或xpath路径
|
||||
:param xpath: 用xpath还是css
|
||||
:return: css路径或xpath路径
|
||||
"""
|
||||
...
|
||||
|
||||
|
||||
def make_session_ele(html_or_ele: Union[str, SessionElement, SessionPage, ChromiumElement, BaseElement, ChromiumFrame,
|
||||
ChromiumBase],
|
||||
loc: Union[str, Tuple[str, str]] = None,
|
||||
index: Optional[int] = 1,
|
||||
method: Optional[str] = None) -> Union[SessionElement, SessionElementsList, str, float]:
|
||||
"""从接收到的对象或html文本中查找元素,返回SessionElement对象
|
||||
如要直接从html生成SessionElement而不在下级查找,loc输入None即可
|
||||
:param html_or_ele: html文本、BaseParser对象
|
||||
:param loc: 定位元组或字符串,为None时不在下级查找,返回根元素
|
||||
:param index: 获取第几个元素,从1开始,可传入负数获取倒数第几个,None获取所有
|
||||
:param method: 调用此方法的方法
|
||||
:return: 返回SessionElement元素或列表,或属性文本
|
||||
"""
|
||||
...
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,321 @@
|
||||
# -*- coding:utf-8 -*-
|
||||
"""
|
||||
@Author : g1879
|
||||
@Contact : g1879@qq.com
|
||||
@Website : https://DrissionPage.cn
|
||||
@Copyright: (c) 2020 by g1879, Inc. All Rights Reserved.
|
||||
"""
|
||||
from json import load, dump, JSONDecodeError
|
||||
from os import environ
|
||||
from pathlib import Path
|
||||
from shutil import rmtree
|
||||
from subprocess import Popen, DEVNULL
|
||||
from tempfile import gettempdir
|
||||
from time import perf_counter, sleep
|
||||
|
||||
from requests import Session
|
||||
|
||||
from .settings import Settings as _S
|
||||
from .tools import port_is_using
|
||||
from .._configs.options_manage import OptionsManager
|
||||
from ..errors import BrowserConnectError
|
||||
|
||||
|
||||
def connect_browser(option):
|
||||
address = option.address.replace('localhost', '127.0.0.1')
|
||||
if address.startswith('http'):
|
||||
address = address.lstrip('htps:/')
|
||||
browser_path = option.browser_path
|
||||
|
||||
ip, port = address.split(':')
|
||||
using = port_is_using(ip, port)
|
||||
if ip != '127.0.0.1' or using or option.is_existing_only:
|
||||
if test_connect(ip, port):
|
||||
return True
|
||||
elif ip != '127.0.0.1':
|
||||
raise BrowserConnectError(ADDRESS=address)
|
||||
elif using:
|
||||
raise BrowserConnectError(_S._lang.BROWSER_CONNECT_ERR1_, port, port, ADDRESS=address)
|
||||
else: # option.is_existing_only
|
||||
raise BrowserConnectError(_S._lang.BROWSER_CONNECT_ERR2, ADDRESS=address)
|
||||
|
||||
# ----------创建浏览器进程----------
|
||||
args, user_path = get_launch_args(option)
|
||||
if option._new_env:
|
||||
rmtree(user_path, ignore_errors=True)
|
||||
set_prefs(option)
|
||||
set_flags(option)
|
||||
try:
|
||||
_run_browser(port, browser_path, args)
|
||||
|
||||
except FileNotFoundError: # 传入的路径找不到,主动在ini文件、注册表、系统变量中找
|
||||
browser_path = get_chrome_path(option.ini_path)
|
||||
if not browser_path:
|
||||
raise FileNotFoundError(_S._lang.join(_S._lang.BROWSER_EXE_NOT_FOUND))
|
||||
_run_browser(port, browser_path, args)
|
||||
|
||||
if not test_connect(ip, port):
|
||||
raise BrowserConnectError(ADDRESS=address, TIP=_S._lang.BROWSER_CONNECT_ERR_INFO)
|
||||
return False
|
||||
|
||||
|
||||
def get_launch_args(opt):
|
||||
# ----------处理arguments-----------
|
||||
result = set()
|
||||
user_path = False
|
||||
for i in opt.arguments:
|
||||
if i.startswith(('--disable-extensions-except=', '--load-extension=', '--remote-debugging-port=')):
|
||||
continue
|
||||
elif i.startswith('--user-data-dir') and not opt.system_user_path:
|
||||
user_path = f'--user-data-dir={Path(i[16:]).absolute()}'
|
||||
result.add(user_path)
|
||||
continue
|
||||
elif i.startswith('--user-agent='):
|
||||
opt._ua_set = True
|
||||
result.add(i)
|
||||
|
||||
if not user_path and not opt.system_user_path:
|
||||
port = opt.address.split(':')[-1] if opt.address else '0'
|
||||
p = Path(opt.tmp_path) if opt.tmp_path else Path(gettempdir()) / 'DrissionPage'
|
||||
path = p / 'userData' / port
|
||||
path.mkdir(parents=True, exist_ok=True)
|
||||
user_path = path.absolute()
|
||||
opt.set_user_data_path(user_path)
|
||||
result.add(f'--user-data-dir={user_path}')
|
||||
|
||||
result = list(result)
|
||||
|
||||
# ----------处理插件extensions-------------
|
||||
ext = [Path(e) for e in opt.extensions]
|
||||
exts = []
|
||||
if ext:
|
||||
for e in ext:
|
||||
if e.is_file():
|
||||
raise ValueError(_S._lang.join(_S._lang.PLUGIN_NEED_FOLDER, str(e.absolute())))
|
||||
elif not e.exists():
|
||||
raise FileNotFoundError(_S._lang.join(_S._lang.EXT_NOT_FOUND, PATH=e))
|
||||
else:
|
||||
exts.append(str(e.absolute()))
|
||||
ext = ','.join(set(exts))
|
||||
result.append(f'--disable-extensions-except={ext}')
|
||||
result.append(f'--load-extension={ext}')
|
||||
|
||||
return result, user_path
|
||||
|
||||
|
||||
def set_prefs(opt):
|
||||
if not opt.user_data_path or (not opt.preferences and not opt._prefs_to_del):
|
||||
return
|
||||
prefs = opt.preferences
|
||||
del_list = opt._prefs_to_del
|
||||
|
||||
user = 'Default'
|
||||
for arg in opt.arguments:
|
||||
if arg.startswith('--profile-directory'):
|
||||
user = arg.split('=')[-1].strip()
|
||||
break
|
||||
|
||||
prefs_file = Path(opt.user_data_path) / user / 'Preferences'
|
||||
|
||||
if not prefs_file.exists():
|
||||
prefs_file.parent.mkdir(parents=True, exist_ok=True)
|
||||
with open(prefs_file, 'w') as f:
|
||||
f.write('{}')
|
||||
|
||||
with open(prefs_file, "r", encoding='utf-8') as f:
|
||||
try:
|
||||
prefs_dict = load(f)
|
||||
except JSONDecodeError:
|
||||
prefs_dict = {}
|
||||
|
||||
for pref in prefs:
|
||||
value = prefs[pref]
|
||||
pref = pref.split('.')
|
||||
_make_leave_in_dict(prefs_dict, pref, 0, len(pref))
|
||||
_set_value_to_dict(prefs_dict, pref, value)
|
||||
|
||||
for pref in del_list:
|
||||
_remove_arg_from_dict(prefs_dict, pref)
|
||||
|
||||
with open(prefs_file, 'w', encoding='utf-8') as f:
|
||||
dump(prefs_dict, f)
|
||||
|
||||
|
||||
def set_flags(opt):
|
||||
if not opt.user_data_path or (not opt.clear_file_flags and not opt.flags):
|
||||
return
|
||||
|
||||
state_file = Path(opt.user_data_path) / 'Local State'
|
||||
|
||||
if not state_file.exists():
|
||||
state_file.parent.mkdir(parents=True, exist_ok=True)
|
||||
with open(state_file, 'w') as f:
|
||||
f.write('{}')
|
||||
|
||||
with open(state_file, "r", encoding='utf-8') as f:
|
||||
try:
|
||||
states_dict = load(f)
|
||||
except JSONDecodeError:
|
||||
states_dict = {}
|
||||
states_dict.setdefault('browser', {}).setdefault('enabled_labs_experiments', [])
|
||||
flags_list = [] if opt.clear_file_flags else states_dict['browser']['enabled_labs_experiments']
|
||||
flags_dict = {}
|
||||
for i in flags_list:
|
||||
f = str(i).split('@', 1)
|
||||
flags_dict[f[0]] = None if len(f) == 1 else f[1]
|
||||
|
||||
for k, i in opt.flags.items():
|
||||
flags_dict[k] = i
|
||||
|
||||
states_dict['browser']['enabled_labs_experiments'] = [f'{k}@{i}' if i else k for k, i in flags_dict.items()]
|
||||
|
||||
with open(state_file, 'w', encoding='utf-8') as f:
|
||||
dump(states_dict, f)
|
||||
|
||||
|
||||
def test_connect(ip, port):
|
||||
end_time = perf_counter() + _S.browser_connect_timeout
|
||||
s = Session()
|
||||
s.trust_env = False
|
||||
s.keep_alive = False
|
||||
while perf_counter() < end_time:
|
||||
try:
|
||||
r = s.get(f'http://{ip}:{port}/json', timeout=10, headers={'Connection': 'close'})
|
||||
for tab in r.json():
|
||||
if tab['type'] in ('page', 'webview'):
|
||||
r.close()
|
||||
s.close()
|
||||
return True
|
||||
r.close()
|
||||
except Exception:
|
||||
sleep(.2)
|
||||
|
||||
s.close()
|
||||
return False
|
||||
|
||||
|
||||
def _run_browser(port, path: str, args) -> Popen:
|
||||
"""创建浏览器进程
|
||||
:param port: 端口号
|
||||
:param path: 浏览器路径
|
||||
:param args: 启动参数
|
||||
:return: 进程对象
|
||||
"""
|
||||
p = Path(path)
|
||||
p = str(p / 'chrome') if p.is_dir() else str(path)
|
||||
arguments = [p, f'--remote-debugging-port={port}']
|
||||
arguments.extend(args)
|
||||
try:
|
||||
return Popen(arguments, shell=False, stdout=DEVNULL, stderr=DEVNULL)
|
||||
except FileNotFoundError:
|
||||
raise FileNotFoundError(_S._lang.join(_S._lang.BROWSER_NOT_FOUND))
|
||||
|
||||
|
||||
def _make_leave_in_dict(target_dict: dict, src: list, num: int, end: int) -> None:
|
||||
"""把prefs中a.b.c形式的属性转为a['b']['c']形式
|
||||
:param target_dict: 要处理的字典
|
||||
:param src: 属性层级列表[a, b, c]
|
||||
:param num: 当前处理第几个
|
||||
:param end: src长度
|
||||
:return: None
|
||||
"""
|
||||
if num == end:
|
||||
return
|
||||
if src[num] not in target_dict:
|
||||
target_dict[src[num]] = {}
|
||||
num += 1
|
||||
_make_leave_in_dict(target_dict[src[num - 1]], src, num, end)
|
||||
|
||||
|
||||
def _set_value_to_dict(target_dict: dict, src: list, value) -> None:
|
||||
"""把a.b.c形式的属性的值赋值到a['b']['c']形式的字典中
|
||||
:param target_dict: 要处理的字典
|
||||
:param src: 属性层级列表[a, b, c]
|
||||
:param value: 属性值
|
||||
:return: None
|
||||
"""
|
||||
src = "']['".join(src)
|
||||
src = f"target_dict['{src}']=value"
|
||||
exec(src)
|
||||
|
||||
|
||||
def _remove_arg_from_dict(target_dict: dict, arg: str) -> None:
|
||||
"""把a.b.c形式的属性从字典中删除
|
||||
:param target_dict: 要处理的字典
|
||||
:param arg: 层级属性,形式'a.b.c'
|
||||
:return: None
|
||||
"""
|
||||
args = arg.split('.')
|
||||
args = [f"['{i}']" for i in args]
|
||||
src = ''.join(args)
|
||||
src = f"target_dict{src}"
|
||||
try:
|
||||
exec(src)
|
||||
src = ''.join(args[:-1])
|
||||
src = f"target_dict{src}.pop({args[-1][1:-1]})"
|
||||
exec(src)
|
||||
except:
|
||||
pass
|
||||
|
||||
|
||||
def get_chrome_path(ini_path):
|
||||
# -----------从ini文件中获取--------------
|
||||
if ini_path and Path(ini_path).exists():
|
||||
path = OptionsManager(ini_path).chromium_options.get('browser_path', None)
|
||||
if path and Path(path).is_file():
|
||||
return str(path)
|
||||
|
||||
# -----------使用which获取-----------
|
||||
from shutil import which
|
||||
path = (which('chrome') or which('chromium') or which('google-chrome') or which('google-chrome-stable')
|
||||
or which('google-chrome-unstable') or which('google-chrome-beta'))
|
||||
if path:
|
||||
return path
|
||||
|
||||
# -----------从MAC和Linux默认路径获取-----------
|
||||
from platform import system
|
||||
sys = system().lower()
|
||||
if sys in ('macos', 'darwin'):
|
||||
p = '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome'
|
||||
return p if Path(p).exists() else None
|
||||
|
||||
elif sys == 'linux':
|
||||
paths = ('/usr/bin/google-chrome', '/opt/google/chrome/google-chrome',
|
||||
'/user/lib/chromium-browser/chromium-browser')
|
||||
for p in paths:
|
||||
if Path(p).exists():
|
||||
return p
|
||||
return None
|
||||
|
||||
elif sys != 'windows':
|
||||
return None
|
||||
|
||||
# -----------从注册表中获取--------------
|
||||
from winreg import OpenKey, EnumValue, CloseKey, HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE, KEY_READ
|
||||
txt = r'SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\chrome.exe'
|
||||
try:
|
||||
key = OpenKey(HKEY_CURRENT_USER, txt, reserved=0, access=KEY_READ)
|
||||
k = EnumValue(key, 0)
|
||||
CloseKey(key)
|
||||
if k[1]:
|
||||
return k[1]
|
||||
|
||||
except (FileNotFoundError, OSError):
|
||||
try:
|
||||
key = OpenKey(HKEY_LOCAL_MACHINE, txt, reserved=0, access=KEY_READ)
|
||||
k = EnumValue(key, 0)
|
||||
CloseKey(key)
|
||||
if k[1]:
|
||||
return k[1]
|
||||
|
||||
except (FileNotFoundError, OSError):
|
||||
pass
|
||||
|
||||
# -----------从系统变量中获取--------------
|
||||
for path in environ.get('PATH', '').split(';'):
|
||||
path = Path(path) / 'chrome.exe'
|
||||
try:
|
||||
if path.exists():
|
||||
return str(path)
|
||||
except OSError:
|
||||
pass
|
||||
@@ -0,0 +1,60 @@
|
||||
# -*- coding:utf-8 -*-
|
||||
"""
|
||||
@Author : g1879
|
||||
@Contact : g1879@qq.com
|
||||
@Website : https://DrissionPage.cn
|
||||
@Copyright: (c) 2020 by g1879, Inc. All Rights Reserved.
|
||||
"""
|
||||
from typing import Union
|
||||
|
||||
from .._configs.chromium_options import ChromiumOptions
|
||||
|
||||
|
||||
def connect_browser(option: ChromiumOptions) -> bool:
|
||||
"""连接或启动浏览器
|
||||
:param option: ChromiumOptions对象
|
||||
:return: 返回是否接管的浏览器
|
||||
"""
|
||||
...
|
||||
|
||||
|
||||
def get_launch_args(opt: ChromiumOptions) -> list:
|
||||
"""从ChromiumOptions获取命令行启动参数
|
||||
:param opt: ChromiumOptions
|
||||
:return: 启动参数列表
|
||||
"""
|
||||
...
|
||||
|
||||
|
||||
def set_prefs(opt: ChromiumOptions) -> None:
|
||||
"""处理启动配置中的prefs项,目前只能对已存在文件夹配置
|
||||
:param opt: ChromiumOptions
|
||||
:return: None
|
||||
"""
|
||||
...
|
||||
|
||||
|
||||
def set_flags(opt: ChromiumOptions) -> None:
|
||||
"""处理启动配置中的flags项
|
||||
:param opt: ChromiumOptions
|
||||
:return: None
|
||||
"""
|
||||
...
|
||||
|
||||
|
||||
def test_connect(ip: str, port: Union[int, str], timeout: float = 30) -> bool:
|
||||
"""测试浏览器是否可用
|
||||
:param ip: 浏览器ip
|
||||
:param port: 浏览器端口
|
||||
:param timeout: 超时时间(秒)
|
||||
:return: None
|
||||
"""
|
||||
...
|
||||
|
||||
|
||||
def get_chrome_path(ini_path: str) -> Union[str, None]:
|
||||
"""从ini文件或系统变量中获取chrome可执行文件的路径
|
||||
:param ini_path: ini文件路径
|
||||
:return: 文件路径
|
||||
"""
|
||||
...
|
||||
@@ -0,0 +1,18 @@
|
||||
# -*- coding:utf-8 -*-
|
||||
"""
|
||||
@Author : g1879
|
||||
@Contact : g1879@qq.com
|
||||
@Website : https://DrissionPage.cn
|
||||
@Copyright: (c) 2020 by g1879, Inc. All Rights Reserved.
|
||||
"""
|
||||
|
||||
|
||||
class By:
|
||||
ID = 'id'
|
||||
XPATH = 'xpath'
|
||||
LINK_TEXT = 'link text'
|
||||
PARTIAL_LINK_TEXT = 'partial link text'
|
||||
NAME = 'name'
|
||||
TAG_NAME = 'tag name'
|
||||
CLASS_NAME = 'class name'
|
||||
CSS_SELECTOR = 'css selector'
|
||||
@@ -0,0 +1,53 @@
|
||||
# -*- coding:utf-8 -*-
|
||||
"""
|
||||
@Author : g1879
|
||||
@Contact : g1879@qq.com
|
||||
@Website : https://DrissionPage.cn
|
||||
@Copyright: (c) 2020 by g1879, Inc. All Rights Reserved.
|
||||
"""
|
||||
from click import command, option
|
||||
|
||||
from .._functions.tools import configs_to_here as ch
|
||||
from .._configs.chromium_options import ChromiumOptions
|
||||
from .._pages.chromium_page import ChromiumPage
|
||||
|
||||
|
||||
@command()
|
||||
@option("-p", "--set-browser-path", help="设置浏览器路径")
|
||||
@option("-u", "--set-user-path", help="设置用户数据路径")
|
||||
@option("-c", "--configs-to-here", is_flag=True, help="复制默认配置文件到当前路径")
|
||||
@option("-l", "--launch-browser", default=-1, help="启动浏览器,传入端口号,0表示用配置文件中的值")
|
||||
def main(set_browser_path, set_user_path, configs_to_here, launch_browser):
|
||||
if set_browser_path:
|
||||
set_paths(browser_path=set_browser_path)
|
||||
|
||||
if set_user_path:
|
||||
set_paths(user_data_path=set_user_path)
|
||||
|
||||
if configs_to_here:
|
||||
ch()
|
||||
|
||||
if launch_browser >= 0:
|
||||
port = f'127.0.0.1:{launch_browser}' if launch_browser else None
|
||||
ChromiumPage(port)
|
||||
|
||||
|
||||
def set_paths(browser_path=None, user_data_path=None):
|
||||
"""快捷的路径设置函数
|
||||
:param browser_path: 浏览器可执行文件路径
|
||||
:param user_data_path: 用户数据路径
|
||||
:return: None
|
||||
"""
|
||||
co = ChromiumOptions()
|
||||
|
||||
if browser_path is not None:
|
||||
co.set_browser_path(browser_path)
|
||||
|
||||
if user_data_path is not None:
|
||||
co.set_user_data_path(user_data_path)
|
||||
|
||||
co.save()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
@@ -0,0 +1,231 @@
|
||||
# -*- coding:utf-8 -*-
|
||||
"""
|
||||
@Author : g1879
|
||||
@Contact : g1879@qq.com
|
||||
@Website : https://DrissionPage.cn
|
||||
@Copyright: (c) 2020 by g1879, Inc. All Rights Reserved.
|
||||
"""
|
||||
from datetime import datetime
|
||||
from http.cookiejar import Cookie, CookieJar
|
||||
|
||||
from tldextract import TLDExtract
|
||||
|
||||
from .settings import Settings as _S
|
||||
|
||||
|
||||
def cookie_to_dict(cookie):
|
||||
if isinstance(cookie, Cookie):
|
||||
cookie_dict = cookie.__dict__.copy()
|
||||
cookie_dict.pop('rfc2109', None)
|
||||
cookie_dict.pop('_rest', None)
|
||||
return cookie_dict
|
||||
|
||||
elif isinstance(cookie, dict):
|
||||
cookie_dict = cookie
|
||||
|
||||
elif isinstance(cookie, str):
|
||||
cookie_dict = {}
|
||||
for attr in cookie.strip().rstrip(';,').split(',' if ',' in cookie else ';'):
|
||||
attr_val = attr.strip().split('=', 1)
|
||||
if attr_val[0] in ('domain', 'path', 'expires', 'max-age', 'HttpOnly', 'secure', 'expiry', 'name', 'value'):
|
||||
cookie_dict[attr_val[0]] = attr_val[1] if len(attr_val) == 2 else ''
|
||||
else:
|
||||
cookie_dict['name'] = attr_val[0]
|
||||
cookie_dict['value'] = attr_val[1] if len(attr_val) == 2 else ''
|
||||
|
||||
return cookie_dict
|
||||
|
||||
else:
|
||||
raise ValueError(_S._lang.join(_S._lang.INCORRECT_TYPE_, 'cookie',
|
||||
ALLOW_TYPE='Cookie, str, dict', CURR_TYPE=type(cookie)))
|
||||
|
||||
return cookie_dict
|
||||
|
||||
|
||||
def cookies_to_tuple(cookies):
|
||||
if isinstance(cookies, (list, tuple, CookieJar)):
|
||||
cookies = tuple(cookie_to_dict(cookie) for cookie in cookies)
|
||||
|
||||
elif isinstance(cookies, str):
|
||||
c_dict = {}
|
||||
cookies = cookies.rstrip('; ')
|
||||
cookies = cookies.split(';')
|
||||
|
||||
for attr in cookies:
|
||||
attr_val = attr.strip().split('=', 1)
|
||||
c_dict[attr_val[0]] = attr_val[1] if len(attr_val) == 2 else True
|
||||
cookies = _dict_cookies_to_tuple(c_dict)
|
||||
|
||||
elif isinstance(cookies, dict):
|
||||
cookies = _dict_cookies_to_tuple(cookies)
|
||||
|
||||
elif isinstance(cookies, Cookie):
|
||||
cookies = (cookie_to_dict(cookies),)
|
||||
|
||||
else:
|
||||
raise ValueError(_S._lang.join(_S._lang.INCORRECT_TYPE_, 'cookies',
|
||||
ALLOW_TYPE='Cookie, CookieJar, list, tuple, str, dict', CURR_TYPE=type(cookies)))
|
||||
|
||||
return cookies
|
||||
|
||||
|
||||
def set_session_cookies(session, cookies):
|
||||
for cookie in cookies_to_tuple(cookies):
|
||||
if cookie['value'] is None:
|
||||
cookie['value'] = ''
|
||||
|
||||
kwargs = {x: cookie[x] for x in cookie
|
||||
if x.lower() in ('version', 'port', 'domain', 'path', 'secure',
|
||||
'expires', 'discard', 'comment', 'comment_url', 'rest')}
|
||||
|
||||
if 'expiry' in cookie:
|
||||
kwargs['expires'] = cookie['expiry']
|
||||
|
||||
session.cookies.set(cookie['name'], cookie['value'], **kwargs)
|
||||
|
||||
|
||||
def set_browser_cookies(browser, cookies):
|
||||
c = []
|
||||
for cookie in cookies_to_tuple(cookies):
|
||||
if 'domain' not in cookie and 'url' not in cookie:
|
||||
raise ValueError(_S._lang.join(_S._lang.NEED_DOMAIN2, cookie=cookie))
|
||||
c.append(format_cookie(cookie))
|
||||
browser._run_cdp('Storage.setCookies', cookies=c)
|
||||
|
||||
|
||||
def set_tab_cookies(page, cookies):
|
||||
for cookie in cookies_to_tuple(cookies):
|
||||
cookie = format_cookie(cookie)
|
||||
|
||||
if cookie['name'].startswith('__Host-'):
|
||||
if not page.url.startswith('http'):
|
||||
cookie['name'] = cookie['name'].replace('__Host-', '__Secure-', 1)
|
||||
else:
|
||||
cookie['url'] = page.url
|
||||
page._run_cdp_loaded('Network.setCookie', **cookie)
|
||||
continue # 不用设置域名,可退出
|
||||
|
||||
if cookie.get('domain', None):
|
||||
try:
|
||||
page._run_cdp_loaded('Network.setCookie', **cookie)
|
||||
if not is_cookie_in_driver(page, cookie):
|
||||
page.browser.set.cookies(cookie)
|
||||
continue
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
url = page._browser_url
|
||||
if not url.startswith('http'):
|
||||
raise ValueError(_S._lang.join(_S._lang.DOMAIN_NOT_SET, cookie=cookie))
|
||||
ex_url = TLDExtract(suffix_list_urls=["https://publicsuffix.org/list/public_suffix_list.dat",
|
||||
f"file:///{_S.suffixes_list}"]).extract_str(url)
|
||||
d_list = ex_url.subdomain.split('.')
|
||||
d_list.append(f'{ex_url.domain}.{ex_url.suffix}' if ex_url.suffix else ex_url.domain)
|
||||
|
||||
tmp = [d_list[0]]
|
||||
if len(d_list) > 1:
|
||||
for i in d_list[1:]:
|
||||
tmp.append('.')
|
||||
tmp.append(i)
|
||||
|
||||
for i in range(len(tmp)):
|
||||
cookie['domain'] = ''.join(tmp[i:])
|
||||
page._run_cdp_loaded('Network.setCookie', **cookie)
|
||||
if is_cookie_in_driver(page, cookie):
|
||||
break
|
||||
|
||||
|
||||
def is_cookie_in_driver(page, cookie):
|
||||
if 'domain' in cookie:
|
||||
for c in page.cookies(all_domains=True):
|
||||
if cookie['name'] == c['name'] and cookie['value'] == c['value'] and cookie['domain'] == c.get('domain',
|
||||
None):
|
||||
return True
|
||||
else:
|
||||
for c in page.cookies(all_domains=True):
|
||||
if cookie['name'] == c['name'] and cookie['value'] == c['value']:
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
def format_cookie(cookie):
|
||||
if 'expiry' in cookie:
|
||||
cookie['expires'] = int(cookie['expiry'])
|
||||
cookie.pop('expiry')
|
||||
|
||||
if 'expires' in cookie:
|
||||
if not cookie['expires']:
|
||||
cookie.pop('expires')
|
||||
|
||||
elif isinstance(cookie['expires'], str):
|
||||
if cookie['expires'].isdigit():
|
||||
cookie['expires'] = int(cookie['expires'])
|
||||
|
||||
elif cookie['expires'].replace('.', '').isdigit():
|
||||
cookie['expires'] = float(cookie['expires'])
|
||||
|
||||
else:
|
||||
try:
|
||||
cookie['expires'] = datetime.strptime(cookie['expires'], '%a, %d %b %Y %H:%M:%S GMT').timestamp()
|
||||
except ValueError:
|
||||
cookie.pop('expires')
|
||||
|
||||
if cookie['value'] is None:
|
||||
cookie['value'] = ''
|
||||
elif not isinstance(cookie['value'], str):
|
||||
cookie['value'] = str(cookie['value'])
|
||||
|
||||
if cookie['name'].startswith('__Host-'):
|
||||
cookie['path'] = '/'
|
||||
cookie['secure'] = True
|
||||
|
||||
elif cookie['name'].startswith('__Secure-'):
|
||||
cookie['secure'] = True
|
||||
|
||||
if 'sameSite' in cookie:
|
||||
sameSite = cookie['sameSite']
|
||||
if sameSite in (None, False) or sameSite not in ('None', 'Lax', 'Strict', 'no_restriction'):
|
||||
cookie.pop('sameSite')
|
||||
|
||||
if 'priority' in cookie:
|
||||
priority = cookie['priority']
|
||||
if priority in (None, False):
|
||||
cookie.pop('priority')
|
||||
elif priority not in ('Low', 'Medium', 'High'):
|
||||
raise ValueError(_S._lang.join(_S._lang.INCORRECT_VAL_, 'priority', ALLOW_VAL='"Low", "Medium", "High"',
|
||||
CURR_VAL=priority, cookie=cookie))
|
||||
|
||||
if 'sourceScheme' in cookie:
|
||||
sourceScheme = cookie['sourceScheme']
|
||||
if sourceScheme in (None, False):
|
||||
cookie.pop('sourceScheme')
|
||||
elif sourceScheme not in ('Unset', 'NonSecure', 'Secure'):
|
||||
raise ValueError(_S._lang.join(_S._lang.INCORRECT_VAL_, 'sourceScheme',
|
||||
ALLOW_VAL='"Unset", "NonSecure", "Secure"', CURR_VAL=sourceScheme,
|
||||
cookie=cookie))
|
||||
|
||||
return cookie
|
||||
|
||||
|
||||
class CookiesList(list):
|
||||
def as_dict(self):
|
||||
return {c['name']: c['value'] for c in self}
|
||||
|
||||
def as_str(self):
|
||||
return '; '.join([f'{c["name"]}={c["value"]}' for c in self])
|
||||
|
||||
def as_json(self):
|
||||
from json import dumps
|
||||
return dumps(self)
|
||||
|
||||
|
||||
def _dict_cookies_to_tuple(cookies: dict):
|
||||
"""把dict形式的cookies转换为tuple形式
|
||||
:param cookies: 单个或多个cookies,单个时包含 'name' 和 'value'
|
||||
:return: 多个dict格式cookies组成的列表
|
||||
"""
|
||||
if 'name' in cookies and 'value' in cookies: # 单个cookie
|
||||
return (cookies,)
|
||||
keys = ('domain', 'path', 'expires', 'max-age', 'HttpOnly', 'secure', 'expiry')
|
||||
template = {k: v for k, v in cookies.items() if k in keys}
|
||||
return tuple(dict(**{'name': k, 'value': v}, **template) for k, v in cookies.items() if k not in keys)
|
||||
@@ -0,0 +1,94 @@
|
||||
# -*- coding:utf-8 -*-
|
||||
"""
|
||||
@Author : g1879
|
||||
@Contact : g1879@qq.com
|
||||
@Website : https://DrissionPage.cn
|
||||
@Copyright: (c) 2020 by g1879, Inc. All Rights Reserved.
|
||||
"""
|
||||
from http.cookiejar import Cookie
|
||||
from typing import Union
|
||||
|
||||
from requests import Session
|
||||
from requests.cookies import RequestsCookieJar
|
||||
|
||||
from .._base.chromium import Chromium
|
||||
from .._pages.chromium_base import ChromiumBase
|
||||
|
||||
|
||||
def cookie_to_dict(cookie: Union[Cookie, str, dict]) -> dict:
|
||||
"""把Cookie对象转为dict格式
|
||||
:param cookie: Cookie对象、字符串或字典
|
||||
:return: cookie字典
|
||||
"""
|
||||
...
|
||||
|
||||
|
||||
def cookies_to_tuple(cookies: Union[RequestsCookieJar, list, tuple, str, dict, Cookie]) -> tuple:
|
||||
"""把cookies转为tuple格式
|
||||
:param cookies: cookies信息,可为CookieJar, list, tuple, str, dict
|
||||
:return: 返回tuple形式的cookies
|
||||
"""
|
||||
...
|
||||
|
||||
|
||||
def set_session_cookies(session: Session,
|
||||
cookies: Union[RequestsCookieJar, list, tuple, str, dict]) -> None:
|
||||
"""设置Session对象的cookies
|
||||
:param session: Session对象
|
||||
:param cookies: cookies信息
|
||||
:return: None
|
||||
"""
|
||||
...
|
||||
|
||||
|
||||
def set_browser_cookies(browser: Chromium,
|
||||
cookies: Union[RequestsCookieJar, list, tuple, str, dict]) -> None:
|
||||
"""设置cookies值
|
||||
:param browser: 页面对象
|
||||
:param cookies: cookies信息
|
||||
:return: None
|
||||
"""
|
||||
...
|
||||
|
||||
|
||||
def set_tab_cookies(page: ChromiumBase,
|
||||
cookies: Union[RequestsCookieJar, list, tuple, str, dict]) -> None:
|
||||
"""设置cookies值
|
||||
:param page: 页面对象
|
||||
:param cookies: cookies信息
|
||||
:return: None
|
||||
"""
|
||||
...
|
||||
|
||||
|
||||
def is_cookie_in_driver(page: ChromiumBase, cookie: dict) -> bool:
|
||||
"""查询cookie是否在浏览器内
|
||||
:param page: BasePage对象
|
||||
:param cookie: dict格式cookie
|
||||
:return: bool
|
||||
"""
|
||||
...
|
||||
|
||||
|
||||
def format_cookie(cookie: dict) -> dict:
|
||||
"""设置cookie为可用格式
|
||||
:param cookie: dict格式cookie
|
||||
:return: 格式化后的cookie字典
|
||||
"""
|
||||
...
|
||||
|
||||
|
||||
class CookiesList(list):
|
||||
def as_dict(self) -> dict:
|
||||
"""以dict格式返回,只包含name和value字段"""
|
||||
...
|
||||
|
||||
def as_str(self) -> str:
|
||||
"""以str格式返回,只包含name和value字段"""
|
||||
...
|
||||
|
||||
def as_json(self) -> str:
|
||||
"""以json格式返回"""
|
||||
...
|
||||
|
||||
def __next__(self) -> dict: ...
|
||||
@@ -0,0 +1,460 @@
|
||||
# -*- coding:utf-8 -*-
|
||||
"""
|
||||
@Author : g1879
|
||||
@Contact : g1879@qq.com
|
||||
@Website : https://DrissionPage.cn
|
||||
@Copyright: (c) 2020 by g1879, Inc. All Rights Reserved.
|
||||
"""
|
||||
from time import perf_counter, sleep
|
||||
|
||||
from .locator import is_str_loc, is_selenium_loc
|
||||
from .._functions.settings import Settings as _S
|
||||
from .._elements.none_element import NoneElement
|
||||
from ..errors import LocatorError
|
||||
|
||||
|
||||
class SessionElementsList(list):
|
||||
def __init__(self, owner=None, *args):
|
||||
super().__init__(*args)
|
||||
self._owner = owner
|
||||
|
||||
def __getitem__(self, item):
|
||||
cls = type(self)
|
||||
if isinstance(item, slice):
|
||||
return cls(self._owner, super().__getitem__(item))
|
||||
elif isinstance(item, int):
|
||||
return super().__getitem__(item)
|
||||
else:
|
||||
raise ValueError(_S._lang.join(_S._lang.INDEX_FORMAT, CURR_VAL=item))
|
||||
|
||||
@property
|
||||
def get(self):
|
||||
return Getter(self)
|
||||
|
||||
@property
|
||||
def filter(self):
|
||||
return SessionFilter(self)
|
||||
|
||||
@property
|
||||
def filter_one(self):
|
||||
return SessionFilterOne(self)
|
||||
|
||||
|
||||
class ChromiumElementsList(SessionElementsList):
|
||||
|
||||
@property
|
||||
def filter(self):
|
||||
return ChromiumFilter(self)
|
||||
|
||||
@property
|
||||
def filter_one(self):
|
||||
return ChromiumFilterOne(self)
|
||||
|
||||
def search(self, displayed=None, checked=None, selected=None, enabled=None, clickable=None,
|
||||
have_rect=None, have_text=None, tag=None):
|
||||
return _search(self, displayed=displayed, checked=checked, selected=selected, enabled=enabled,
|
||||
clickable=clickable, have_rect=have_rect, have_text=have_text, tag=tag)
|
||||
|
||||
def search_one(self, index=1, displayed=None, checked=None, selected=None, enabled=None, clickable=None,
|
||||
have_rect=None, have_text=None, tag=None):
|
||||
return _search_one(self, index=index, displayed=displayed, checked=checked, selected=selected,
|
||||
enabled=enabled, clickable=clickable, have_rect=have_rect, have_text=have_text, tag=tag)
|
||||
|
||||
|
||||
class SessionFilterOne(object):
|
||||
def __init__(self, _list):
|
||||
self._list = _list
|
||||
self._index = 1
|
||||
|
||||
def __call__(self, index=1):
|
||||
self._index = index
|
||||
return self
|
||||
|
||||
def tag(self, name, equal=True):
|
||||
num = 0
|
||||
name = name.lower()
|
||||
if equal:
|
||||
for i in self._list:
|
||||
if not isinstance(i, str) and i.tag == name:
|
||||
num += 1
|
||||
if self._index == num:
|
||||
return i
|
||||
else:
|
||||
for i in self._list:
|
||||
if not isinstance(i, str) and i.tag != name:
|
||||
num += 1
|
||||
if self._index == num:
|
||||
return i
|
||||
return NoneElement(self._list._owner, 'tag()', args={'name': name, 'equal': equal, 'index': self._index})
|
||||
|
||||
def attr(self, name, value, equal=True):
|
||||
return self._get_attr(name, value, 'attr', equal=equal)
|
||||
|
||||
def text(self, text, fuzzy=True, contain=True):
|
||||
num = 0
|
||||
if contain:
|
||||
for i in self._list:
|
||||
t = i if isinstance(i, str) else i.raw_text
|
||||
if (fuzzy and text in t) or (not fuzzy and text == t):
|
||||
num += 1
|
||||
if self._index == num:
|
||||
return i
|
||||
else:
|
||||
for i in self._list:
|
||||
t = i if isinstance(i, str) else i.raw_text
|
||||
if (fuzzy and text not in t) or (not fuzzy and text != t):
|
||||
num += 1
|
||||
if self._index == num:
|
||||
return i
|
||||
return NoneElement(self._list._owner, 'text()',
|
||||
args={'text': text, 'fuzzy': fuzzy, 'contain': contain, 'index': self._index})
|
||||
|
||||
def _get_attr(self, name, value, method, equal=True):
|
||||
num = 0
|
||||
if equal:
|
||||
for i in self._list:
|
||||
if not isinstance(i, str) and getattr(i, method)(name) == value:
|
||||
num += 1
|
||||
if self._index == num:
|
||||
return i
|
||||
else:
|
||||
for i in self._list:
|
||||
if not isinstance(i, str) and getattr(i, method)(name) != value:
|
||||
num += 1
|
||||
if self._index == num:
|
||||
return i
|
||||
return NoneElement(self._list._owner, f'{method}()',
|
||||
args={'name': name, 'value': value, 'equal': equal, 'index': self._index})
|
||||
|
||||
|
||||
class SessionFilter(SessionFilterOne):
|
||||
|
||||
def __iter__(self):
|
||||
return iter(self._list)
|
||||
|
||||
def __next__(self):
|
||||
return next(self._list)
|
||||
|
||||
def __len__(self):
|
||||
return len(self._list)
|
||||
|
||||
def __getitem__(self, item):
|
||||
return self._list[item]
|
||||
|
||||
@property
|
||||
def get(self):
|
||||
return self._list.get
|
||||
|
||||
def tag(self, name, equal=True):
|
||||
self._list = _tag_all(self._list, SessionElementsList(owner=self._list._owner), name=name, equal=equal)
|
||||
return self
|
||||
|
||||
def text(self, text, fuzzy=True, contain=True):
|
||||
self._list = _text_all(self._list, SessionElementsList(owner=self._list._owner),
|
||||
text=text, fuzzy=fuzzy, contain=contain)
|
||||
return self
|
||||
|
||||
def _get_attr(self, name, value, method, equal=True):
|
||||
self._list = _attr_all(self._list, SessionElementsList(owner=self._list._owner),
|
||||
name=name, value=value, method=method, equal=equal)
|
||||
return self
|
||||
|
||||
|
||||
class ChromiumFilterOne(SessionFilterOne):
|
||||
|
||||
def displayed(self, equal=True):
|
||||
return self._any_state('is_displayed', equal=equal)
|
||||
|
||||
def checked(self, equal=True):
|
||||
return self._any_state('is_checked', equal=equal)
|
||||
|
||||
def selected(self, equal=True):
|
||||
return self._any_state('is_selected', equal=equal)
|
||||
|
||||
def enabled(self, equal=True):
|
||||
return self._any_state('is_enabled', equal=equal)
|
||||
|
||||
def clickable(self, equal=True):
|
||||
return self._any_state('is_clickable', equal=equal)
|
||||
|
||||
def have_rect(self, equal=True):
|
||||
return self._any_state('has_rect', equal=equal)
|
||||
|
||||
def style(self, name, value, equal=True):
|
||||
return self._get_attr(name, value, 'style', equal=equal)
|
||||
|
||||
def property(self, name, value, equal=True):
|
||||
return self._get_attr(name, value, 'property', equal=equal)
|
||||
|
||||
def _any_state(self, name, equal=True):
|
||||
num = 0
|
||||
if equal:
|
||||
for i in self._list:
|
||||
if not isinstance(i, str) and getattr(i.states, name):
|
||||
num += 1
|
||||
if self._index == num:
|
||||
return i
|
||||
else:
|
||||
for i in self._list:
|
||||
if not isinstance(i, str) and not getattr(i.states, name):
|
||||
num += 1
|
||||
if self._index == num:
|
||||
return i
|
||||
return NoneElement(self._list._owner, f'{name}()', args={'equal': equal, 'index': self._index})
|
||||
|
||||
|
||||
class ChromiumFilter(ChromiumFilterOne):
|
||||
|
||||
def __iter__(self):
|
||||
return iter(self._list)
|
||||
|
||||
def __next__(self):
|
||||
return next(self._list)
|
||||
|
||||
def __len__(self):
|
||||
return len(self._list)
|
||||
|
||||
def __getitem__(self, item):
|
||||
return self._list[item]
|
||||
|
||||
@property
|
||||
def get(self):
|
||||
return self._list.get
|
||||
|
||||
def search_one(self, index=1, displayed=None, checked=None, selected=None, enabled=None, clickable=None,
|
||||
have_rect=None, have_text=None, tag=None):
|
||||
return _search_one(self._list, index=index, displayed=displayed, checked=checked, selected=selected,
|
||||
enabled=enabled, clickable=clickable, have_rect=have_rect, have_text=have_text, tag=tag)
|
||||
|
||||
def search(self, displayed=None, checked=None, selected=None, enabled=None, clickable=None,
|
||||
have_rect=None, have_text=None, tag=None):
|
||||
return _search(self._list, displayed=displayed, checked=checked, selected=selected, enabled=enabled,
|
||||
clickable=clickable, have_rect=have_rect, have_text=have_text, tag=tag)
|
||||
|
||||
def tag(self, name, equal=True):
|
||||
self._list = _tag_all(self._list, ChromiumElementsList(owner=self._list._owner), name=name, equal=equal)
|
||||
return self
|
||||
|
||||
def text(self, text, fuzzy=True, contain=True):
|
||||
self._list = _text_all(self._list, ChromiumElementsList(owner=self._list._owner),
|
||||
text=text, fuzzy=fuzzy, contain=contain)
|
||||
return self
|
||||
|
||||
def _get_attr(self, name, value, method, equal=True):
|
||||
self._list = _attr_all(self._list, ChromiumElementsList(owner=self._list._owner),
|
||||
name=name, value=value, method=method, equal=equal)
|
||||
return self
|
||||
|
||||
def _any_state(self, name, equal=True):
|
||||
r = ChromiumElementsList(owner=self._list._owner)
|
||||
if equal:
|
||||
for i in self._list:
|
||||
if not isinstance(i, str) and getattr(i.states, name):
|
||||
r.append(i)
|
||||
else:
|
||||
for i in self._list:
|
||||
if not isinstance(i, str) and not getattr(i.states, name):
|
||||
r.append(i)
|
||||
self._list = r
|
||||
return self
|
||||
|
||||
|
||||
class Getter(object):
|
||||
def __init__(self, _list):
|
||||
self._list = _list
|
||||
|
||||
def links(self):
|
||||
return [e.link for e in self._list if not isinstance(e, str)]
|
||||
|
||||
def texts(self):
|
||||
return [e if isinstance(e, str) else e.text for e in self._list]
|
||||
|
||||
def attrs(self, name):
|
||||
return [e.attr(name) for e in self._list if not isinstance(e, str)]
|
||||
|
||||
|
||||
def get_eles(locators, owner, any_one=False, first_ele=True, timeout=10):
|
||||
if is_selenium_loc(locators):
|
||||
locators = (locators,)
|
||||
res = {loc: None for loc in locators}
|
||||
|
||||
if timeout == 0:
|
||||
for loc in locators:
|
||||
ele = owner._ele(loc, timeout=0, raise_err=False, index=1 if first_ele else None, method='find()')
|
||||
res[loc] = ele
|
||||
if ele and any_one:
|
||||
return res
|
||||
return res
|
||||
|
||||
end_time = perf_counter() + timeout
|
||||
while perf_counter() <= end_time:
|
||||
for loc in locators:
|
||||
if res[loc]:
|
||||
continue
|
||||
ele = owner._ele(loc, timeout=0, raise_err=False, index=1 if first_ele else None, method='find()')
|
||||
res[loc] = ele
|
||||
if ele and any_one:
|
||||
return res
|
||||
if all(res.values()):
|
||||
return res
|
||||
sleep(.05)
|
||||
|
||||
return res
|
||||
|
||||
|
||||
def get_frame(owner, loc_ind_ele, timeout=None):
|
||||
if isinstance(loc_ind_ele, str):
|
||||
if is_str_loc(loc_ind_ele):
|
||||
xpath = loc_ind_ele
|
||||
else:
|
||||
xpath = f'xpath://*[(name()="iframe" or name()="frame") and (@name="{loc_ind_ele}" or @id="{loc_ind_ele}")]'
|
||||
ele = owner._ele(xpath, timeout=timeout)
|
||||
if ele and ele._type != 'ChromiumFrame':
|
||||
raise LocatorError(_S._lang.LOC_NOT_FOR_FRAME, LOCATOR=loc_ind_ele)
|
||||
r = ele
|
||||
|
||||
elif isinstance(loc_ind_ele, tuple):
|
||||
ele = owner._ele(loc_ind_ele, timeout=timeout)
|
||||
if ele and ele._type != 'ChromiumFrame':
|
||||
raise LocatorError(_S._lang.LOC_NOT_FOR_FRAME, LOCATOR=loc_ind_ele)
|
||||
r = ele
|
||||
|
||||
elif isinstance(loc_ind_ele, int):
|
||||
ele = owner._ele('@|tag():iframe@|tag():frame', timeout=timeout, index=loc_ind_ele)
|
||||
if ele and ele._type != 'ChromiumFrame':
|
||||
raise LocatorError(_S._lang.LOC_NOT_FOR_FRAME, LOCATOR=loc_ind_ele)
|
||||
r = ele
|
||||
|
||||
elif getattr(loc_ind_ele, '_type', None) == 'ChromiumFrame':
|
||||
r = loc_ind_ele
|
||||
|
||||
else:
|
||||
raise ValueError(_S._lang.join(_S._lang.INCORRECT_VAL_, 'loc_ind_ele',
|
||||
ALLOW_VAL=_S._lang.FRAME_LOC_FORMAT, CURR_VAL=loc_ind_ele))
|
||||
|
||||
if isinstance(r, NoneElement):
|
||||
r.method = 'get_frame()'
|
||||
r.args = {'loc_ind_ele': loc_ind_ele}
|
||||
return r
|
||||
|
||||
|
||||
def _attr_all(src_list, aim_list, name, value, method, equal=True):
|
||||
if equal:
|
||||
for i in src_list:
|
||||
if not isinstance(i, str) and getattr(i, method)(name) == value:
|
||||
aim_list.append(i)
|
||||
else:
|
||||
for i in src_list:
|
||||
if not isinstance(i, str) and getattr(i, method)(name) != value:
|
||||
aim_list.append(i)
|
||||
return aim_list
|
||||
|
||||
|
||||
def _tag_all(src_list, aim_list, name, equal=True):
|
||||
name = name.lower()
|
||||
if equal:
|
||||
for i in src_list:
|
||||
if not isinstance(i, str) and i.tag == name:
|
||||
aim_list.append(i)
|
||||
else:
|
||||
for i in src_list:
|
||||
if not isinstance(i, str) and i.tag != name:
|
||||
aim_list.append(i)
|
||||
return aim_list
|
||||
|
||||
|
||||
def _text_all(src_list, aim_list, text, fuzzy=True, contain=True):
|
||||
"""以是否含有指定文本为条件筛选元素
|
||||
:param text: 用于匹配的文本
|
||||
:param fuzzy: 是否模糊匹配
|
||||
:param contain: 是否包含该字符串,False表示不包含
|
||||
:return: 筛选结果
|
||||
"""
|
||||
if contain:
|
||||
for i in src_list:
|
||||
t = i if isinstance(i, str) else i.raw_text
|
||||
if (fuzzy and text in t) or (not fuzzy and text == t):
|
||||
aim_list.append(i)
|
||||
else:
|
||||
for i in src_list:
|
||||
t = i if isinstance(i, str) else i.raw_text
|
||||
if (fuzzy and text not in t) or (not fuzzy and text != t):
|
||||
aim_list.append(i)
|
||||
return aim_list
|
||||
|
||||
|
||||
def _search(_list, displayed=None, checked=None, selected=None, enabled=None, clickable=None,
|
||||
have_rect=None, have_text=None, tag=None):
|
||||
"""或关系筛选元素
|
||||
:param displayed: 是否显示,bool,None为忽略该项
|
||||
:param checked: 是否被选中,bool,None为忽略该项
|
||||
:param selected: 是否被选择,bool,None为忽略该项
|
||||
:param enabled: 是否可用,bool,None为忽略该项
|
||||
:param clickable: 是否可点击,bool,None为忽略该项
|
||||
:param have_rect: 是否拥有大小和位置,bool,None为忽略该项
|
||||
:param have_text: 是否含有文本,bool,None为忽略该项
|
||||
:param tag: 元素类型
|
||||
:return: 筛选结果
|
||||
"""
|
||||
r = ChromiumElementsList(owner=_list._owner)
|
||||
for i in _list:
|
||||
if not isinstance(i, str) and (
|
||||
(displayed is not None and (displayed is True and i.states.is_displayed)
|
||||
or (displayed is False and not i.states.is_displayed))
|
||||
or (checked is not None and (checked is True and i.states.is_checked)
|
||||
or (checked is False and not i.states.is_checked))
|
||||
or (selected is not None and (selected is True and i.states.is_selected)
|
||||
or (selected is False and not i.states.is_selected))
|
||||
or (enabled is not None and (enabled is True and i.states.is_enabled)
|
||||
or (enabled is False and not i.states.is_enabled))
|
||||
or (clickable is not None and (clickable is True and i.states.is_clickable)
|
||||
or (clickable is False and not i.states.is_clickable))
|
||||
or (have_rect is not None and (have_rect is True and i.states.has_rect)
|
||||
or (have_rect is False and not i.states.has_rect))
|
||||
or (have_text is not None and (have_text is True and i.raw_text)
|
||||
or (have_text is False and not i.raw_text))
|
||||
or (tag is not None and i.tag == tag.lower())):
|
||||
r.append(i)
|
||||
return ChromiumFilter(r)
|
||||
|
||||
|
||||
def _search_one(_list, index=1, displayed=None, checked=None, selected=None, enabled=None, clickable=None,
|
||||
have_rect=None, have_text=None, tag=None):
|
||||
"""或关系筛选元素,获取一个结果
|
||||
:param index: 元素序号,从1开始
|
||||
:param displayed: 是否显示,bool,None为忽略该项
|
||||
:param checked: 是否被选中,bool,None为忽略该项
|
||||
:param selected: 是否被选择,bool,None为忽略该项
|
||||
:param enabled: 是否可用,bool,None为忽略该项
|
||||
:param clickable: 是否可点击,bool,None为忽略该项
|
||||
:param have_rect: 是否拥有大小和位置,bool,None为忽略该项
|
||||
:param have_text: 是否含有文本,bool,None为忽略该项
|
||||
:param tag: 元素类型
|
||||
:return: 筛选结果
|
||||
"""
|
||||
num = 0
|
||||
for i in _list:
|
||||
if not isinstance(i, str) and (
|
||||
(displayed is not None and (displayed is True and i.states.is_displayed)
|
||||
or (displayed is False and not i.states.is_displayed))
|
||||
or (checked is not None and (checked is True and i.states.is_checked)
|
||||
or (checked is False and not i.states.is_checked))
|
||||
or (selected is not None and (selected is True and i.states.is_selected)
|
||||
or (selected is False and not i.states.is_selected))
|
||||
or (enabled is not None and (enabled is True and i.states.is_enabled)
|
||||
or (enabled is False and not i.states.is_enabled))
|
||||
or (clickable is not None and (clickable is True and i.states.is_clickable)
|
||||
or (clickable is False and not i.states.is_clickable))
|
||||
or (have_rect is not None and (have_rect is True and i.states.has_rect)
|
||||
or (have_rect is False and not i.states.has_rect))
|
||||
or (have_text is not None and (have_text is True and i.raw_text)
|
||||
or (have_text is False and not i.raw_text))
|
||||
or (tag is not None and i.tag == tag.lower())):
|
||||
num += 1
|
||||
if num == index:
|
||||
return i
|
||||
|
||||
return NoneElement(_list._owner, method='filter()', args={'displayed': displayed, 'checked': checked,
|
||||
'selected': selected, 'enabled': enabled,
|
||||
'clickable': clickable, 'have_rect': have_rect,
|
||||
'have_text': have_text, 'tag': tag})
|
||||
@@ -0,0 +1,588 @@
|
||||
# -*- coding:utf-8 -*-
|
||||
"""
|
||||
@Author : g1879
|
||||
@Contact : g1879@qq.com
|
||||
@Website : https://DrissionPage.cn
|
||||
@Copyright: (c) 2020 by g1879, Inc. All Rights Reserved.
|
||||
"""
|
||||
from typing import Union, List, Optional, Iterable, Dict
|
||||
|
||||
from .._base.base import BaseParser
|
||||
from .._elements.chromium_element import ChromiumElement
|
||||
from .._elements.session_element import SessionElement
|
||||
from .._pages.chromium_base import ChromiumBase
|
||||
from .._pages.chromium_frame import ChromiumFrame
|
||||
from .._pages.session_page import SessionPage
|
||||
|
||||
|
||||
class SessionElementsList(list):
|
||||
_owner: SessionPage = ...
|
||||
|
||||
def __init__(self,
|
||||
owner: SessionPage = None,
|
||||
*args):
|
||||
"""
|
||||
:param owner: 产生元素列表的页面
|
||||
:param args:
|
||||
"""
|
||||
...
|
||||
|
||||
def __next__(self) -> SessionElement: ...
|
||||
|
||||
def __getitem__(self, _i) -> Union[SessionElement, SessionElementsList]: ...
|
||||
|
||||
def __iter__(self) -> List[SessionElement]: ...
|
||||
|
||||
@property
|
||||
def get(self) -> Getter:
|
||||
"""返回用于属性的对象"""
|
||||
...
|
||||
|
||||
@property
|
||||
def filter(self) -> SessionFilter:
|
||||
"""返回用于筛选多个元素的对象"""
|
||||
...
|
||||
|
||||
@property
|
||||
def filter_one(self) -> SessionFilterOne:
|
||||
"""用于筛选单个元素的对象"""
|
||||
...
|
||||
|
||||
|
||||
class ChromiumElementsList(SessionElementsList):
|
||||
_owner: ChromiumBase = ...
|
||||
|
||||
def __init__(self,
|
||||
owner: ChromiumBase = None,
|
||||
*args):
|
||||
"""
|
||||
:param owner: 产生元素列表的页面
|
||||
:param args:
|
||||
"""
|
||||
...
|
||||
|
||||
def __next__(self) -> ChromiumElement: ...
|
||||
|
||||
def __getitem__(self, _i) -> Union[ChromiumElement, ChromiumElementsList]: ...
|
||||
|
||||
def __iter__(self) -> List[ChromiumElement]: ...
|
||||
|
||||
@property
|
||||
def filter(self) -> ChromiumFilter:
|
||||
"""返回用于筛选多个元素的对象"""
|
||||
...
|
||||
|
||||
@property
|
||||
def filter_one(self) -> ChromiumFilterOne:
|
||||
"""用于筛选单个元素的对象"""
|
||||
...
|
||||
|
||||
def search(self,
|
||||
displayed: Optional[bool] = None,
|
||||
checked: Optional[bool] = None,
|
||||
selected: Optional[bool] = None,
|
||||
enabled: Optional[bool] = None,
|
||||
clickable: Optional[bool] = None,
|
||||
have_rect: Optional[bool] = None,
|
||||
have_text: Optional[bool] = None,
|
||||
tag: str = None) -> ChromiumFilter:
|
||||
"""或关系筛选元素
|
||||
:param displayed: 是否显示,bool,None为忽略该项
|
||||
:param checked: 是否被选中,bool,None为忽略该项
|
||||
:param selected: 是否被选择,bool,None为忽略该项
|
||||
:param enabled: 是否可用,bool,None为忽略该项
|
||||
:param clickable: 是否可点击,bool,None为忽略该项
|
||||
:param have_rect: 是否拥有大小和位置,bool,None为忽略该项
|
||||
:param have_text: 是否含有文本,bool,None为忽略该项
|
||||
:param tag: 指定的元素类型
|
||||
:return: 筛选结果
|
||||
"""
|
||||
...
|
||||
|
||||
def search_one(self,
|
||||
index: int = 1,
|
||||
displayed: Optional[bool] = None,
|
||||
checked: Optional[bool] = None,
|
||||
selected: Optional[bool] = None,
|
||||
enabled: Optional[bool] = None,
|
||||
clickable: Optional[bool] = None,
|
||||
have_rect: Optional[bool] = None,
|
||||
have_text: Optional[bool] = None,
|
||||
tag: str = None) -> ChromiumElement:
|
||||
"""或关系筛选元素,获取一个结果
|
||||
:param index: 元素序号,从1开始
|
||||
:param displayed: 是否显示,bool,None为忽略该项
|
||||
:param checked: 是否被选中,bool,None为忽略该项
|
||||
:param selected: 是否被选择,bool,None为忽略该项
|
||||
:param enabled: 是否可用,bool,None为忽略该项
|
||||
:param clickable: 是否可点击,bool,None为忽略该项
|
||||
:param have_rect: 是否拥有大小和位置,bool,None为忽略该项
|
||||
:param have_text: 是否含有文本,bool,None为忽略该项
|
||||
:param tag: 指定的元素类型
|
||||
:return: 筛选结果
|
||||
"""
|
||||
...
|
||||
|
||||
|
||||
class SessionFilterOne(object):
|
||||
_list: SessionElementsList = ...
|
||||
_index: int = ...
|
||||
|
||||
def __init__(self, _list: SessionElementsList):
|
||||
"""
|
||||
:param _list: 元素列表对象
|
||||
"""
|
||||
...
|
||||
|
||||
def __call__(self, index: int = 1) -> SessionFilterOne:
|
||||
"""返回结果中第几个元素
|
||||
:param index: 元素序号,从1开始
|
||||
:return: 对象自身
|
||||
"""
|
||||
...
|
||||
|
||||
def tag(self, name: str, equal: bool = True) -> SessionElement:
|
||||
"""筛选某种元素
|
||||
:param name: 标签页名称
|
||||
:param equal: True表示匹配这种元素,False表示匹配非这种元素
|
||||
:return: 筛选结果
|
||||
"""
|
||||
...
|
||||
|
||||
def attr(self, name: str, value: str, equal: bool = True) -> SessionElement:
|
||||
"""以是否拥有某个attribute值为条件筛选元素
|
||||
:param name: 属性名称
|
||||
:param value: 属性值
|
||||
:param equal: True表示匹配name值为value值的元素,False表示匹配name值不为value值的
|
||||
:return: 筛选结果
|
||||
"""
|
||||
...
|
||||
|
||||
def text(self, text: str, fuzzy: bool = True, contain: bool = True) -> SessionElement:
|
||||
"""以是否含有指定文本为条件筛选元素
|
||||
:param text: 用于匹配的文本
|
||||
:param fuzzy: 是否模糊匹配
|
||||
:param contain: 是否包含该字符串,False表示不包含
|
||||
:return: 筛选结果
|
||||
"""
|
||||
...
|
||||
|
||||
def _get_attr(self,
|
||||
name: str,
|
||||
value: str,
|
||||
method: str,
|
||||
equal: bool = True) -> SessionElement:
|
||||
"""返回通过某个方法可获得某个值的元素
|
||||
:param name: 属性名称
|
||||
:param value: 属性值
|
||||
:param method: 方法名称
|
||||
:return: 筛选结果
|
||||
"""
|
||||
...
|
||||
|
||||
|
||||
class SessionFilter(SessionFilterOne):
|
||||
|
||||
def __iter__(self) -> Iterable[SessionElement]: ...
|
||||
|
||||
def __next__(self) -> SessionElement: ...
|
||||
|
||||
def __len__(self) -> int: ...
|
||||
|
||||
def __getitem__(self, item: int) -> SessionElement: ...
|
||||
|
||||
@property
|
||||
def get(self) -> Getter:
|
||||
"""返回用于获取元素属性的对象"""
|
||||
...
|
||||
|
||||
def tag(self, name: str, equal: bool = True) -> SessionFilter:
|
||||
"""筛选某种元素
|
||||
:param name: 标签页名称
|
||||
:param equal: True表示匹配这种元素,False表示匹配非这种元素
|
||||
:return: 筛选结果
|
||||
"""
|
||||
...
|
||||
|
||||
def attr(self, name: str, value: str, equal: bool = True) -> SessionFilter:
|
||||
"""以是否拥有某个attribute值为条件筛选元素
|
||||
:param name: 属性名称
|
||||
:param value: 属性值
|
||||
:param equal: True表示匹配name值为value值的元素,False表示匹配name值不为value值的
|
||||
:return: 筛选结果
|
||||
"""
|
||||
...
|
||||
|
||||
def text(self, text: str, fuzzy: bool = True, contain: bool = True) -> SessionFilter:
|
||||
"""以是否含有指定文本为条件筛选元素
|
||||
:param text: 用于匹配的文本
|
||||
:param fuzzy: 是否模糊匹配
|
||||
:param contain: 是否包含该字符串,False表示不包含
|
||||
:return: 筛选结果
|
||||
"""
|
||||
...
|
||||
|
||||
def _get_attr(self,
|
||||
name: str,
|
||||
value: str,
|
||||
method: str,
|
||||
equal: bool = True) -> SessionFilter:
|
||||
"""返回通过某个方法可获得某个值的元素
|
||||
:param name: 属性名称
|
||||
:param value: 属性值
|
||||
:param method: 方法名称
|
||||
:return: 筛选结果
|
||||
"""
|
||||
...
|
||||
|
||||
|
||||
class ChromiumFilterOne(SessionFilterOne):
|
||||
_list: ChromiumElementsList = ...
|
||||
|
||||
def __init__(self, _list: ChromiumElementsList):
|
||||
"""
|
||||
:param _list: 元素列表对象
|
||||
"""
|
||||
...
|
||||
|
||||
def __call__(self, index: int = 1) -> ChromiumFilterOne:
|
||||
"""返回结果中第几个元素
|
||||
:param index: 元素序号,从1开始
|
||||
:return: 对象自身
|
||||
"""
|
||||
...
|
||||
|
||||
def tag(self, name: str, equal: bool = True) -> SessionElement:
|
||||
"""筛选某种元素
|
||||
:param name: 标签页名称
|
||||
:param equal: True表示匹配这种元素,False表示匹配非这种元素
|
||||
:return: 筛选结果
|
||||
"""
|
||||
...
|
||||
|
||||
def attr(self, name: str, value: str, equal: bool = True) -> ChromiumElement:
|
||||
"""以是否拥有某个attribute值为条件筛选元素
|
||||
:param name: 属性名称
|
||||
:param value: 属性值
|
||||
:param equal: True表示匹配name值为value值的元素,False表示匹配name值不为value值的
|
||||
:return: 筛选结果
|
||||
"""
|
||||
...
|
||||
|
||||
def text(self,
|
||||
text: str,
|
||||
fuzzy: bool = True,
|
||||
contain: bool = True) -> ChromiumElement:
|
||||
"""以是否含有指定文本为条件筛选元素
|
||||
:param text: 用于匹配的文本
|
||||
:param fuzzy: 是否模糊匹配
|
||||
:param contain: 是否包含该字符串,False表示不包含
|
||||
:return: 筛选结果
|
||||
"""
|
||||
...
|
||||
|
||||
def displayed(self, equal: bool = True) -> ChromiumElement:
|
||||
"""以是否显示为条件筛选元素
|
||||
:param equal: 是否匹配显示的元素,False匹配不显示的
|
||||
:return: 筛选结果
|
||||
"""
|
||||
...
|
||||
|
||||
def checked(self, equal: bool = True) -> ChromiumElement:
|
||||
"""以是否被选中为条件筛选元素
|
||||
:param equal: 是否匹配被选中的元素,False匹配不被选中的
|
||||
:return: 筛选结果
|
||||
"""
|
||||
...
|
||||
|
||||
def selected(self, equal: bool = True) -> ChromiumElement:
|
||||
"""以是否被选择为条件筛选元素,用于<select>元素项目
|
||||
:param equal: 是否匹配被选择的元素,False匹配不被选择的
|
||||
:return: 筛选结果
|
||||
"""
|
||||
...
|
||||
|
||||
def enabled(self, equal: bool = True) -> ChromiumElement:
|
||||
"""以是否可用为条件筛选元素
|
||||
:param equal: 是否匹配可用的元素,False表示匹配disabled状态的
|
||||
:return: 筛选结果
|
||||
"""
|
||||
...
|
||||
|
||||
def clickable(self, equal: bool = True) -> ChromiumElement:
|
||||
"""以是否可点击为条件筛选元素
|
||||
:param equal: 是否匹配可点击的元素,False表示匹配不是可点击的
|
||||
:return: 筛选结果
|
||||
"""
|
||||
...
|
||||
|
||||
def have_rect(self, equal: bool = True) -> ChromiumElement:
|
||||
"""以是否有大小为条件筛选元素
|
||||
:param equal: 是否匹配有大小的元素,False表示匹配没有大小的
|
||||
:return: 筛选结果
|
||||
"""
|
||||
...
|
||||
|
||||
def style(self, name: str, value: str, equal: bool = True) -> ChromiumElement:
|
||||
"""以是否拥有某个style值为条件筛选元素
|
||||
:param name: 属性名称
|
||||
:param value: 属性值
|
||||
:param equal: True表示匹配name值为value值的元素,False表示匹配name值不为value值的
|
||||
:return: 筛选结果
|
||||
"""
|
||||
...
|
||||
|
||||
def property(self,
|
||||
name: str,
|
||||
value: str, equal: bool = True) -> ChromiumElement:
|
||||
"""以是否拥有某个property值为条件筛选元素
|
||||
:param name: 属性名称
|
||||
:param value: 属性值
|
||||
:param equal: True表示匹配name值为value值的元素,False表示匹配name值不为value值的
|
||||
:return: 筛选结果
|
||||
"""
|
||||
...
|
||||
|
||||
def _get_attr(self,
|
||||
name: str,
|
||||
value: str,
|
||||
method: str, equal: bool = True) -> ChromiumElement:
|
||||
"""返回通过某个方法可获得某个值的元素
|
||||
:param name: 属性名称
|
||||
:param value: 属性值
|
||||
:param method: 方法名称
|
||||
:return: 筛选结果
|
||||
"""
|
||||
...
|
||||
|
||||
def _any_state(self, name: str, equal: bool = True) -> ChromiumElement:
|
||||
"""
|
||||
:param name: 状态名称
|
||||
:param equal: 是否是指定状态,False表示否定状态
|
||||
:return: 选中的列表
|
||||
"""
|
||||
...
|
||||
|
||||
|
||||
class ChromiumFilter(ChromiumFilterOne):
|
||||
|
||||
def __iter__(self) -> Iterable[ChromiumElement]: ...
|
||||
|
||||
def __next__(self) -> ChromiumElement: ...
|
||||
|
||||
def __len__(self) -> int: ...
|
||||
|
||||
def __getitem__(self, item: int) -> ChromiumElement: ...
|
||||
|
||||
@property
|
||||
def get(self) -> Getter:
|
||||
"""返回用于获取元素属性的对象"""
|
||||
...
|
||||
|
||||
def tag(self, name: str, equal: bool = True) -> ChromiumFilter:
|
||||
"""筛选某种元素
|
||||
:param name: 标签页名称
|
||||
:param equal: True表示匹配这种元素,False表示匹配非这种元素
|
||||
:return: 筛选结果
|
||||
"""
|
||||
...
|
||||
|
||||
def attr(self, name: str, value: str, equal: bool = True) -> ChromiumFilter:
|
||||
"""以是否拥有某个attribute值为条件筛选元素
|
||||
:param name: 属性名称
|
||||
:param value: 属性值
|
||||
:param equal: True表示匹配name值为value值的元素,False表示匹配name值不为value值的
|
||||
:return: 筛选结果
|
||||
"""
|
||||
...
|
||||
|
||||
def text(self, text: str, fuzzy: bool = True, contain: bool = True) -> ChromiumFilter:
|
||||
"""以是否含有指定文本为条件筛选元素
|
||||
:param text: 用于匹配的文本
|
||||
:param fuzzy: 是否模糊匹配
|
||||
:param contain: 是否包含该字符串,False表示不包含
|
||||
:return: 筛选结果
|
||||
"""
|
||||
...
|
||||
|
||||
def displayed(self, equal: bool = True) -> ChromiumFilter:
|
||||
"""以是否显示为条件筛选元素
|
||||
:param equal: 是否匹配显示的元素,False匹配不显示的
|
||||
:return: 筛选结果
|
||||
"""
|
||||
...
|
||||
|
||||
def checked(self, equal: bool = True) -> ChromiumFilter:
|
||||
"""以是否被选中为条件筛选元素
|
||||
:param equal: 是否匹配被选中的元素,False匹配不被选中的
|
||||
:return: 筛选结果
|
||||
"""
|
||||
...
|
||||
|
||||
def selected(self, equal: bool = True) -> ChromiumFilter:
|
||||
"""以是否被选择为条件筛选元素,用于<select>元素项目
|
||||
:param equal: 是否匹配被选择的元素,False匹配不被选择的
|
||||
:return: 筛选结果
|
||||
"""
|
||||
...
|
||||
|
||||
def enabled(self, equal: bool = True) -> ChromiumFilter:
|
||||
"""以是否可用为条件筛选元素
|
||||
:param equal: 是否匹配可用的元素,False表示匹配disabled状态的
|
||||
:return: 筛选结果
|
||||
"""
|
||||
...
|
||||
|
||||
def clickable(self, equal: bool = True) -> ChromiumFilter:
|
||||
"""以是否可点击为条件筛选元素
|
||||
:param equal: 是否匹配可点击的元素,False表示匹配不是可点击的
|
||||
:return: 筛选结果
|
||||
"""
|
||||
...
|
||||
|
||||
def have_rect(self, equal: bool = True) -> ChromiumFilter:
|
||||
"""以是否有大小为条件筛选元素
|
||||
:param equal: 是否匹配有大小的元素,False表示匹配没有大小的
|
||||
:return: 筛选结果
|
||||
"""
|
||||
...
|
||||
|
||||
def style(self, name: str, value: str, equal: bool = True) -> ChromiumFilter:
|
||||
"""以是否拥有某个style值为条件筛选元素
|
||||
:param name: 属性名称
|
||||
:param value: 属性值
|
||||
:param equal: True表示匹配name值为value值的元素,False表示匹配name值不为value值的
|
||||
:return: 筛选结果
|
||||
"""
|
||||
...
|
||||
|
||||
def property(self,
|
||||
name: str,
|
||||
value: str, equal: bool = True) -> ChromiumFilter:
|
||||
"""以是否拥有某个property值为条件筛选元素
|
||||
:param name: 属性名称
|
||||
:param value: 属性值
|
||||
:param equal: True表示匹配name值为value值的元素,False表示匹配name值不为value值的
|
||||
:return: 筛选结果
|
||||
"""
|
||||
...
|
||||
|
||||
def search_one(self,
|
||||
index: int = 1,
|
||||
displayed: Optional[bool] = None,
|
||||
checked: Optional[bool] = None,
|
||||
selected: Optional[bool] = None,
|
||||
enabled: Optional[bool] = None,
|
||||
clickable: Optional[bool] = None,
|
||||
have_rect: Optional[bool] = None,
|
||||
have_text: Optional[bool] = None,
|
||||
tag: str = None) -> ChromiumElement:
|
||||
"""或关系筛选元素,获取一个结果
|
||||
:param index: 元素序号,从1开始
|
||||
:param displayed: 是否显示,bool,None为忽略该项
|
||||
:param checked: 是否被选中,bool,None为忽略该项
|
||||
:param selected: 是否被选择,bool,None为忽略该项
|
||||
:param enabled: 是否可用,bool,None为忽略该项
|
||||
:param clickable: 是否可点击,bool,None为忽略该项
|
||||
:param have_rect: 是否拥有大小和位置,bool,None为忽略该项
|
||||
:param have_text: 是否含有文本,bool,None为忽略该项
|
||||
:param tag: 指定的元素类型
|
||||
:return: 筛选结果
|
||||
"""
|
||||
...
|
||||
|
||||
def search(self,
|
||||
displayed: Optional[bool] = None,
|
||||
checked: Optional[bool] = None,
|
||||
selected: Optional[bool] = None,
|
||||
enabled: Optional[bool] = None,
|
||||
clickable: Optional[bool] = None,
|
||||
have_rect: Optional[bool] = None,
|
||||
have_text: Optional[bool] = None,
|
||||
tag: str = None) -> ChromiumFilter:
|
||||
"""或关系筛选元素
|
||||
:param displayed: 是否显示,bool,None为忽略该项
|
||||
:param checked: 是否被选中,bool,None为忽略该项
|
||||
:param selected: 是否被选择,bool,None为忽略该项
|
||||
:param enabled: 是否可用,bool,None为忽略该项
|
||||
:param clickable: 是否可点击,bool,None为忽略该项
|
||||
:param have_rect: 是否拥有大小和位置,bool,None为忽略该项
|
||||
:param have_text: 是否含有文本,bool,None为忽略该项
|
||||
:param tag: 指定的元素类型
|
||||
:return: 筛选结果
|
||||
"""
|
||||
...
|
||||
|
||||
def _get_attr(self,
|
||||
name: str,
|
||||
value: str,
|
||||
method: str, equal: bool = True) -> ChromiumFilter:
|
||||
"""返回通过某个方法可获得某个值的元素
|
||||
:param name: 属性名称
|
||||
:param value: 属性值
|
||||
:param method: 方法名称
|
||||
:return: 筛选结果
|
||||
"""
|
||||
...
|
||||
|
||||
def _any_state(self, name: str, equal: bool = True) -> ChromiumFilter:
|
||||
"""
|
||||
:param name: 状态名称
|
||||
:param equal: 是否是指定状态,False表示否定状态
|
||||
:return: 选中的列表
|
||||
"""
|
||||
...
|
||||
|
||||
|
||||
class Getter(object):
|
||||
_list: SessionElementsList = ...
|
||||
|
||||
def __init__(self, _list: SessionElementsList):
|
||||
"""
|
||||
:param _list: 元素列表对象
|
||||
"""
|
||||
...
|
||||
|
||||
def links(self) -> List[str]:
|
||||
"""返回所有元素的link属性组成的列表"""
|
||||
...
|
||||
|
||||
def texts(self) -> List[str]:
|
||||
"""返回所有元素的text属性组成的列表"""
|
||||
...
|
||||
|
||||
def attrs(self, name: str) -> List[str]:
|
||||
"""返回所有元素指定的attr属性组成的列表
|
||||
:param name: 属性名称
|
||||
:return: 属性文本组成的列表
|
||||
"""
|
||||
...
|
||||
|
||||
|
||||
def get_eles(locators: Union[str, tuple, List[Union[str, tuple]]],
|
||||
owner: BaseParser,
|
||||
any_one: bool = False,
|
||||
first_ele: bool = True,
|
||||
timeout: float = 10) -> Union[Dict[str, ChromiumElement], Dict[str, SessionElement],
|
||||
Dict[str, List[ChromiumElement]], Dict[str, List[SessionElement]]]:
|
||||
"""传入多个定位符,获取多个ele
|
||||
:param locators: 定位符或它们组成的列表
|
||||
:param owner: 页面或元素对象
|
||||
:param any_one: 是否找到任何一个即返回
|
||||
:param first_ele: 每个定位符是否只获取第一个元素
|
||||
:param timeout: 超时时间(秒)
|
||||
:return: 多个定位符组成的dict,first_only为False返回列表,否则为元素,无结果的返回False
|
||||
"""
|
||||
...
|
||||
|
||||
|
||||
def get_frame(owner: BaseParser,
|
||||
loc_ind_ele: Union[str, int, tuple, ChromiumFrame, ChromiumElement],
|
||||
timeout: float = None) -> ChromiumFrame:
|
||||
"""获取页面中一个frame对象
|
||||
:param owner: 要在其中查找元素的对象
|
||||
:param loc_ind_ele: 定位符、iframe序号、ChromiumFrame对象,序号从1开始,可传入负数获取倒数第几个
|
||||
:param timeout: 查找元素超时时间(秒)
|
||||
:return: ChromiumFrame对象
|
||||
"""
|
||||
...
|
||||
@@ -0,0 +1,436 @@
|
||||
# -*- coding:utf-8 -*-
|
||||
"""
|
||||
@Author : g1879
|
||||
@Contact : g1879@qq.com
|
||||
@Website : https://DrissionPage.cn
|
||||
@Copyright: (c) 2020 by g1879, Inc. All Rights Reserved.
|
||||
"""
|
||||
from platform import system
|
||||
|
||||
from ..errors import AlertExistsError
|
||||
|
||||
modifierBit = {'\ue00a': 1, '\ue009': 2, '\ue03d': 4, '\ue008': 8}
|
||||
sys = system().lower()
|
||||
|
||||
|
||||
class Keys:
|
||||
"""特殊按键"""
|
||||
NULL = '\ue000'
|
||||
CANCEL = '\ue001' # ^break
|
||||
HELP = '\ue002'
|
||||
BACKSPACE = '\ue003'
|
||||
TAB = '\ue004'
|
||||
CLEAR = '\ue005'
|
||||
RETURN = '\ue006'
|
||||
ENTER = '\ue007'
|
||||
SHIFT = '\ue008'
|
||||
CONTROL = '\ue009'
|
||||
CTRL = '\ue009'
|
||||
ALT = '\ue00a'
|
||||
PAUSE = '\ue00b'
|
||||
ESCAPE = '\ue00c'
|
||||
SPACE = '\ue00d'
|
||||
PAGE_UP = '\ue00e'
|
||||
PAGE_DOWN = '\ue00f'
|
||||
END = '\ue010'
|
||||
HOME = '\ue011'
|
||||
LEFT = '\ue012'
|
||||
UP = '\ue013'
|
||||
RIGHT = '\ue014'
|
||||
DOWN = '\ue015'
|
||||
INSERT = '\ue016'
|
||||
DELETE = '\ue017'
|
||||
DEL = '\ue017'
|
||||
SEMICOLON = '\ue018'
|
||||
EQUALS = '\ue019'
|
||||
|
||||
META = '\ue03d'
|
||||
COMMAND = '\ue03d'
|
||||
|
||||
CTRL_COMM = '\ue03d' if sys in ('macos', 'darwin') else '\ue009'
|
||||
|
||||
CTRL_A = (CTRL_COMM, 'a')
|
||||
CTRL_C = (CTRL_COMM, 'c')
|
||||
CTRL_X = (CTRL_COMM, 'x')
|
||||
CTRL_V = (CTRL_COMM, 'v')
|
||||
CTRL_Z = (CTRL_COMM, 'z')
|
||||
CTRL_Y = (CTRL_COMM, 'y')
|
||||
|
||||
NUMPAD0 = '\ue01a' # number pad keys
|
||||
NUMPAD1 = '\ue01b'
|
||||
NUMPAD2 = '\ue01c'
|
||||
NUMPAD3 = '\ue01d'
|
||||
NUMPAD4 = '\ue01e'
|
||||
NUMPAD5 = '\ue01f'
|
||||
NUMPAD6 = '\ue020'
|
||||
NUMPAD7 = '\ue021'
|
||||
NUMPAD8 = '\ue022'
|
||||
NUMPAD9 = '\ue023'
|
||||
MULTIPLY = '\ue024'
|
||||
ADD = '\ue025'
|
||||
# SEPARATOR = '\ue026'
|
||||
SUBTRACT = '\ue027'
|
||||
DECIMAL = '\ue028'
|
||||
DIVIDE = '\ue029'
|
||||
|
||||
F1 = '\ue031' # function keys
|
||||
F2 = '\ue032'
|
||||
F3 = '\ue033'
|
||||
F4 = '\ue034'
|
||||
F5 = '\ue035'
|
||||
F6 = '\ue036'
|
||||
F7 = '\ue037'
|
||||
F8 = '\ue038'
|
||||
F9 = '\ue039'
|
||||
F10 = '\ue03a'
|
||||
F11 = '\ue03b'
|
||||
F12 = '\ue03c'
|
||||
# ZENKAKU_HANKAKU = '\ue040'
|
||||
|
||||
|
||||
keyDefinitions = {
|
||||
'0': {'keyCode': 48, 'key': '0', 'code': 'Digit0'},
|
||||
'1': {'keyCode': 49, 'key': '1', 'code': 'Digit1'},
|
||||
'2': {'keyCode': 50, 'key': '2', 'code': 'Digit2'},
|
||||
'3': {'keyCode': 51, 'key': '3', 'code': 'Digit3'},
|
||||
'4': {'keyCode': 52, 'key': '4', 'code': 'Digit4'},
|
||||
'5': {'keyCode': 53, 'key': '5', 'code': 'Digit5'},
|
||||
'6': {'keyCode': 54, 'key': '6', 'code': 'Digit6'},
|
||||
'7': {'keyCode': 55, 'key': '7', 'code': 'Digit7'},
|
||||
'8': {'keyCode': 56, 'key': '8', 'code': 'Digit8'},
|
||||
'9': {'keyCode': 57, 'key': '9', 'code': 'Digit9'},
|
||||
'a': {'keyCode': 65, 'key': 'a', 'code': 'KeyA'},
|
||||
'b': {'keyCode': 66, 'key': 'b', 'code': 'KeyB'},
|
||||
'c': {'keyCode': 67, 'key': 'c', 'code': 'KeyC'},
|
||||
'd': {'keyCode': 68, 'key': 'd', 'code': 'KeyD'},
|
||||
'e': {'keyCode': 69, 'key': 'e', 'code': 'KeyE'},
|
||||
'f': {'keyCode': 70, 'key': 'f', 'code': 'KeyF'},
|
||||
'g': {'keyCode': 71, 'key': 'g', 'code': 'KeyG'},
|
||||
'h': {'keyCode': 72, 'key': 'h', 'code': 'KeyH'},
|
||||
'i': {'keyCode': 73, 'key': 'i', 'code': 'KeyI'},
|
||||
'j': {'keyCode': 74, 'key': 'j', 'code': 'KeyJ'},
|
||||
'k': {'keyCode': 75, 'key': 'k', 'code': 'KeyK'},
|
||||
'l': {'keyCode': 76, 'key': 'l', 'code': 'KeyL'},
|
||||
'm': {'keyCode': 77, 'key': 'm', 'code': 'KeyM'},
|
||||
'n': {'keyCode': 78, 'key': 'n', 'code': 'KeyN'},
|
||||
'o': {'keyCode': 79, 'key': 'o', 'code': 'KeyO'},
|
||||
'p': {'keyCode': 80, 'key': 'p', 'code': 'KeyP'},
|
||||
'q': {'keyCode': 81, 'key': 'q', 'code': 'KeyQ'},
|
||||
'r': {'keyCode': 82, 'key': 'r', 'code': 'KeyR'},
|
||||
's': {'keyCode': 83, 'key': 's', 'code': 'KeyS'},
|
||||
't': {'keyCode': 84, 'key': 't', 'code': 'KeyT'},
|
||||
'u': {'keyCode': 85, 'key': 'u', 'code': 'KeyU'},
|
||||
'v': {'keyCode': 86, 'key': 'v', 'code': 'KeyV'},
|
||||
'w': {'keyCode': 87, 'key': 'w', 'code': 'KeyW'},
|
||||
'x': {'keyCode': 88, 'key': 'x', 'code': 'KeyX'},
|
||||
'y': {'keyCode': 89, 'key': 'y', 'code': 'KeyY'},
|
||||
'z': {'keyCode': 90, 'key': 'z', 'code': 'KeyZ'},
|
||||
' ': {'keyCode': 32, 'key': ' ', 'code': 'Space'},
|
||||
'*': {'keyCode': 106, 'key': '*', 'code': 'NumpadMultiply', 'location': 3},
|
||||
'+': {'keyCode': 107, 'key': '+', 'code': 'NumpadAdd', 'location': 3},
|
||||
'-': {'keyCode': 109, 'key': '-', 'code': 'NumpadSubtract', 'location': 3},
|
||||
'/': {'keyCode': 111, 'key': '/', 'code': 'NumpadDivide', 'location': 3},
|
||||
';': {'keyCode': 186, 'key': ';', 'code': 'Semicolon'},
|
||||
'=': {'keyCode': 187, 'key': '=', 'code': 'Equal'},
|
||||
',': {'keyCode': 188, 'key': ',', 'code': 'Comma'},
|
||||
'.': {'keyCode': 190, 'key': '.', 'code': 'Period'},
|
||||
'`': {'keyCode': 192, 'key': '`', 'code': 'Backquote'},
|
||||
'[': {'keyCode': 219, 'key': '[', 'code': 'BracketLeft'},
|
||||
'\\': {'keyCode': 220, 'key': '\\', 'code': 'Backslash'},
|
||||
']': {'keyCode': 221, 'key': ']', 'code': 'BracketRight'},
|
||||
'\'': {'keyCode': 222, 'key': '\'', 'code': 'Quote'},
|
||||
')': {'keyCode': 48, 'key': ')', 'code': 'Digit0'},
|
||||
'!': {'keyCode': 49, 'key': '!', 'code': 'Digit1'},
|
||||
'@': {'keyCode': 50, 'key': '@', 'code': 'Digit2'},
|
||||
'#': {'keyCode': 51, 'key': '#', 'code': 'Digit3'},
|
||||
'$': {'keyCode': 52, 'key': '$', 'code': 'Digit4'},
|
||||
'%': {'keyCode': 53, 'key': '%', 'code': 'Digit5'},
|
||||
'^': {'keyCode': 54, 'key': '^', 'code': 'Digit6'},
|
||||
'&': {'keyCode': 55, 'key': '&', 'code': 'Digit7'},
|
||||
'(': {'keyCode': 57, 'key': '(', 'code': 'Digit9'},
|
||||
'A': {'keyCode': 65, 'key': 'A', 'code': 'KeyA'},
|
||||
'B': {'keyCode': 66, 'key': 'B', 'code': 'KeyB'},
|
||||
'C': {'keyCode': 67, 'key': 'C', 'code': 'KeyC'},
|
||||
'D': {'keyCode': 68, 'key': 'D', 'code': 'KeyD'},
|
||||
'E': {'keyCode': 69, 'key': 'E', 'code': 'KeyE'},
|
||||
'F': {'keyCode': 70, 'key': 'F', 'code': 'KeyF'},
|
||||
'G': {'keyCode': 71, 'key': 'G', 'code': 'KeyG'},
|
||||
'H': {'keyCode': 72, 'key': 'H', 'code': 'KeyH'},
|
||||
'I': {'keyCode': 73, 'key': 'I', 'code': 'KeyI'},
|
||||
'J': {'keyCode': 74, 'key': 'J', 'code': 'KeyJ'},
|
||||
'K': {'keyCode': 75, 'key': 'K', 'code': 'KeyK'},
|
||||
'L': {'keyCode': 76, 'key': 'L', 'code': 'KeyL'},
|
||||
'M': {'keyCode': 77, 'key': 'M', 'code': 'KeyM'},
|
||||
'N': {'keyCode': 78, 'key': 'N', 'code': 'KeyN'},
|
||||
'O': {'keyCode': 79, 'key': 'O', 'code': 'KeyO'},
|
||||
'P': {'keyCode': 80, 'key': 'P', 'code': 'KeyP'},
|
||||
'Q': {'keyCode': 81, 'key': 'Q', 'code': 'KeyQ'},
|
||||
'R': {'keyCode': 82, 'key': 'R', 'code': 'KeyR'},
|
||||
'S': {'keyCode': 83, 'key': 'S', 'code': 'KeyS'},
|
||||
'T': {'keyCode': 84, 'key': 'T', 'code': 'KeyT'},
|
||||
'U': {'keyCode': 85, 'key': 'U', 'code': 'KeyU'},
|
||||
'V': {'keyCode': 86, 'key': 'V', 'code': 'KeyV'},
|
||||
'W': {'keyCode': 87, 'key': 'W', 'code': 'KeyW'},
|
||||
'X': {'keyCode': 88, 'key': 'X', 'code': 'KeyX'},
|
||||
'Y': {'keyCode': 89, 'key': 'Y', 'code': 'KeyY'},
|
||||
'Z': {'keyCode': 90, 'key': 'Z', 'code': 'KeyZ'},
|
||||
':': {'keyCode': 186, 'key': ':', 'code': 'Semicolon'},
|
||||
'<': {'keyCode': 188, 'key': '<', 'code': 'Comma'},
|
||||
'_': {'keyCode': 189, 'key': '_', 'code': 'Minus'},
|
||||
'>': {'keyCode': 190, 'key': '>', 'code': 'Period'},
|
||||
'?': {'keyCode': 191, 'key': '?', 'code': 'Slash'},
|
||||
'~': {'keyCode': 192, 'key': '~', 'code': 'Backquote'},
|
||||
'{': {'keyCode': 219, 'key': '{', 'code': 'BracketLeft'},
|
||||
'|': {'keyCode': 220, 'key': '|', 'code': 'Backslash'},
|
||||
# '\ue026': {'keyCode': 220, 'key': '|', 'code': 'Backslash'},
|
||||
'}': {'keyCode': 221, 'key': '}', 'code': 'BracketRight'},
|
||||
'"': {'keyCode': 222, 'key': '"', 'code': 'Quote'},
|
||||
'\n': {'keyCode': 13, 'code': 'Enter', 'key': 'Enter', 'text': '\r'},
|
||||
'\ue007': {'keyCode': 13, 'code': 'Enter', 'key': 'Enter', 'text': '\r'},
|
||||
'\ue006': {'keyCode': 13, 'code': 'NumpadEnter', 'key': 'Enter', 'text': '\r', 'location': 3},
|
||||
'\ue003': {'keyCode': 8, 'code': 'Backspace', 'key': 'Backspace'},
|
||||
'\ue00d': {'keyCode': 32, 'code': 'Space', 'key': ' '},
|
||||
# 'PageUp': {'keyCode': 33, 'shiftKeyCode': 105, 'key': 'PageUp', 'code': 'Numpad9', 'shiftKey': '9', 'location': 3},
|
||||
'\ue00e': {'keyCode': 33, 'code': 'PageUp', 'key': 'PageUp'},
|
||||
# 'PageDown': {'keyCode': 34, 'shiftKeyCode': 99, 'key': 'PageDown', 'code': 'Numpad3', 'shiftKey': '3', 'location': 3},
|
||||
'\ue00f': {'keyCode': 34, 'code': 'PageDown', 'key': 'PageDown'},
|
||||
'\ue008': {'keyCode': 16, 'code': 'ShiftLeft', 'key': 'Shift', 'location': 1},
|
||||
# 'ShiftRight': {'keyCode': 16, 'code': 'ShiftRight', 'key': 'Shift', 'location': 2},
|
||||
'\ue009': {'keyCode': 17, 'code': 'ControlLeft', 'key': 'Control', 'location': 1},
|
||||
# 'ControlRight': {'keyCode': 17, 'code': 'ControlRight', 'key': 'Control', 'location': 2},
|
||||
'\ue00a': {'keyCode': 18, 'code': 'AltLeft', 'key': 'Alt', 'location': 1},
|
||||
# 'AltRight': {'keyCode': 18, 'code': 'AltRight', 'key': 'Alt', 'location': 2},
|
||||
'\ue03d': {'keyCode': 91, 'key': 'Meta', 'code': 'MetaLeft'},
|
||||
'\r': {'keyCode': 13, 'code': 'Enter', 'key': 'Enter', 'text': '\r'},
|
||||
'\ue011': {'keyCode': 36, 'code': 'Home', 'key': 'Home'},
|
||||
# 'Numpad7': {'keyCode': 36, 'shiftKeyCode': 103, 'key': 'Home', 'code': 'Numpad7', 'shiftKey': '7', 'location': 3},
|
||||
'\ue012': {'keyCode': 37, 'code': 'ArrowLeft', 'key': 'ArrowLeft'},
|
||||
# 'Numpad4': {'keyCode': 37, 'shiftKeyCode': 100, 'key': 'ArrowLeft', 'code': 'Numpad4', 'shiftKey': '4', 'location': 3},
|
||||
# 'Numpad8': {'keyCode': 38, 'shiftKeyCode': 104, 'key': 'ArrowUp', 'code': 'Numpad8', 'shiftKey': '8', 'location': 3},
|
||||
'\ue013': {'keyCode': 38, 'code': 'ArrowUp', 'key': 'ArrowUp'},
|
||||
'\ue014': {'keyCode': 39, 'code': 'ArrowRight', 'key': 'ArrowRight'},
|
||||
# 'Numpad6': {'keyCode': 39, 'shiftKeyCode': 102, 'key': 'ArrowRight', 'code': 'Numpad6', 'shiftKey': '6', 'location': 3},
|
||||
# 'Numpad2': {'keyCode': 40, 'shiftKeyCode': 98, 'key': 'ArrowDown', 'code': 'Numpad2', 'shiftKey': '2', 'location': 3},
|
||||
'\ue015': {'keyCode': 40, 'code': 'ArrowDown', 'key': 'ArrowDown'},
|
||||
|
||||
'\ue001': {'keyCode': 3, 'code': 'Abort', 'key': 'Cancel'},
|
||||
'\ue002': {'keyCode': 6, 'code': 'Help', 'key': 'Help'},
|
||||
'\ue004': {'keyCode': 9, 'code': 'Tab', 'key': 'Tab'},
|
||||
'\ue005': {'keyCode': 12, 'shiftKeyCode': 101, 'key': 'Clear', 'code': 'Numpad5', 'shiftKey': '5', 'location': 3},
|
||||
'\ue00b': {'keyCode': 19, 'code': 'Pause', 'key': 'Pause'},
|
||||
# 'CapsLock': {'keyCode': 20, 'code': 'CapsLock', 'key': 'CapsLock'},
|
||||
'\ue00c': {'keyCode': 27, 'code': 'Escape', 'key': 'Escape'},
|
||||
# 'Convert': {'keyCode': 28, 'code': 'Convert', 'key': 'Convert'},
|
||||
# 'NonConvert': {'keyCode': 29, 'code': 'NonConvert', 'key': 'NonConvert'},
|
||||
'\ue010': {'keyCode': 35, 'code': 'End', 'key': 'End'},
|
||||
# 'Numpad1': {'keyCode': 35, 'shiftKeyCode': 97, 'key': 'End', 'code': 'Numpad1', 'shiftKey': '1', 'location': 3},
|
||||
# 'Select': {'keyCode': 41, 'code': 'Select', 'key': 'Select'},
|
||||
# 'Open': {'keyCode': 43, 'code': 'Open', 'key': 'Execute'},
|
||||
# 'PrintScreen': {'keyCode': 44, 'code': 'PrintScreen', 'key': 'PrintScreen'},
|
||||
'\ue016': {'keyCode': 45, 'code': 'Insert', 'key': 'Insert'},
|
||||
# 'Numpad0': {'keyCode': 45, 'shiftKeyCode': 96, 'key': 'Insert', 'code': 'Numpad0', 'shiftKey': '0', 'location': 3},
|
||||
'\ue017': {'keyCode': 46, 'code': 'Delete', 'key': 'Delete'},
|
||||
'\ue028': {'keyCode': 46, 'shiftKeyCode': 110, 'code': 'NumpadDecimal', 'key': '\u0000', 'shiftKey': '.',
|
||||
'location': 3},
|
||||
'\ue01a': {'keyCode': 48, 'code': 'Digit0', 'shiftKey': ')', 'key': '0'},
|
||||
'\ue01b': {'keyCode': 49, 'code': 'Digit1', 'shiftKey': '!', 'key': '1'},
|
||||
'\ue01c': {'keyCode': 50, 'code': 'Digit2', 'shiftKey': '@', 'key': '2'},
|
||||
'\ue01d': {'keyCode': 51, 'code': 'Digit3', 'shiftKey': '#', 'key': '3'},
|
||||
'\ue01e': {'keyCode': 52, 'code': 'Digit4', 'shiftKey': '$', 'key': '4'},
|
||||
'\ue01f': {'keyCode': 53, 'code': 'Digit5', 'shiftKey': '%', 'key': '5'},
|
||||
'\ue020': {'keyCode': 54, 'code': 'Digit6', 'shiftKey': '^', 'key': '6'},
|
||||
'\ue021': {'keyCode': 55, 'code': 'Digit7', 'shiftKey': '&', 'key': '7'},
|
||||
'\ue022': {'keyCode': 56, 'code': 'Digit8', 'shiftKey': '*', 'key': '8'},
|
||||
'\ue023': {'keyCode': 57, 'code': 'Digit9', 'shiftKey': r'\(', 'key': '9'},
|
||||
'\ue024': {'keyCode': 106, 'code': 'NumpadMultiply', 'key': '*', 'location': 3},
|
||||
'\ue025': {'keyCode': 107, 'code': 'NumpadAdd', 'key': '+', 'location': 3},
|
||||
'\ue027': {'keyCode': 109, 'code': 'NumpadSubtract', 'key': '-', 'location': 3},
|
||||
'\ue029': {'keyCode': 111, 'code': 'NumpadDivide', 'key': '/', 'location': 3},
|
||||
'\ue031': {'keyCode': 112, 'code': 'F1', 'key': 'F1'},
|
||||
'\ue032': {'keyCode': 113, 'code': 'F2', 'key': 'F2'},
|
||||
'\ue033': {'keyCode': 114, 'code': 'F3', 'key': 'F3'},
|
||||
'\ue034': {'keyCode': 115, 'code': 'F4', 'key': 'F4'},
|
||||
'\ue035': {'keyCode': 116, 'code': 'F5', 'key': 'F5'},
|
||||
'\ue036': {'keyCode': 117, 'code': 'F6', 'key': 'F6'},
|
||||
'\ue037': {'keyCode': 118, 'code': 'F7', 'key': 'F7'},
|
||||
'\ue038': {'keyCode': 119, 'code': 'F8', 'key': 'F8'},
|
||||
'\ue039': {'keyCode': 120, 'code': 'F9', 'key': 'F9'},
|
||||
'\ue03a': {'keyCode': 121, 'code': 'F10', 'key': 'F10'},
|
||||
'\ue03b': {'keyCode': 122, 'code': 'F11', 'key': 'F11'},
|
||||
'\ue03c': {'keyCode': 123, 'code': 'F12', 'key': 'F12'},
|
||||
'\ue018': {'keyCode': 186, 'code': 'Semicolon', 'shiftKey': ':', 'key': ';'},
|
||||
'\ue019': {'keyCode': 187, 'code': 'NumpadEqual', 'key': '=', 'location': 3},
|
||||
'\u0000': {'keyCode': 46, 'key': '\u0000', 'code': 'NumpadDecimal', 'location': 3},
|
||||
# 'KeyA': {'keyCode': 65, 'code': 'KeyA', 'shiftKey': 'A', 'key': 'a'},
|
||||
# 'KeyB': {'keyCode': 66, 'code': 'KeyB', 'shiftKey': 'B', 'key': 'b'},
|
||||
# 'KeyC': {'keyCode': 67, 'code': 'KeyC', 'shiftKey': 'C', 'key': 'c'},
|
||||
# 'KeyD': {'keyCode': 68, 'code': 'KeyD', 'shiftKey': 'D', 'key': 'd'},
|
||||
# 'KeyE': {'keyCode': 69, 'code': 'KeyE', 'shiftKey': 'E', 'key': 'e'},
|
||||
# 'KeyF': {'keyCode': 70, 'code': 'KeyF', 'shiftKey': 'F', 'key': 'f'},
|
||||
# 'KeyG': {'keyCode': 71, 'code': 'KeyG', 'shiftKey': 'G', 'key': 'g'},
|
||||
# 'KeyH': {'keyCode': 72, 'code': 'KeyH', 'shiftKey': 'H', 'key': 'h'},
|
||||
# 'KeyI': {'keyCode': 73, 'code': 'KeyI', 'shiftKey': 'I', 'key': 'i'},
|
||||
# 'KeyJ': {'keyCode': 74, 'code': 'KeyJ', 'shiftKey': 'J', 'key': 'j'},
|
||||
# 'KeyK': {'keyCode': 75, 'code': 'KeyK', 'shiftKey': 'K', 'key': 'k'},
|
||||
# 'KeyL': {'keyCode': 76, 'code': 'KeyL', 'shiftKey': 'L', 'key': 'l'},
|
||||
# 'KeyM': {'keyCode': 77, 'code': 'KeyM', 'shiftKey': 'M', 'key': 'm'},
|
||||
# 'KeyN': {'keyCode': 78, 'code': 'KeyN', 'shiftKey': 'N', 'key': 'n'},
|
||||
# 'KeyO': {'keyCode': 79, 'code': 'KeyO', 'shiftKey': 'O', 'key': 'o'},
|
||||
# 'KeyP': {'keyCode': 80, 'code': 'KeyP', 'shiftKey': 'P', 'key': 'p'},
|
||||
# 'KeyQ': {'keyCode': 81, 'code': 'KeyQ', 'shiftKey': 'Q', 'key': 'q'},
|
||||
# 'KeyR': {'keyCode': 82, 'code': 'KeyR', 'shiftKey': 'R', 'key': 'r'},
|
||||
# 'KeyS': {'keyCode': 83, 'code': 'KeyS', 'shiftKey': 'S', 'key': 's'},
|
||||
# 'KeyT': {'keyCode': 84, 'code': 'KeyT', 'shiftKey': 'T', 'key': 't'},
|
||||
# 'KeyU': {'keyCode': 85, 'code': 'KeyU', 'shiftKey': 'U', 'key': 'u'},
|
||||
# 'KeyV': {'keyCode': 86, 'code': 'KeyV', 'shiftKey': 'V', 'key': 'v'},
|
||||
# 'KeyW': {'keyCode': 87, 'code': 'KeyW', 'shiftKey': 'W', 'key': 'w'},
|
||||
# 'KeyX': {'keyCode': 88, 'code': 'KeyX', 'shiftKey': 'X', 'key': 'x'},
|
||||
# 'KeyY': {'keyCode': 89, 'code': 'KeyY', 'shiftKey': 'Y', 'key': 'y'},
|
||||
# 'KeyZ': {'keyCode': 90, 'code': 'KeyZ', 'shiftKey': 'Z', 'key': 'z'},
|
||||
# 'MetaLeft': {'keyCode': 91, 'code': 'MetaLeft', 'key': 'Meta'},
|
||||
# 'MetaRight': {'keyCode': 92, 'code': 'MetaRight', 'key': 'Meta'},
|
||||
# 'ContextMenu': {'keyCode': 93, 'code': 'ContextMenu', 'key': 'ContextMenu'},
|
||||
# 'F13': {'keyCode': 124, 'code': 'F13', 'key': 'F13'},
|
||||
# 'F14': {'keyCode': 125, 'code': 'F14', 'key': 'F14'},
|
||||
# 'F15': {'keyCode': 126, 'code': 'F15', 'key': 'F15'},
|
||||
# 'F16': {'keyCode': 127, 'code': 'F16', 'key': 'F16'},
|
||||
# 'F17': {'keyCode': 128, 'code': 'F17', 'key': 'F17'},
|
||||
# 'F18': {'keyCode': 129, 'code': 'F18', 'key': 'F18'},
|
||||
# 'F19': {'keyCode': 130, 'code': 'F19', 'key': 'F19'},
|
||||
# 'F20': {'keyCode': 131, 'code': 'F20', 'key': 'F20'},
|
||||
# 'F21': {'keyCode': 132, 'code': 'F21', 'key': 'F21'},
|
||||
# 'F22': {'keyCode': 133, 'code': 'F22', 'key': 'F22'},
|
||||
# 'F23': {'keyCode': 134, 'code': 'F23', 'key': 'F23'},
|
||||
# 'F24': {'keyCode': 135, 'code': 'F24', 'key': 'F24'},
|
||||
# 'NumLock': {'keyCode': 144, 'code': 'NumLock', 'key': 'NumLock'},
|
||||
# 'ScrollLock': {'keyCode': 145, 'code': 'ScrollLock', 'key': 'ScrollLock'},
|
||||
# 'AudioVolumeMute': {'keyCode': 173, 'code': 'AudioVolumeMute', 'key': 'AudioVolumeMute'},
|
||||
# 'AudioVolumeDown': {'keyCode': 174, 'code': 'AudioVolumeDown', 'key': 'AudioVolumeDown'},
|
||||
# 'AudioVolumeUp': {'keyCode': 175, 'code': 'AudioVolumeUp', 'key': 'AudioVolumeUp'},
|
||||
# 'MediaTrackNext': {'keyCode': 176, 'code': 'MediaTrackNext', 'key': 'MediaTrackNext'},
|
||||
# 'MediaTrackPrevious': {'keyCode': 177, 'code': 'MediaTrackPrevious', 'key': 'MediaTrackPrevious'},
|
||||
# 'MediaStop': {'keyCode': 178, 'code': 'MediaStop', 'key': 'MediaStop'},
|
||||
# 'MediaPlayPause': {'keyCode': 179, 'code': 'MediaPlayPause', 'key': 'MediaPlayPause'},
|
||||
# 'Equal': {'keyCode': 187, 'code': 'Equal', 'shiftKey': '+', 'key': '='},
|
||||
# 'Comma': {'keyCode': 188, 'code': 'Comma', 'shiftKey': '<', 'key': ','},
|
||||
# 'Minus': {'keyCode': 189, 'code': 'Minus', 'shiftKey': '_', 'key': '-'},
|
||||
# 'Period': {'keyCode': 190, 'code': 'Period', 'shiftKey': '>', 'key': '.'},
|
||||
# 'Slash': {'keyCode': 191, 'code': 'Slash', 'shiftKey': '?', 'key': '/'},
|
||||
# 'Backquote': {'keyCode': 192, 'code': 'Backquote', 'shiftKey': '~', 'key': '`'},
|
||||
# 'BracketLeft': {'keyCode': 219, 'code': 'BracketLeft', 'shiftKey': '{', 'key': '['},
|
||||
# 'Backslash': {'keyCode': 220, 'code': 'Backslash', 'shiftKey': '|', 'key': '\\'},
|
||||
# 'BracketRight': {'keyCode': 221, 'code': 'BracketRight', 'shiftKey': '}', 'key': ']'},
|
||||
# 'Quote': {'keyCode': 222, 'code': 'Quote', 'shiftKey': '"', 'key': '\''},
|
||||
# 'AltGraph': {'keyCode': 225, 'code': 'AltGraph', 'key': 'AltGraph'},
|
||||
# 'Props': {'keyCode': 247, 'code': 'Props', 'key': 'CrSel'},
|
||||
# 'Cancel': {'keyCode': 3, 'key': 'Cancel', 'code': 'Abort'},
|
||||
# 'Clear': {'keyCode': 12, 'key': 'Clear', 'code': 'Numpad5', 'location': 3},
|
||||
# 'Shift': {'keyCode': 16, 'key': 'Shift', 'code': 'ShiftLeft'},
|
||||
# 'Control': {'keyCode': 17, 'key': 'Control', 'code': 'ControlLeft'},
|
||||
# 'Alt': {'keyCode': 18, 'key': 'Alt', 'code': 'AltLeft'},
|
||||
# 'Accept': {'keyCode': 30, 'key': 'Accept'},
|
||||
# 'ModeChange': {'keyCode': 31, 'key': 'ModeChange'},
|
||||
# 'Print': {'keyCode': 42, 'key': 'Print'},
|
||||
# 'Execute': {'keyCode': 43, 'key': 'Execute', 'code': 'Open'},
|
||||
# 'Attn': {'keyCode': 246, 'key': 'Attn'},
|
||||
# 'CrSel': {'keyCode': 247, 'key': 'CrSel', 'code': 'Props'},
|
||||
# 'ExSel': {'keyCode': 248, 'key': 'ExSel'},
|
||||
# 'EraseEof': {'keyCode': 249, 'key': 'EraseEof'},
|
||||
# 'Play': {'keyCode': 250, 'key': 'Play'},
|
||||
# 'ZoomOut': {'keyCode': 251, 'key': 'ZoomOut'},
|
||||
# 'Power': {'key': 'Power', 'code': 'Power'},
|
||||
# 'Eject': {'key': 'Eject', 'code': 'Eject'},
|
||||
}
|
||||
|
||||
|
||||
def keys_to_typing(value):
|
||||
typing = []
|
||||
modifier = 0
|
||||
for val in value:
|
||||
if val in ('\ue009', '\ue008', '\ue00a', '\ue03d'):
|
||||
modifier |= modifierBit.get(val, 0)
|
||||
continue
|
||||
if isinstance(val, (int, float)):
|
||||
val = str(val)
|
||||
for i in range(len(val)):
|
||||
typing.append(val[i])
|
||||
else:
|
||||
for i in range(len(val)):
|
||||
typing.append(val[i])
|
||||
|
||||
return modifier, ''.join(typing)
|
||||
|
||||
|
||||
def make_input_data(modifiers, key, key_up=False):
|
||||
data = keyDefinitions.get(key)
|
||||
if not data:
|
||||
return None
|
||||
|
||||
result = {'modifiers': modifiers, 'autoRepeat': False, '_ignore': AlertExistsError}
|
||||
shift = modifiers & 8
|
||||
|
||||
if shift and data.get('shiftKey'):
|
||||
result['key'] = data['shiftKey']
|
||||
result['text'] = data['shiftKey']
|
||||
elif 'key' in data:
|
||||
result['key'] = data['key']
|
||||
|
||||
if len(result.get('key', '')) == 1: # type: ignore
|
||||
result['text'] = data['key']
|
||||
|
||||
sys_text = 'windowsVirtualKeyCode' if sys == 'windows' else 'nativeVirtualKeyCode'
|
||||
if shift and data.get('shiftKeyCode'):
|
||||
result[sys_text] = data['shiftKeyCode']
|
||||
elif 'keyCode' in data:
|
||||
result[sys_text] = data['keyCode']
|
||||
|
||||
if 'code' in data:
|
||||
result['code'] = data['code']
|
||||
|
||||
if 'location' in data:
|
||||
result['location'] = data['location']
|
||||
result['isKeypad'] = data['location'] == 3
|
||||
else:
|
||||
result['location'] = 0
|
||||
result['isKeypad'] = False
|
||||
|
||||
if shift and data.get('shiftText'):
|
||||
result['text'] = data['shiftText']
|
||||
result['unmodifiedText'] = data['shiftText']
|
||||
elif 'text' in data:
|
||||
result['text'] = data['text']
|
||||
result['unmodifiedText'] = data['text']
|
||||
|
||||
if modifiers & ~8:
|
||||
result['text'] = ''
|
||||
|
||||
result['type'] = 'keyUp' if key_up else ('keyDown' if result.get('text') else 'rawKeyDown')
|
||||
return result
|
||||
|
||||
|
||||
def send_key(page, modifier, key):
|
||||
data = make_input_data(modifier, key)
|
||||
if data:
|
||||
page._run_cdp('Input.dispatchKeyEvent', **data)
|
||||
data['type'] = 'keyUp'
|
||||
page._run_cdp('Input.dispatchKeyEvent', **data)
|
||||
|
||||
else:
|
||||
page._run_cdp('Input.insertText', text=key, _ignore=AlertExistsError)
|
||||
|
||||
|
||||
def input_text_or_keys(page, text_or_keys):
|
||||
if not isinstance(text_or_keys, (tuple, list)):
|
||||
text_or_keys = (str(text_or_keys),)
|
||||
modifier, text_or_keys = keys_to_typing(text_or_keys)
|
||||
|
||||
if modifier != 0: # 包含修饰符
|
||||
for key in text_or_keys:
|
||||
send_key(page, modifier, key)
|
||||
return
|
||||
|
||||
if text_or_keys.endswith(('\n', '\ue007', '\ue006')):
|
||||
page._run_cdp('Input.insertText', text=text_or_keys[:-1], _ignore=AlertExistsError)
|
||||
send_key(page, modifier, '\n')
|
||||
else:
|
||||
page._run_cdp('Input.insertText', text=text_or_keys, _ignore=AlertExistsError)
|
||||
@@ -0,0 +1,122 @@
|
||||
# -*- coding:utf-8 -*-
|
||||
"""
|
||||
@Author : g1879
|
||||
@Contact : g1879@qq.com
|
||||
@Website : https://DrissionPage.cn
|
||||
@Copyright: (c) 2020 by g1879, Inc. All Rights Reserved.
|
||||
"""
|
||||
from typing import Tuple, Union, Any
|
||||
|
||||
from .._pages.chromium_base import ChromiumBase
|
||||
|
||||
|
||||
class Keys:
|
||||
"""特殊按键"""
|
||||
CTRL_A: tuple
|
||||
CTRL_C: tuple
|
||||
CTRL_X: tuple
|
||||
CTRL_V: tuple
|
||||
CTRL_Z: tuple
|
||||
CTRL_Y: tuple
|
||||
|
||||
NULL: str
|
||||
CANCEL: str
|
||||
HELP: str
|
||||
BACKSPACE: str
|
||||
TAB: str
|
||||
CLEAR: str
|
||||
RETURN: str
|
||||
ENTER: str
|
||||
SHIFT: str
|
||||
CONTROL: str
|
||||
CTRL: str
|
||||
ALT: str
|
||||
PAUSE: str
|
||||
ESCAPE: str
|
||||
SPACE: str
|
||||
PAGE_UP: str
|
||||
PAGE_DOWN: str
|
||||
END: str
|
||||
HOME: str
|
||||
LEFT: str
|
||||
UP: str
|
||||
RIGHT: str
|
||||
DOWN: str
|
||||
INSERT: str
|
||||
DELETE: str
|
||||
DEL: str
|
||||
SEMICOLON: str
|
||||
EQUALS: str
|
||||
|
||||
NUMPAD0: str
|
||||
NUMPAD1: str
|
||||
NUMPAD2: str
|
||||
NUMPAD3: str
|
||||
NUMPAD4: str
|
||||
NUMPAD5: str
|
||||
NUMPAD6: str
|
||||
NUMPAD7: str
|
||||
NUMPAD8: str
|
||||
NUMPAD9: str
|
||||
MULTIPLY: str
|
||||
ADD: str
|
||||
SUBTRACT: str
|
||||
DECIMAL: str
|
||||
DIVIDE: str
|
||||
|
||||
F1: str
|
||||
F2: str
|
||||
F3: str
|
||||
F4: str
|
||||
F5: str
|
||||
F6: str
|
||||
F7: str
|
||||
F8: str
|
||||
F9: str
|
||||
F10: str
|
||||
F11: str
|
||||
F12: str
|
||||
|
||||
META: str
|
||||
COMMAND: str
|
||||
|
||||
|
||||
keyDefinitions: dict = ...
|
||||
modifierBit: dict = ...
|
||||
|
||||
|
||||
def keys_to_typing(value: Union[str, int, list, tuple]) -> Tuple[int, str]:
|
||||
"""把要输入的内容连成字符串,去掉其中 ctrl 等键。
|
||||
返回的modifier表示是否有按下组合键"""
|
||||
...
|
||||
|
||||
|
||||
def make_input_data(modifiers: int,
|
||||
key: str,
|
||||
key_up: bool = False) -> dict:
|
||||
"""
|
||||
:param modifiers: 功能键设置
|
||||
:param key: 按键字符
|
||||
:param key_up: 是否提起
|
||||
:return: None
|
||||
"""
|
||||
...
|
||||
|
||||
|
||||
def send_key(page: ChromiumBase, modifier: int, key: str) -> None:
|
||||
"""发送一个字,在键盘中的字符触发按键,其它直接发送文本
|
||||
:param page: 动作所在页面
|
||||
:param modifier: 功能键信息
|
||||
:param key: 要是输入的按键
|
||||
:return: None
|
||||
"""
|
||||
...
|
||||
|
||||
|
||||
def input_text_or_keys(page: ChromiumBase, text_or_keys: Any) -> None:
|
||||
"""输入文本,也可输入组合键,组合键用tuple形式输入
|
||||
:param page: ChromiumBase对象
|
||||
:param text_or_keys: 文本值或按键组合
|
||||
:return: self
|
||||
"""
|
||||
...
|
||||
@@ -0,0 +1,549 @@
|
||||
# -*- coding:utf-8 -*-
|
||||
"""
|
||||
@Author : g1879
|
||||
@Contact : g1879@qq.com
|
||||
@Website : https://DrissionPage.cn
|
||||
@Copyright: (c) 2020 by g1879, Inc. All Rights Reserved.
|
||||
"""
|
||||
from re import split
|
||||
|
||||
from .by import By
|
||||
from .._functions.settings import Settings as _S
|
||||
from ..errors import LocatorError
|
||||
|
||||
|
||||
def locator_to_tuple(loc):
|
||||
loc = _preprocess(loc)
|
||||
|
||||
# 多属性查找
|
||||
if loc.startswith(('@@', '@|', '@!')) and loc not in ('@@', '@|', '@!'):
|
||||
args = _get_args(loc)
|
||||
|
||||
# 单属性查找
|
||||
elif loc.startswith('@') and loc != '@':
|
||||
arg = _get_arg(loc[1:])
|
||||
arg.append(False)
|
||||
args = {'and': True, 'args': [arg]}
|
||||
|
||||
# 根据tag name查找
|
||||
elif loc.startswith(('tag:', 'tag=', 'tag^', 'tag$')) and loc not in ('tag:', 'tag=', 'tag^', 'tag$'):
|
||||
at_ind = loc.find('@')
|
||||
if at_ind == -1:
|
||||
args = {'and': True, 'args': [['tag()', '=', loc[4:].lower(), False]]}
|
||||
else:
|
||||
args_str = loc[at_ind:]
|
||||
if args_str.startswith(('@@', '@|', '@!')):
|
||||
args = _get_args(args_str)
|
||||
args['args'].append([f'tag()', '=', loc[4:at_ind].lower(), False])
|
||||
else: # t:div@aa=bb的格式
|
||||
arg = _get_arg(loc[at_ind + 1:])
|
||||
arg.append(False)
|
||||
args = {'and': True, 'args': [['tag()', '=', loc[4:at_ind].lower(), False], arg]}
|
||||
|
||||
# 根据文本查找
|
||||
elif loc.startswith(('text=', 'text:', 'text^', 'text$')):
|
||||
args = {'and': True, 'args': [['text()', loc[4], loc[5:], False]]}
|
||||
|
||||
# 根据文本模糊查找
|
||||
else:
|
||||
args = {'and': True, 'args': [['text()', '=', loc, False]]}
|
||||
|
||||
return args
|
||||
|
||||
|
||||
def _get_args(text: str = '') -> dict:
|
||||
"""解析定位参数字符串生成dict格式数据
|
||||
:param text: 待处理的字符串
|
||||
:return: 格式: {'and': bool, 'args': ['属性名称', '匹配方式', '属性值', 是否否定]}
|
||||
"""
|
||||
arg_list = []
|
||||
args = split(r'(@!|@@|@\|)', text)[1:]
|
||||
if '@@' in args and '@|' in args:
|
||||
raise LocatorError(_S._lang.SYMBOL_CONFLICT, VALUE=text)
|
||||
_and = '@|' not in args
|
||||
|
||||
for k in range(0, len(args) - 1, 2):
|
||||
arg = _get_arg(args[k + 1])
|
||||
if arg:
|
||||
arg.append(True if args[k] == '@!' else False) # 是否去除某个属性
|
||||
arg_list.append(arg)
|
||||
|
||||
return {'and': _and, 'args': arg_list}
|
||||
|
||||
|
||||
def _get_arg(text) -> list:
|
||||
"""解析arg=abc格式字符串,生成格式:['属性名称', '匹配方式', '属性值', 是否否定],不是式子的返回None"""
|
||||
r = split(r'([:=$^])', text, maxsplit=1)
|
||||
if not r[0]:
|
||||
return [None, None, None, None]
|
||||
# !=时只有属性名没有属性内容,查询是否存在该属性
|
||||
name = r[0] if r[0] != 'tx()' else 'text()'
|
||||
name = name if name != 't()' else 'teg()'
|
||||
return [name, None, None] if len(r) != 3 else [name, r[1], r[2]]
|
||||
|
||||
|
||||
def is_str_loc(text):
|
||||
return text.startswith(('.', '#', '@', 't:', 't=', 'tag:', 'tag=', 'tx:', 'tx=', 'tx^', 'tx$', 'text:', 'text=',
|
||||
'text^', 'text$', 'xpath:', 'xpath=', 'x:', 'x=', 'css:', 'css=', 'c:', 'c='))
|
||||
|
||||
|
||||
def is_selenium_loc(loc):
|
||||
return (isinstance(loc, tuple) and len(loc) == 2 and loc[0].lower() in (
|
||||
'id', 'xpath', 'link text', 'partial link text', 'name', 'tag name', 'class name', 'css selector')
|
||||
and isinstance(loc[1], str))
|
||||
|
||||
|
||||
def get_loc(loc, translate_css=False, css_mode=False):
|
||||
if isinstance(loc, tuple):
|
||||
loc = translate_css_loc(loc) if css_mode else translate_loc(loc)
|
||||
|
||||
elif isinstance(loc, str):
|
||||
loc = str_to_css_loc(loc) if css_mode else str_to_xpath_loc(loc)
|
||||
|
||||
else:
|
||||
raise LocatorError(ALLOW_TYPE=_S._lang.LOC_FORMAT, CURR_VAL=loc)
|
||||
|
||||
if loc[0] == 'css selector' and translate_css:
|
||||
from lxml.cssselect import CSSSelector, ExpressionError
|
||||
try:
|
||||
path = str(CSSSelector(loc[1], translator='html').path)
|
||||
path = path[20:] if path.startswith('descendant-or-self::') else path
|
||||
loc = 'xpath', path
|
||||
except ExpressionError:
|
||||
pass
|
||||
|
||||
return loc
|
||||
|
||||
|
||||
def str_to_xpath_loc(loc):
|
||||
loc_by = 'xpath'
|
||||
loc = _preprocess(loc)
|
||||
|
||||
# 多属性查找
|
||||
if loc.startswith(('@@', '@|', '@!')) and loc not in ('@@', '@|', '@!'):
|
||||
loc_str = _make_multi_xpath_str('*', loc)[1]
|
||||
|
||||
# 单属性查找
|
||||
elif loc.startswith('@') and loc != '@':
|
||||
loc_str = _make_single_xpath_str('*', loc)[1]
|
||||
|
||||
# 根据tag name查找
|
||||
elif loc.startswith(('tag:', 'tag=', 'tag^', 'tag$')) and loc not in ('tag:', 'tag=', 'tag^', 'tag$'):
|
||||
at_ind = loc.find('@')
|
||||
if at_ind == -1:
|
||||
loc_str = f'//*[name()="{loc[4:]}"]'
|
||||
elif loc[at_ind:].startswith(('@@', '@|', '@!')):
|
||||
loc_str = _make_multi_xpath_str(loc[4:at_ind], loc[at_ind:])[1]
|
||||
else:
|
||||
loc_str = _make_single_xpath_str(loc[4:at_ind], loc[at_ind:])[1]
|
||||
|
||||
# 根据文本查找
|
||||
elif loc.startswith('text='):
|
||||
loc_str = f'//*[text()={_quotes_escape(loc[5:])}]'
|
||||
elif loc.startswith('text:') and loc != 'text:':
|
||||
loc_str = f'//*/text()[contains(., {_quotes_escape(loc[5:])})]/..'
|
||||
elif loc.startswith('text^') and loc != 'text^':
|
||||
loc_str = f'//*/text()[starts-with(., {_quotes_escape(loc[5:])})]/..'
|
||||
elif loc.startswith('text$') and loc != 'text$':
|
||||
loc_str = (f'//*/text()[substring(., string-length(.) - string-length({_quotes_escape(loc[5:])}) +1) = '
|
||||
f'{_quotes_escape(loc[5:])}]/..')
|
||||
|
||||
# 用xpath查找
|
||||
elif loc.startswith(('xpath:', 'xpath=')) and loc not in ('xpath:', 'xpath='):
|
||||
loc_str = loc[6:]
|
||||
|
||||
# 用css selector查找
|
||||
elif loc.startswith(('css:', 'css=')) and loc not in ('css:', 'css='):
|
||||
loc_by = 'css selector'
|
||||
loc_str = loc[4:]
|
||||
|
||||
# 根据文本模糊查找
|
||||
elif loc:
|
||||
loc_str = f'//*/text()[contains(., {_quotes_escape(loc)})]/..'
|
||||
else:
|
||||
loc_str = '//*'
|
||||
|
||||
return loc_by, loc_str
|
||||
|
||||
|
||||
def str_to_css_loc(loc):
|
||||
loc_by = 'css selector'
|
||||
loc = _preprocess(loc)
|
||||
|
||||
# 多属性查找
|
||||
if loc.startswith(('@@', '@|', '@!')) and loc not in ('@@', '@|', '@!'):
|
||||
loc_str = _make_multi_css_str('*', loc)[1]
|
||||
|
||||
# 单属性查找
|
||||
elif loc.startswith('@') and loc != '@':
|
||||
loc_by, loc_str = _make_single_css_str('*', loc)
|
||||
|
||||
# 根据tag name查找
|
||||
elif loc.startswith(('tag:', 'tag=', 'tag^', 'tag$')) and loc not in ('tag:', 'tag=', 'tag^', 'tag$'):
|
||||
at_ind = loc.find('@')
|
||||
if at_ind == -1:
|
||||
loc_str = loc[4:]
|
||||
elif loc[at_ind:].startswith(('@@', '@|', '@!')):
|
||||
loc_by, loc_str = _make_multi_css_str(loc[4:at_ind], loc[at_ind:])
|
||||
else:
|
||||
loc_by, loc_str = _make_single_css_str(loc[4:at_ind], loc[at_ind:])
|
||||
|
||||
# 根据文本查找
|
||||
elif loc.startswith(('text=', 'text:', 'text^', 'text$', 'xpath=', 'xpath:')):
|
||||
loc_by, loc_str = str_to_xpath_loc(loc)
|
||||
|
||||
# 用css selector查找
|
||||
elif loc.startswith(('css:', 'css=')) and loc not in ('css:', 'css='):
|
||||
loc_str = loc[4:]
|
||||
|
||||
# 根据文本模糊查找
|
||||
elif loc:
|
||||
loc_by, loc_str = str_to_xpath_loc(loc)
|
||||
|
||||
else:
|
||||
loc_str = '*'
|
||||
|
||||
return loc_by, loc_str
|
||||
|
||||
|
||||
def _make_single_xpath_str(tag: str, text: str) -> tuple:
|
||||
"""生成单属性xpath语句
|
||||
:param tag: 标签名
|
||||
:param text: 待处理的字符串
|
||||
:return: xpath字符串
|
||||
"""
|
||||
arg_list = [] if tag == '*' else [f'name()="{tag}"']
|
||||
arg_str = txt_str = ''
|
||||
|
||||
if text == '@':
|
||||
arg_str = 'not(@*)'
|
||||
|
||||
else:
|
||||
r = split(r'([:=$^])', text, maxsplit=1)
|
||||
len_r = len(r)
|
||||
len_r0 = len(r[0])
|
||||
if len_r == 3 and len_r0 > 1:
|
||||
if r[0] in ('@tag()', '@t()'):
|
||||
arg_str = f'name()="{r[2].lower()}"'
|
||||
else:
|
||||
symbol = r[1]
|
||||
if symbol == '=': # 精确查找
|
||||
arg = 'text()' if r[0] in ('@text()', '@tx()') else r[0]
|
||||
arg_str = f'{arg}={_quotes_escape(r[2])}'
|
||||
|
||||
elif symbol == '^': # 匹配开头
|
||||
if r[0] in ('@text()', '@tx()'):
|
||||
txt_str = f'/text()[starts-with(., {_quotes_escape(r[2])})]/..'
|
||||
arg_str = ''
|
||||
else:
|
||||
arg_str = f"starts-with({r[0]},{_quotes_escape(r[2])})"
|
||||
|
||||
elif symbol == '$': # 匹配结尾
|
||||
if r[0] in ('@text()', '@tx()'):
|
||||
txt_str = (f'/text()[substring(., string-length(.) - string-length('
|
||||
f'{_quotes_escape(r[2])}) +1) = {_quotes_escape(r[2])}]/..')
|
||||
arg_str = ''
|
||||
else:
|
||||
arg_str = (f'substring({r[0]}, string-length({r[0]}) - string-length('
|
||||
f'{_quotes_escape(r[2])}) +1) = {_quotes_escape(r[2])}')
|
||||
|
||||
elif symbol == ':': # 模糊查找
|
||||
if r[0] in ('@text()', '@tx()'):
|
||||
txt_str = f'/text()[contains(., {_quotes_escape(r[2])})]/..'
|
||||
arg_str = ''
|
||||
else:
|
||||
arg_str = f"contains({r[0]},{_quotes_escape(r[2])})"
|
||||
|
||||
else:
|
||||
raise LocatorError(_S._lang.INCORRECT_SIGN_, symbol)
|
||||
|
||||
elif len_r != 3 and len_r0 > 1:
|
||||
if r[0] in ('@tag()', '@t()'):
|
||||
arg_str = ''
|
||||
else:
|
||||
arg_str = 'normalize-space(text())' if r[0] in ('@text()', '@tx()') else f'{r[0]}'
|
||||
|
||||
if arg_str:
|
||||
arg_list.append(arg_str)
|
||||
arg_str = ' and '.join(arg_list)
|
||||
return 'xpath', f'//*[{arg_str}]{txt_str}' if arg_str else f'//*{txt_str}'
|
||||
|
||||
|
||||
def _make_multi_xpath_str(tag: str, text: str) -> tuple:
|
||||
"""生成多属性查找的xpath语句
|
||||
:param tag: 标签名
|
||||
:param text: 待处理的字符串
|
||||
:return: xpath字符串
|
||||
"""
|
||||
arg_list = []
|
||||
args = split(r'(@!|@@|@\|)', text)[1:]
|
||||
if '@@' in args and '@|' in args:
|
||||
raise LocatorError(_S._lang.SYMBOL_CONFLICT, VALUE=text)
|
||||
_and = '@|' not in args
|
||||
tags = [] if tag == '*' else [f'name()="{tag}"']
|
||||
tags_connect = ' or '
|
||||
|
||||
for k in range(0, len(args) - 1, 2):
|
||||
r = split(r'([:=$^])', args[k + 1], maxsplit=1)
|
||||
arg_str = ''
|
||||
len_r = len(r)
|
||||
|
||||
if not r[0]: # 不查询任何属性
|
||||
arg_str = 'not(@*)'
|
||||
|
||||
else:
|
||||
ignore = True if args[k] == '@!' else False # 是否去除某个属性
|
||||
if len_r != 3: # 只有属性名没有属性内容,查询是否存在该属性
|
||||
if r[0] in ('tag()', 't()'):
|
||||
continue
|
||||
arg_str = 'normalize-space(text())' if r[0] in ('text()', 'tx()') else f'@{r[0]}'
|
||||
|
||||
elif len_r == 3: # 属性名和内容都有
|
||||
if r[0] in ('tag()', 't()'):
|
||||
if ignore:
|
||||
tags.append(f'not(name()="{r[2]}")')
|
||||
tags_connect = ' and '
|
||||
else:
|
||||
tags.append(f'name()="{r[2]}"')
|
||||
continue
|
||||
|
||||
symbol = r[1]
|
||||
if r[0] in ('text()', 'tx()'):
|
||||
arg = '.'
|
||||
txt = r[2]
|
||||
else:
|
||||
arg = f'@{r[0]}'
|
||||
txt = r[2]
|
||||
|
||||
if symbol == '=':
|
||||
arg_str = f'{arg}={_quotes_escape(txt)}'
|
||||
|
||||
elif symbol == ':':
|
||||
arg_str = f'contains({arg},{_quotes_escape(txt)})'
|
||||
|
||||
elif symbol == '^':
|
||||
arg_str = f'starts-with({arg},{_quotes_escape(txt)})'
|
||||
|
||||
elif symbol == '$':
|
||||
arg_str = (f'substring({arg}, string-length({arg}) - string-length('
|
||||
f'{_quotes_escape(txt)}) +1) = {_quotes_escape(txt)}')
|
||||
|
||||
else:
|
||||
raise LocatorError(_S._lang.INCORRECT_SIGN_, symbol)
|
||||
|
||||
if arg_str and ignore:
|
||||
arg_str = f'not({arg_str})'
|
||||
|
||||
if arg_str:
|
||||
arg_list.append(arg_str)
|
||||
|
||||
arg_str = ' and '.join(arg_list) if _and else ' or '.join(arg_list)
|
||||
if tags:
|
||||
condition = f' and ({arg_str})' if arg_str else ''
|
||||
arg_str = f'({tags_connect.join(tags)}){condition}'
|
||||
|
||||
return 'xpath', f'//*[{arg_str}]' if arg_str else f'//*'
|
||||
|
||||
|
||||
def _quotes_escape(search_str: str) -> str:
|
||||
"""将"转义,不知何故不能直接用 斜杠 来转义
|
||||
:param search_str: 查询字符串
|
||||
:return: 把"转义后的字符串
|
||||
"""
|
||||
if '"' not in search_str:
|
||||
return f'"{search_str}"'
|
||||
|
||||
parts = search_str.split('"')
|
||||
parts_num = len(parts)
|
||||
search_str = 'concat('
|
||||
|
||||
for key, i in enumerate(parts):
|
||||
search_str += f'"{i}"'
|
||||
search_str += ',' + '\'"\',' if key < parts_num - 1 else ''
|
||||
|
||||
search_str += ',"")'
|
||||
return search_str
|
||||
|
||||
|
||||
def _make_multi_css_str(tag: str, text: str) -> tuple:
|
||||
"""生成多属性查找的css selector语句
|
||||
:param tag: 标签名
|
||||
:param text: 待处理的字符串
|
||||
:return: css selector字符串
|
||||
"""
|
||||
arg_list = []
|
||||
args = split(r'(@!|@@|@\|)', text)[1:]
|
||||
if '@@' in args and '@|' in args:
|
||||
raise LocatorError(_S._lang.SYMBOL_CONFLICT, LOCATOR=text)
|
||||
_and = '@|' not in args
|
||||
|
||||
for k in range(0, len(args) - 1, 2):
|
||||
r = split(r'([:=$^])', args[k + 1], maxsplit=1)
|
||||
if not r[0] or r[0].startswith(('text()', 'tx()')):
|
||||
return _make_multi_xpath_str(tag, text)
|
||||
|
||||
arg_str = ''
|
||||
len_r = len(r)
|
||||
ignore = True if args[k] == '@!' else False # 是否去除某个属性
|
||||
if len_r != 3: # 只有属性名没有属性内容,查询是否存在该属性
|
||||
if r[0] in ('tag()', 't()'):
|
||||
continue
|
||||
arg_str = f'[{r[0]}]'
|
||||
|
||||
elif len_r == 3: # 属性名和内容都有
|
||||
if r[0] in ('tag()', 't()'):
|
||||
if tag == '*':
|
||||
tag = f':not({r[2].lower()})' if ignore else f'{r[2]}'
|
||||
else:
|
||||
tag += f',:not({r[2].lower()})' if ignore else f',{r[2]}'
|
||||
continue
|
||||
|
||||
d = {'=': '', '^': '^', '$': '$', ':': '*'}
|
||||
arg_str = f'[{r[0]}{d[r[1]]}={css_trans(r[2])}]'
|
||||
|
||||
if arg_str and ignore:
|
||||
arg_str = f':not({arg_str})'
|
||||
|
||||
if arg_str:
|
||||
arg_list.append(arg_str)
|
||||
|
||||
if _and:
|
||||
return 'css selector', f'{tag}{"".join(arg_list)}'
|
||||
|
||||
return 'css selector', f'{tag}{("," + tag).join(arg_list)}'
|
||||
|
||||
|
||||
def _make_single_css_str(tag: str, text: str) -> tuple:
|
||||
"""生成单属性css selector语句
|
||||
:param tag: 标签名
|
||||
:param text: 待处理的字符串
|
||||
:return: css selector字符串
|
||||
"""
|
||||
if text == '@' or text.startswith(('@text()', '@tx()')):
|
||||
return _make_single_xpath_str(tag, text)
|
||||
|
||||
r = split(r'([:=$^])', text, maxsplit=1)
|
||||
if r[0] in ('@tag()', '@t()'):
|
||||
return 'css selector', r[2]
|
||||
|
||||
if len(r) == 3:
|
||||
d = {'=': '', '^': '^', '$': '$', ':': '*'}
|
||||
arg_str = f'[{r[0][1:]}{d[r[1]]}={css_trans(r[2])}]'
|
||||
|
||||
else:
|
||||
arg_str = f'[{css_trans(r[0][1:])}]'
|
||||
|
||||
return 'css selector', f'{tag}{arg_str}'
|
||||
|
||||
|
||||
def translate_loc(loc):
|
||||
if len(loc) != 2:
|
||||
raise LocatorError(_S._lang.LOC_LEN, LOCATOR=loc)
|
||||
|
||||
loc_by = By.XPATH
|
||||
loc_0 = loc[0].lower()
|
||||
|
||||
if loc_0 == By.XPATH:
|
||||
loc_str = loc[1]
|
||||
|
||||
elif loc_0 == By.CSS_SELECTOR:
|
||||
loc_by = loc_0
|
||||
loc_str = loc[1]
|
||||
|
||||
elif loc_0 == By.ID:
|
||||
loc_str = f'//*[@id="{loc[1]}"]'
|
||||
|
||||
elif loc_0 == By.CLASS_NAME:
|
||||
loc_str = f'//*[@class="{loc[1]}"]'
|
||||
|
||||
elif loc_0 == By.LINK_TEXT:
|
||||
loc_str = f'//a[text()="{loc[1]}"]'
|
||||
|
||||
elif loc_0 == By.NAME:
|
||||
loc_str = f'//*[@name="{loc[1]}"]'
|
||||
|
||||
elif loc_0 == By.TAG_NAME:
|
||||
loc_str = f'//*[name()="{loc[1]}"]'
|
||||
|
||||
elif loc_0 == By.PARTIAL_LINK_TEXT:
|
||||
loc_str = f'//a[contains(text(),"{loc[1]}")]'
|
||||
|
||||
else:
|
||||
raise LocatorError(_S._lang.INVALID_LOC, LOCATOR=loc)
|
||||
|
||||
return loc_by, loc_str
|
||||
|
||||
|
||||
def translate_css_loc(loc):
|
||||
if len(loc) != 2:
|
||||
raise LocatorError(_S._lang.LOC_LEN, LOCATOR=loc)
|
||||
|
||||
loc_by = By.CSS_SELECTOR
|
||||
loc_0 = loc[0].lower()
|
||||
if loc_0 == By.XPATH:
|
||||
loc_by = By.XPATH
|
||||
loc_str = loc[1]
|
||||
|
||||
elif loc_0 == By.CSS_SELECTOR:
|
||||
loc_by = loc_0
|
||||
loc_str = loc[1]
|
||||
|
||||
elif loc_0 == By.ID:
|
||||
loc_str = f'#{css_trans(loc[1])}'
|
||||
|
||||
elif loc_0 == By.CLASS_NAME:
|
||||
loc_str = f'.{css_trans(loc[1])}'
|
||||
|
||||
elif loc_0 == By.LINK_TEXT:
|
||||
loc_by = By.XPATH
|
||||
loc_str = f'//a[text()="{css_trans(loc[1])}"]'
|
||||
|
||||
elif loc_0 == By.NAME:
|
||||
loc_str = f'*[@name={css_trans(loc[1])}]'
|
||||
|
||||
elif loc_0 == By.TAG_NAME:
|
||||
loc_str = loc[1]
|
||||
|
||||
elif loc_0 == By.PARTIAL_LINK_TEXT:
|
||||
loc_by = By.XPATH
|
||||
loc_str = f'//a[contains(text(),"{loc[1]}")]'
|
||||
|
||||
else:
|
||||
raise LocatorError(_S._lang.INVALID_LOC, LOCATOR=loc)
|
||||
|
||||
return loc_by, loc_str
|
||||
|
||||
|
||||
def css_trans(txt):
|
||||
c = ('!', '"', '#', '$', '%', '&', '\'', '(', ')', '*', '+', ',', '-', '.', '/', ':', ';', '<', '=', '>', '?', '@',
|
||||
'[', '\\', ']', '^', '`', ',', '{', '|', '}', '~', ' ')
|
||||
return ''.join([fr'\{i}' if i in c else i for i in txt])
|
||||
|
||||
|
||||
def _preprocess(loc):
|
||||
"""对缩写进行处理,替换回完整写法"""
|
||||
if loc.startswith('.'):
|
||||
if loc.startswith(('.=', '.:', '.^', '.$')):
|
||||
loc = loc.replace('.', '@class', 1)
|
||||
else:
|
||||
loc = loc.replace('.', '@class=', 1)
|
||||
|
||||
elif loc.startswith('#'):
|
||||
if loc.startswith(('#=', '#:', '#^', '#$')):
|
||||
loc = loc.replace('#', '@id', 1)
|
||||
else:
|
||||
loc = loc.replace('#', '@id=', 1)
|
||||
|
||||
elif loc.startswith(('t:', 't=')):
|
||||
loc = f'tag:{loc[2:]}'
|
||||
|
||||
elif loc.startswith(('tx:', 'tx=', 'tx^', 'tx$')):
|
||||
loc = f'text{loc[2:]}'
|
||||
|
||||
elif loc.startswith(('c:', 'c=')):
|
||||
loc = f'css:{loc[2:]}'
|
||||
|
||||
elif loc.startswith(('x:', 'x=')):
|
||||
loc = f'xpath:{loc[2:]}'
|
||||
|
||||
return loc
|
||||
@@ -0,0 +1,78 @@
|
||||
# -*- coding:utf-8 -*-
|
||||
"""
|
||||
@Author : g1879
|
||||
@Contact : g1879@qq.com
|
||||
@Website : https://DrissionPage.cn
|
||||
@Copyright: (c) 2020 by g1879, Inc. All Rights Reserved.
|
||||
"""
|
||||
from typing import Union
|
||||
|
||||
|
||||
def locator_to_tuple(loc: str) -> dict:
|
||||
"""解析定位字符串生成dict格式数据
|
||||
:param loc: 待处理的字符串
|
||||
:return: 格式: {'and': bool, 'args': ['属性名称', '匹配方式', '属性值', 是否否定]}
|
||||
"""
|
||||
...
|
||||
|
||||
|
||||
def is_str_loc(text: str) -> bool:
|
||||
"""返回text是否定位符"""
|
||||
...
|
||||
|
||||
|
||||
def is_selenium_loc(loc: tuple) -> bool:
|
||||
"""返回tuple是否selenium的定位符"""
|
||||
...
|
||||
|
||||
|
||||
def get_loc(loc: Union[tuple, str],
|
||||
translate_css: bool = False,
|
||||
css_mode: bool = False) -> tuple:
|
||||
"""接收本库定位语法或selenium定位元组,转换为标准定位元组,可翻译css selector为xpath
|
||||
:param loc: 本库定位语法或selenium定位元组
|
||||
:param translate_css: 是否翻译css selector为xpath,用于相对定位
|
||||
:param css_mode: 是否尽量用css selector方式
|
||||
:return: DrissionPage定位元组
|
||||
"""
|
||||
...
|
||||
|
||||
|
||||
def str_to_xpath_loc(loc: str) -> tuple:
|
||||
"""处理元素查找语句
|
||||
:param loc: 查找语法字符串
|
||||
:return: 匹配符元组
|
||||
"""
|
||||
...
|
||||
|
||||
|
||||
def str_to_css_loc(loc: str) -> tuple:
|
||||
"""处理元素查找语句
|
||||
:param loc: 查找语法字符串
|
||||
:return: 匹配符元组
|
||||
"""
|
||||
...
|
||||
|
||||
|
||||
def translate_loc(loc: tuple) -> tuple:
|
||||
"""把By类型的loc元组转换为css selector或xpath类型的
|
||||
:param loc: By类型的loc元组
|
||||
:return: css selector或xpath类型的loc元组
|
||||
"""
|
||||
...
|
||||
|
||||
|
||||
def translate_css_loc(loc: tuple) -> tuple:
|
||||
"""把By类型的loc元组转换为css selector或xpath类型的
|
||||
:param loc: By类型的loc元组
|
||||
:return: css selector或xpath类型的loc元组
|
||||
"""
|
||||
...
|
||||
|
||||
|
||||
def css_trans(txt: str) -> str:
|
||||
"""css字符串中特殊字符转义
|
||||
:param txt: 要处理的文本
|
||||
:return: 处理后的文本
|
||||
"""
|
||||
...
|
||||
@@ -0,0 +1,67 @@
|
||||
# -*- coding:utf-8 -*-
|
||||
"""
|
||||
@Author : g1879
|
||||
@Contact : g1879@qq.com
|
||||
@Website : https://DrissionPage.cn
|
||||
@Copyright: (c) 2020 by g1879, Inc. All Rights Reserved.
|
||||
"""
|
||||
from pathlib import Path
|
||||
|
||||
from .texts import get_txt_class
|
||||
|
||||
|
||||
class Settings(object):
|
||||
raise_when_ele_not_found = False
|
||||
raise_when_click_failed = False
|
||||
raise_when_wait_failed = False
|
||||
singleton_tab_obj = True
|
||||
cdp_timeout = 30
|
||||
browser_connect_timeout = 30
|
||||
auto_handle_alert = None
|
||||
_lang = get_txt_class(None)
|
||||
suffixes_list = str(Path(__file__).parent.absolute() / 'suffixes.dat').replace('\\', '/')
|
||||
|
||||
@classmethod
|
||||
def set_raise_when_ele_not_found(cls, on_off=True):
|
||||
cls.raise_when_ele_not_found = on_off
|
||||
return cls
|
||||
|
||||
@classmethod
|
||||
def set_raise_when_click_failed(cls, on_off=True):
|
||||
cls.raise_when_click_failed = on_off
|
||||
return cls
|
||||
|
||||
@classmethod
|
||||
def set_raise_when_wait_failed(cls, on_off=True):
|
||||
cls.raise_when_wait_failed = on_off
|
||||
return cls
|
||||
|
||||
@classmethod
|
||||
def set_singleton_tab_obj(cls, on_off=True):
|
||||
cls.singleton_tab_obj = on_off
|
||||
return cls
|
||||
|
||||
@classmethod
|
||||
def set_cdp_timeout(cls, second):
|
||||
cls.cdp_timeout = second
|
||||
return cls
|
||||
|
||||
@classmethod
|
||||
def set_browser_connect_timeout(cls, second):
|
||||
cls.browser_connect_timeout = second
|
||||
return cls
|
||||
|
||||
@classmethod
|
||||
def set_auto_handle_alert(cls, accept=True):
|
||||
cls.auto_handle_alert = accept
|
||||
return cls
|
||||
|
||||
@classmethod
|
||||
def set_language(cls, code):
|
||||
cls._lang = get_txt_class(code)
|
||||
return cls
|
||||
|
||||
@classmethod
|
||||
def set_suffixes_list(cls, path):
|
||||
cls.suffixes_list = str(Path(path).absolute()).replace('\\', '/')
|
||||
return cls
|
||||
@@ -0,0 +1,95 @@
|
||||
# -*- coding:utf-8 -*-
|
||||
"""
|
||||
@Author : g1879
|
||||
@Contact : g1879@qq.com
|
||||
@Website : https://DrissionPage.cn
|
||||
@Copyright: (c) 2020 by g1879, Inc. All Rights Reserved.
|
||||
"""
|
||||
from pathlib import Path
|
||||
from typing import Optional, Union, Literal
|
||||
|
||||
from .texts import Texts
|
||||
|
||||
|
||||
class Settings(object):
|
||||
raise_when_ele_not_found: bool = ...
|
||||
raise_when_click_failed: bool = ...
|
||||
raise_when_wait_failed: bool = ...
|
||||
singleton_tab_obj: bool = ...
|
||||
cdp_timeout: float = ...
|
||||
browser_connect_timeout: float = ...
|
||||
auto_handle_alert: Optional[bool] = ...
|
||||
_lang: Texts = ...
|
||||
suffixes_list: str = ...
|
||||
|
||||
@classmethod
|
||||
def set_raise_when_ele_not_found(cls, on_off: bool = True) -> Settings:
|
||||
"""设置找不到元素时是否立即抛出异常
|
||||
:param on_off: bool表示开或关
|
||||
:return: None
|
||||
"""
|
||||
...
|
||||
|
||||
@classmethod
|
||||
def set_raise_when_click_failed(cls, on_off: bool = True) -> Settings:
|
||||
"""设置点击失败时是否立即抛出异常
|
||||
:param on_off: bool表示开或关
|
||||
:return: None
|
||||
"""
|
||||
...
|
||||
|
||||
@classmethod
|
||||
def set_raise_when_wait_failed(cls, on_off: bool = True) -> Settings:
|
||||
"""设置等待失败时是否立即抛出异常
|
||||
:param on_off: bool表示开或关
|
||||
:return: None
|
||||
"""
|
||||
...
|
||||
|
||||
@classmethod
|
||||
def set_singleton_tab_obj(cls, on_off: bool = True) -> Settings:
|
||||
"""设置是否开启tab单例模式
|
||||
:param on_off: bool表示开或关
|
||||
:return: None
|
||||
"""
|
||||
...
|
||||
|
||||
@classmethod
|
||||
def set_cdp_timeout(cls, second: float) -> Settings:
|
||||
"""设置csp执行超时时间(秒)
|
||||
:param second: 超时秒数
|
||||
:return: None
|
||||
"""
|
||||
...
|
||||
|
||||
@classmethod
|
||||
def set_browser_connect_timeout(cls, second: float) -> Settings:
|
||||
"""设置等待浏览器连接超时时间(秒)
|
||||
:param second: 超时秒数
|
||||
:return: None
|
||||
"""
|
||||
...
|
||||
|
||||
@classmethod
|
||||
def set_auto_handle_alert(cls, accept: Optional[bool] = True) -> Settings:
|
||||
"""设置是否自动处理js弹出信息
|
||||
:param accept: None为不处理,True为接受,False为取消
|
||||
:return: None
|
||||
"""
|
||||
...
|
||||
|
||||
@classmethod
|
||||
def set_language(cls, code: Literal['zh_cn', 'en']) -> Settings:
|
||||
"""设置报错和提示信息使用的语言
|
||||
:param code: 表示语言的文本'zh_cn', 'en'
|
||||
:return: None
|
||||
"""
|
||||
...
|
||||
|
||||
@classmethod
|
||||
def set_suffixes_list(cls, path: Union[str, Path]) -> Settings:
|
||||
"""设置用于识别域名后缀的文件路径
|
||||
:param path: 文件路径
|
||||
:return: None
|
||||
"""
|
||||
...
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,397 @@
|
||||
# -*- coding:utf-8 -*-
|
||||
"""
|
||||
@Author : g1879
|
||||
@Contact : g1879@qq.com
|
||||
@Website : https://DrissionPage.cn
|
||||
@Copyright: (c) 2020 by g1879, Inc. All Rights Reserved.
|
||||
"""
|
||||
from locale import getlocale
|
||||
|
||||
from ..version import __version__
|
||||
|
||||
|
||||
def get_txt_class(lang=None):
|
||||
languages = {
|
||||
'zh_cn': Texts,
|
||||
'cn': Texts,
|
||||
'en': English,
|
||||
}
|
||||
if lang is None:
|
||||
locale = str(getlocale()[0]).lower()
|
||||
if locale.startswith('zh') or 'chinese' in locale:
|
||||
lang = 'zh_cn'
|
||||
elif locale.startswith('en') or 'english' in locale:
|
||||
lang = 'en'
|
||||
else:
|
||||
lang = 'zh_cn'
|
||||
else:
|
||||
lang = lang.lower()
|
||||
lang = languages.get(lang, None)
|
||||
if lang is None:
|
||||
raise ValueError(f'lang must be one of {languages.keys()}')
|
||||
return lang
|
||||
|
||||
|
||||
class Texts(object):
|
||||
# --------- 参数名 ---------
|
||||
VERSION = '版本'
|
||||
INFO = '详情'
|
||||
METHOD = '方法'
|
||||
ARGS = '参数'
|
||||
ARG = '参数'
|
||||
BROWSER_VER = '浏览器版本'
|
||||
PATH = '路径'
|
||||
VALUE = '值'
|
||||
ALLOW_VAL = '允许值'
|
||||
CURR_VAL = '当前值'
|
||||
ALLOW_TYPE = '允许类型'
|
||||
CURR_TYPE = '当前类型'
|
||||
TIP = '提示'
|
||||
ADDRESS = '地址'
|
||||
LOCATOR = '定位符'
|
||||
ALL_TABS = '所有标签页'
|
||||
|
||||
# --------- 异常默认文本 ---------
|
||||
ELEMENTNOTFOUNDERROR = '没有找到元素。'
|
||||
ALERTEXISTSERROR = '存在未处理的提示框。'
|
||||
CONTEXTLOSTERROR = '页面被刷新,请操作前尝试等待页面刷新或加载完成。'
|
||||
ELEMENTLOSTERROR = '元素对象已失效。可能是页面整体刷新,或js局部刷新把元素替换或去除了。'
|
||||
CDPERROR = '方法调用错误。'
|
||||
PAGEDISCONNECTEDERROR = '与页面的连接已断开。'
|
||||
JAVASCRIPTERROR = 'JavaScript运行错误。'
|
||||
NORECTERROR = '该元素没有位置及大小。'
|
||||
BROWSERCONNECTERROR = '浏览器连接失败。'
|
||||
NORESOURCEERROR = '该元素无可保存的内容或保存失败。'
|
||||
CANNOTCLICKERROR = '该元素无法滚动到视口或被遮挡,无法点击。'
|
||||
GETDOCUMENTERROR = '获取文档失败。'
|
||||
WAITTIMEOUTERROR = '等待失败。'
|
||||
INCORRECTURLERROR = '无效的url。'
|
||||
LOCATORERROR = '定位符格式不正确。',
|
||||
STORAGEERROR = '无法操作当前存储数据。'
|
||||
COOKIEFORMATERROR = 'cookie格式不正确。'
|
||||
TARGETNOTFOUNDERROR = '找不到指定页面。'
|
||||
|
||||
# --------- 异常信息 ---------
|
||||
NO_AVAILABLE_PORT_FOUND = '未找到可用端口。'
|
||||
WIN_SYS_ONLY = '该方法只能在Windows系统使用。'
|
||||
NEED_LIB_ = '请先安装{}。'
|
||||
INVALID_URL = '无效的url,也许要加上"http://"?'
|
||||
INVALID_HEADER_NAME = '无效的header项名。'
|
||||
NOT_A_FUNCTION = '传入的js无法解析成函数。'
|
||||
METHOD_NOT_FOUND = '没有找到对应功能,方法错误或你的浏览器太旧。'
|
||||
NO_RESPONSE = '超时,可能是浏览器卡了。'
|
||||
UNKNOWN_ERR = '出现未知错误。'
|
||||
FEEDBACK = ('出现这个错误可能意味着程序有bug,请把错误信息和重现方法告知作者,谢谢。\n'
|
||||
'报告网站: https://gitee.com/g1879/DrissionPage/issues')
|
||||
INI_NOT_FOUND = 'ini文件不存在。'
|
||||
EXT_NOT_FOUND = '插件路径不存在。'
|
||||
WAITING_FAILED_ = '等待{}失败(等待{}秒)。'
|
||||
GET_OBJ_FAILED = '获取对象失败。'
|
||||
INCORRECT_VAL_ = '{}参数值错误。'
|
||||
INCORRECT_TYPE_ = '{}参数类型错误。'
|
||||
CONNECT_ERR = '连接异常。'
|
||||
BROWSER_CONNECT_ERR1_ = '浏览器连接失败,请检查{}端口是否浏览器,且已添加"--remote-debugging-port={}"启动项。'
|
||||
BROWSER_CONNECT_ERR2 = '浏览器连接失败,请确认浏览器已启动。'
|
||||
BROWSER_EXE_NOT_FOUND = '无法找到浏览器可执行文件路径,请手动配置。'
|
||||
BROWSER_NOT_FOUND = '未找到浏览器。'
|
||||
BROWSER_NOT_EXIST = '浏览器未开启或已关闭。'
|
||||
BROWSER_DISCONNECTED = '浏览器已关闭或链接已断开。'
|
||||
BROWSER_NOT_FOR_CONTROL = '浏览器版本太旧或此浏览器不支持接管。'
|
||||
UNSUPPORTED_CSS_SYNTAX = '此css selector语法不受支持,请换成xpath。'
|
||||
UNSUPPORTED_ARG_TYPE_ = '不支持参数{}的类型: {}。'
|
||||
UPGRADE_WS = '请升级websocket-client库。'
|
||||
INI_NOT_SET = 'ini_path未设置。'
|
||||
INVALID_XPATH_ = '无效的xpath语句: {}'
|
||||
INVALID_CSS_ = '无效的css selector语句: {}'
|
||||
INDEX_FORMAT = '序号必须是数字或切片。'
|
||||
LOC_NOT_FOR_FRAME = '该定位符不是指向frame元素。'
|
||||
NEED_DOWNLOAD_PATH = '此功能需显式设置下载路径。'
|
||||
GET_WINDOW_SIZE_FAILED = '获取窗口信息失败。'
|
||||
SET_FAILED_ = '{}设置失败。'
|
||||
NOT_LISTENING = '监听未启动或已停止。'
|
||||
NOT_BLOB = '该链接非blob类型。'
|
||||
CANNOT_INPUT_FILE = '该输入框无法接管,请改用对<input>元素输入路径的方法设置。'
|
||||
NO_SUCH_KEY_ = '没有这个按键: {}'
|
||||
NO_NEW_TAB = '没有等到新标签页。'
|
||||
NO_SUCH_TAB = '没有找到指定标签页。'
|
||||
NEED_DOMAIN = '需设置domain或url值。如设置url值,需以http开头。'
|
||||
NEED_DOMAIN2 = 'cookie必须带有"domain"或"url"字段。'
|
||||
NEED_ARG_ = '{}必须设置。'
|
||||
SAVE_PATH_MUST_BE_FOLDER = 'save_path必须为文件夹。'
|
||||
GET_PDF_FAILED = '保存失败,可能浏览器版本不支持。'
|
||||
GET_BLOB_FAILED = '无法获取该资源。'
|
||||
NO_SRC_ATTR = '元素没有src值或该值为空。'
|
||||
D_MODE_ONLY = 'url、domain、path参数只有d模式下有效。'
|
||||
S_MODE_ONLY = '以下参数在s模式下才会生效:'
|
||||
STATUS_CODE_ = '状态码: {}'
|
||||
TAB_OBJ_EXISTS = '该标签页已有非MixTab版本,如需多对象共用标签页请设置Settings.set_singleton_tab_obj(False)。'
|
||||
ONLY_ENGLISH = '转换成视频仅支持英文路径和文件名。'
|
||||
SELECT_ONLY = 'select方法只能在<select>元素使用。'
|
||||
MULTI_SELECT_ONLY = '只能在多选菜单执行此操作。'
|
||||
OPTION_NOT_FOUND = '没有找到指定选项。'
|
||||
STR_FOR_SINGLE_SELECT = '单选列表只能传入str格式。'
|
||||
JS_RUNTIME_ERR = 'js运行环境出错。'
|
||||
TIMEOUT_ = '{}超时(等待{}秒)'
|
||||
JS_RESULT_ERR = 'js结果解析错误。'
|
||||
S_MODE_GET_FAILED = 's模式访问失败,请设置go=False,自行构造连接参数进行访问。'
|
||||
ZERO_PAGE_SIZE = '页面大小为0,请尝试等待页面加载完成。'
|
||||
CONTENT_IS_EMPTY = '返回内容为空。'
|
||||
FIND_ELE_ERR = '查找元素异常。'
|
||||
SYMBOL_CONFLICT = '@@和@|不能同时出现在一个定位语句中。'
|
||||
INVALID_LOC = '无法识别的定位符。'
|
||||
LOC_LEN = '定位符长度必须为2。'
|
||||
INCORRECT_SIGN_ = '符号不正确: {}'
|
||||
DOMAIN_NOT_SET = '未设置域名,请设置cookie的domain参数或先访问一个网站。'
|
||||
PLUGIN_NEED_FOLDER = '插件需解压为文件夹:{}'
|
||||
NEED_FILES_OR_TEXT_ARG = 'files参数和text参数必须至少输入一个。'
|
||||
|
||||
# --------- 参数内容 ---------
|
||||
ELE_DISPLAYED = '元素显示'
|
||||
ELE_LOADED = '元素加载'
|
||||
PAGE_LOADED = '页面加载'
|
||||
NEW_TAB = '新标签页'
|
||||
DATA_PACKET = '数据包'
|
||||
ELE_HIDDEN_DEL = '元素隐藏或被删除'
|
||||
ELE_DEL = '元素被删除'
|
||||
ELE_HAS_RECT = '元素拥有大小及位置'
|
||||
ELE_HIDDEN = '元素隐藏'
|
||||
ELE_CLICKABLE = '元素可点击'
|
||||
ELE_COVERED = '元素被覆盖'
|
||||
ELE_NOT_COVERED = '元素不被覆盖'
|
||||
ELE_AVAILABLE = '元素可用'
|
||||
ELE_NOT_AVAILABLE = '元素不可用'
|
||||
ELE_STOP_MOVING = '元素停止运动'
|
||||
ELE_STATE_CHANGED_ = '等待元素状态改变失败(等待{}秒)。'
|
||||
RUN_BY_ADMIN = '尝试用管理员权限运行。'
|
||||
BROWSER_CONNECT_ERR_INFO = ('\n1、用户文件夹没有和已打开的浏览器冲突\n'
|
||||
'2、如为无界面系统,请添加\'--headless=new\'启动参数\n'
|
||||
'3、如果是Linux系统,尝试添加\'--no-sandbox\'启动参数\n'
|
||||
'可使用ChromiumOptions设置端口和用户文件夹路径。')
|
||||
# ALLOW
|
||||
LOC_OR_IND = '定位符(str或长度为2的tuple)或序号'
|
||||
STR_ONLY = 'str格式且不支持xpath和css形式'
|
||||
LOC_FORMAT = 'str或长度为2的tuple'
|
||||
ELE_OR_LOC = '定位符(str或长度为2的tuple)或元素'
|
||||
FRAME_LOC_FORMAT = '定位符、iframe序号、id、name、ChromiumFrame对象'
|
||||
SET_DOWNLOAD_PATH = '使用set.download_path()方法、配置对象或ini文件均可。'
|
||||
SET_WINDOW_NORMAL = '浏览器全屏或最小化状态时请先调用set.window.normal()恢复正常状态。'
|
||||
HTML_ELE_TYPE = '元素、页面对象或html文本'
|
||||
ELE_LOC_FORMAT = '(x, y)格式坐标,或ChromiumElement对象'
|
||||
IP_OR_OPTIONS = 'ip:port格式字符串或ChromiumOptions类型'
|
||||
|
||||
TAB_OR_ID = '标签页对象或id'
|
||||
RUN_JS = '执行js'
|
||||
PAGE_CONNECT = '页面连接'
|
||||
NEW_ELE_INFO = 'str格式的html文本,或tuple格式(tag, {name: value})。'
|
||||
DICT_TO_NEW_ELE = '此网页不支持html格式新建元素,请用dict传入html_or_info参数。'
|
||||
|
||||
# --------- print ---------
|
||||
RETRY = '重试'
|
||||
OPTIONS_HAVE_SAVED = '配置已保存到文件'
|
||||
AUTO_LOAD_TIP = '以后程序可自动从文件加载配置'
|
||||
STOP_RECORDING = '停止录制'
|
||||
START_RECORD = '开始录制'
|
||||
CHOOSE_RECORD_TARGET = '请手动选择要录制的目标。'
|
||||
UNSUPPORTED_USER_PROXY = '你似乎在设置使用账号密码的代理,暂时不支持这种代理,可自行用插件实现需求。'
|
||||
UNSUPPORTED_SOCKS_PROXY = '你似乎在设置使用socks代理,暂时不支持这种代理,可自行用插件实现需求。'
|
||||
NOT_SUPPORT_DOWNLOAD = '浏览器版本太低无法使用下载管理功能。'
|
||||
FILE_NAME = '文件名'
|
||||
FOLDER_PATH = '目录路径'
|
||||
UNKNOWN = '未知'
|
||||
DOWNLOAD_COMPLETED = '下载完成'
|
||||
COMPLETED_AND_RENAME = '完成并重命名'
|
||||
OVERWROTE = '已覆盖'
|
||||
DOWNLOAD_CANCELED = '下载取消'
|
||||
SKIPPED = '已跳过'
|
||||
|
||||
@classmethod
|
||||
def get(cls, item):
|
||||
return getattr(cls, item, item) if isinstance(item, str) and item.isupper() else item
|
||||
|
||||
@classmethod
|
||||
def join(cls, *args, **kwargs):
|
||||
kwargs['VERSION'] = __version__
|
||||
main = ('\n' + args[0].format(*[i for i in args[1:]])) if args else ''
|
||||
msg = ('\n' + '\n'.join([f'{cls.get(k)}: {v}' for k, v in kwargs.items()]))
|
||||
return f'{main}{msg}'
|
||||
|
||||
|
||||
class English(Texts):
|
||||
# --------- 参数名 ---------
|
||||
VERSION = 'Version'
|
||||
INFO = 'Information'
|
||||
METHOD = 'Method'
|
||||
ARGS = 'Arguments'
|
||||
ARG = 'Argument'
|
||||
BROWSER_VER = 'Browser Version'
|
||||
PATH = 'Path'
|
||||
VALUE = 'Value'
|
||||
ALLOW_VAL = 'Allow Value'
|
||||
CURR_VAL = 'Current Value'
|
||||
ALLOW_TYPE = 'Allow Type'
|
||||
CURR_TYPE = 'Current Type'
|
||||
TIP = 'Tip'
|
||||
ADDRESS = 'Address'
|
||||
LOCATOR = 'Locator'
|
||||
ALL_TABS = 'All Tabs'
|
||||
|
||||
# --------- 异常默认文本 ---------
|
||||
ELEMENTNOTFOUNDERROR = 'No element found.'
|
||||
ALERTEXISTSERROR = 'An unprocessed dialog box exists.'
|
||||
CONTEXTLOSTERROR = 'The page is refreshed. Please wait until the page is refreshed or loaded.'
|
||||
ELEMENTLOSTERROR = ('The element object is invalid. This may be an overall refresh of the page, or a partial js '
|
||||
'refresh to replace or remove elements.')
|
||||
CDPERROR = 'Method call error.'
|
||||
PAGEDISCONNECTEDERROR = 'The connection to the page has been disconnected.'
|
||||
JAVASCRIPTERROR = 'JavaScript running error.'
|
||||
NORECTERROR = 'This element has no location or size.'
|
||||
BROWSERCONNECTERROR = 'The browser connection fails.'
|
||||
NORESOURCEERROR = 'The element has no saved content or failed to save.'
|
||||
CANNOTCLICKERROR = 'The element does not scroll to the viewport or is blocked and cannot be clicked.'
|
||||
GETDOCUMENTERROR = 'Failed to obtain the document. Procedure'
|
||||
WAITTIMEOUTERROR = 'Wait for failure.'
|
||||
INCORRECTURLERROR = 'Invalid url.'
|
||||
LOCATORERROR = 'Invalid locator format.',
|
||||
STORAGEERROR = 'Cannot manipulate the currently stored data.'
|
||||
COOKIEFORMATERROR = 'The cookie format is incorrect.'
|
||||
TARGETNOTFOUNDERROR = 'The specified page cannot be found.'
|
||||
PLUGIN_NEED_FOLDER = 'The plugin needs to be decompressed into a folder:{}'
|
||||
NEED_FILES_OR_TEXT_ARG = 'At least one of the "files" parameter and the "text" parameter must be entered.'
|
||||
|
||||
# --------- 异常信息 ---------
|
||||
NO_AVAILABLE_PORT_FOUND = 'No available port found.'
|
||||
WIN_SYS_ONLY = 'This method can be used only on Windows.'
|
||||
NEED_LIB_ = 'Please install {} first.'
|
||||
INVALID_URL = 'Invalid url, maybe add "http://"?'
|
||||
INVALID_HEADER_NAME = 'Invalid header name.'
|
||||
NOT_A_FUNCTION = 'The passed js cannot be parsed into a function.'
|
||||
METHOD_NOT_FOUND = 'The function is not found, the method is wrong or your browser is too old.'
|
||||
NO_RESPONSE = 'Time out. Maybe the browser is stuck.'
|
||||
UNKNOWN_ERR = 'An unknown error occurred.'
|
||||
FEEDBACK = ('This error may mean that there is a bug in the program, please inform the author of the error '
|
||||
'message and how to reproduce it, thank you.\n'
|
||||
'Report url: https://gitee.com/g1879/DrissionPage/issues')
|
||||
INI_NOT_FOUND = 'The ini file does not exist.'
|
||||
EXT_NOT_FOUND = 'The plug-in path does not exist.'
|
||||
WAITING_FAILED_ = 'Wait for {} failed ({} seconds).'
|
||||
GET_OBJ_FAILED = 'Failed to obtain the object. Procedure'
|
||||
INCORRECT_VAL_ = 'The {} parameter value is incorrect.'
|
||||
INCORRECT_TYPE_ = 'The {} parameter type is incorrect.'
|
||||
CONNECT_ERR = 'The connection is abnormal.'
|
||||
BROWSER_CONNECT_ERR1_ = ('Browser connect failed, check whether the port {} is a browser and '
|
||||
'"--remote-debugging-port={}" startup item is added.')
|
||||
BROWSER_CONNECT_ERR2 = 'The browser connection failed. Please ensure that the browser is started.'
|
||||
BROWSER_EXE_NOT_FOUND = 'The browser executable file path cannot be found. Please configure it manually.'
|
||||
BROWSER_NOT_FOUND = 'Browser not found.'
|
||||
BROWSER_NOT_EXIST = 'The browser is not started or closed.'
|
||||
BROWSER_DISCONNECTED = 'The browser is closed or the link is broken.'
|
||||
BROWSER_NOT_FOR_CONTROL = 'The browser version is too old or this browser does not support takeover.'
|
||||
UNSUPPORTED_CSS_SYNTAX = 'This css selector syntax is not supported, please replace it with xpath.'
|
||||
UNSUPPORTED_ARG_TYPE_ = 'The type of parameter {} is not supported: {}.'
|
||||
UPGRADE_WS = 'Upgrade the websocket-client library.'
|
||||
INI_NOT_SET = 'ini_path is not set.'
|
||||
INVALID_XPATH_ = 'Invalid xpath statement: {}'
|
||||
INVALID_CSS_ = 'Invalid css selector statement: {}'
|
||||
INDEX_FORMAT = 'The serial number must be a number or slice.'
|
||||
LOC_NOT_FOR_FRAME = 'This locator does not point to the frame element.'
|
||||
NEED_DOWNLOAD_PATH = 'You need to explicitly set the download path for this function.'
|
||||
GET_WINDOW_SIZE_FAILED = 'Failed to obtain window information. Procedure'
|
||||
SET_FAILED_ = 'The argument {} setting failed.'
|
||||
NOT_LISTENING = 'Listening is not started or stopped.'
|
||||
NOT_BLOB = 'The link is not of blob type.'
|
||||
CANNOT_INPUT_FILE = 'This input field cannot handle. Instead, set the input path to the <input> element.'
|
||||
NO_SUCH_KEY_ = 'There is no button: {}'
|
||||
NO_NEW_TAB = 'Failed to wait for new tab.'
|
||||
NO_SUCH_TAB = 'The specified tab was not found.'
|
||||
NEED_DOMAIN = 'You need to set a domain or url value. If the url value is set, it must start with http.'
|
||||
NEED_DOMAIN2 = 'The cookie must have a "domain" or "url" field.'
|
||||
NEED_ARG_ = '{} must be set.'
|
||||
SAVE_PATH_MUST_BE_FOLDER = 'save_path must be a folder.'
|
||||
GET_PDF_FAILED = 'The save fails because the browser version may not support it.'
|
||||
GET_BLOB_FAILED = 'The resource cannot be retrieved.'
|
||||
NO_SRC_ATTR = 'The element does not have a src value or the value is empty.'
|
||||
D_MODE_ONLY = 'The url, domain, and path parameters are valid only in d mode.'
|
||||
S_MODE_ONLY = 'The following parameters take effect only in s mode:'
|
||||
STATUS_CODE_ = 'Status Code: {}'
|
||||
TAB_OBJ_EXISTS = ('There is already a non-mixtab version of this tab. If multiple objects are common, '
|
||||
'use Settings.set_singleton_tab_obj(False).')
|
||||
ONLY_ENGLISH = 'Only English path and file name are supported when converting to video.'
|
||||
SELECT_ONLY = 'The select method can only be used on <select> elements.'
|
||||
MULTI_SELECT_ONLY = 'You can only do this from the multiple select element.'
|
||||
OPTION_NOT_FOUND = 'The specified option was not found.'
|
||||
STR_FOR_SINGLE_SELECT = 'Single-choice select element can only be passed in str format.'
|
||||
JS_RUNTIME_ERR = 'The js runtime environment is faulty.'
|
||||
TIMEOUT_ = 'Wait for {} timeout ({} seconds).'
|
||||
JS_RESULT_ERR = 'js result parsing error.'
|
||||
S_MODE_GET_FAILED = 'S mode access fails, please set go=False and construct connection parameters for access.'
|
||||
ZERO_PAGE_SIZE = 'The page size is 0, please try to wait for the page to load.'
|
||||
CONTENT_IS_EMPTY = 'The returned content is empty.'
|
||||
FIND_ELE_ERR = 'Find element exceptions.'
|
||||
SYMBOL_CONFLICT = '"@@" and "@|" cannot appear in the same location statement.'
|
||||
INVALID_LOC = 'Unrecognized locator.'
|
||||
LOC_LEN = 'The length of the locator must be 2.'
|
||||
INCORRECT_SIGN_ = 'Incorrect symbol: {}'
|
||||
DOMAIN_NOT_SET = 'No domain name is set, please set the domain parameter of the cookie or visit a website first.'
|
||||
|
||||
# --------- 参数内容 ---------
|
||||
ELE_DISPLAYED = 'element display'
|
||||
ELE_LOADED = 'element loaded'
|
||||
PAGE_LOADED = 'page loaded'
|
||||
NEW_TAB = 'new tab'
|
||||
DATA_PACKET = 'data packet'
|
||||
ELE_HIDDEN_DEL = 'element is hidden or deleted'
|
||||
ELE_DEL = 'element be deleted'
|
||||
ELE_HAS_RECT = 'element has size and position'
|
||||
ELE_HIDDEN = 'element hidden'
|
||||
ELE_CLICKABLE = 'element clickable'
|
||||
ELE_COVERED = 'element is covered'
|
||||
ELE_NOT_COVERED = 'element is not covered'
|
||||
ELE_AVAILABLE = 'element available'
|
||||
ELE_NOT_AVAILABLE = 'element not available'
|
||||
ELE_STOP_MOVING = 'element stop moving'
|
||||
ELE_STATE_CHANGED_ = 'Failed to wait for element state change ({} seconds).'
|
||||
RUN_BY_ADMIN = 'Try to run with administrator rights.'
|
||||
BROWSER_CONNECT_ERR_INFO = ('\n1, the user folder does not conflict with the open browser \n'
|
||||
'2, if no interface system, please add \'--headless=new\' startup parameter \n'
|
||||
'3, if the system is Linux, try adding \'--no-sandbox\' boot parameter \n'
|
||||
'The port and user folder paths can be set using ChromiumOptions.')
|
||||
# ALLOW
|
||||
LOC_OR_IND = 'A locator (str or tuple of length 2) or serial number.'
|
||||
STR_ONLY = 'Str format and not support xpath or css selector.'
|
||||
LOC_FORMAT = 'str or tuple of length 2'
|
||||
ELE_OR_LOC = 'A locator (str or tuple of length 2) or element.'
|
||||
FRAME_LOC_FORMAT = 'locator, iframe serial number, id, name, ChromiumFrame object'
|
||||
SET_DOWNLOAD_PATH = 'Use the set.download_path() method, configuration object, or ini file.'
|
||||
SET_WINDOW_NORMAL = ('When the browser is in full screen or minimized state, call set.window.normal() '
|
||||
'first to restore the normal state.')
|
||||
HTML_ELE_TYPE = 'Element, Tab object, or html text'
|
||||
ELE_LOC_FORMAT = '(x, y) format coordinates, or ChromiumElement objects'
|
||||
IP_OR_OPTIONS = 'ip:port format character string or ChromiumOptions type'
|
||||
|
||||
TAB_OR_ID = 'Tab object or tab id'
|
||||
RUN_JS = 'run js'
|
||||
PAGE_CONNECT = 'Page connection'
|
||||
NEW_ELE_INFO = 'html text, or tuple format: (tag, {name: value}).'
|
||||
DICT_TO_NEW_ELE = ('This page does not support new elements in html format. Please pass the html_or_info '
|
||||
'parameter with dict.')
|
||||
|
||||
# --------- print ---------
|
||||
RETRY = 'Retry'
|
||||
OPTIONS_HAVE_SAVED = 'The configuration is saved to a file'
|
||||
AUTO_LOAD_TIP = 'Later the program can automatically load the configuration from the file'
|
||||
STOP_RECORDING = 'Stop recording.'
|
||||
START_RECORD = 'Start recording.'
|
||||
CHOOSE_RECORD_TARGET = 'Manually select the target you want to record.'
|
||||
UNSUPPORTED_USER_PROXY = ('You seem to be setting up a proxy that uses the account password, which is not '
|
||||
'supported for the time being, and can be implemented by the plug-in itself.')
|
||||
UNSUPPORTED_SOCKS_PROXY = ('You seem to be setting up the use of socks proxy, this proxy is not supported for the '
|
||||
'time being, you can use your own plug-in to achieve the requirements.')
|
||||
NOT_SUPPORT_DOWNLOAD = 'The browser version is too low to use the download management function.'
|
||||
FILE_NAME = 'File Name'
|
||||
FOLDER_PATH = 'Folder Path'
|
||||
UNKNOWN = 'Unknown'
|
||||
DOWNLOAD_COMPLETED = 'Complete'
|
||||
COMPLETED_AND_RENAME = 'Renamed'
|
||||
OVERWROTE = 'Overwrote'
|
||||
DOWNLOAD_CANCELED = 'Canceled'
|
||||
SKIPPED = 'Skipped'
|
||||
@@ -0,0 +1,199 @@
|
||||
# -*- coding:utf-8 -*-
|
||||
"""
|
||||
@Author : g1879
|
||||
@Contact : g1879@qq.com
|
||||
@Website : https://DrissionPage.cn
|
||||
@Copyright: (c) 2020 by g1879, Inc. All Rights Reserved.
|
||||
"""
|
||||
from pathlib import Path
|
||||
from platform import system
|
||||
from shutil import rmtree
|
||||
from tempfile import gettempdir
|
||||
from threading import Lock
|
||||
from time import perf_counter, sleep
|
||||
|
||||
from .._configs.options_manage import OptionsManager
|
||||
from .._functions.settings import Settings as _S
|
||||
from ..errors import (ContextLostError, ElementLostError, PageDisconnectedError, NoRectError, BrowserConnectError,
|
||||
AlertExistsError, IncorrectURLError, StorageError, CookieFormatError, JavaScriptError, CDPError)
|
||||
|
||||
|
||||
class PortFinder(object):
|
||||
used_port = set()
|
||||
prev_time = 0
|
||||
lock = Lock()
|
||||
checked_paths = set()
|
||||
|
||||
def __init__(self, path=None):
|
||||
tmp = Path(path) if path else Path(gettempdir()) / 'DrissionPage'
|
||||
self.tmp_dir = tmp / 'autoPortData'
|
||||
self.tmp_dir.mkdir(parents=True, exist_ok=True)
|
||||
if str(self.tmp_dir.absolute()) not in PortFinder.checked_paths:
|
||||
for i in self.tmp_dir.iterdir():
|
||||
if i.is_dir() and not port_is_using('127.0.0.1', i.name):
|
||||
rmtree(i, ignore_errors=True)
|
||||
PortFinder.checked_paths.add(str(self.tmp_dir.absolute()))
|
||||
|
||||
def get_port(self, scope=None):
|
||||
from random import randint
|
||||
with PortFinder.lock:
|
||||
if PortFinder.prev_time and perf_counter() - PortFinder.prev_time > 60:
|
||||
PortFinder.used_port.clear()
|
||||
if scope in (True, None):
|
||||
scope = (9600, 59600)
|
||||
max_times = scope[1] - scope[0]
|
||||
times = 0
|
||||
while times < max_times:
|
||||
times += 1
|
||||
port = randint(*scope)
|
||||
if port in PortFinder.used_port or port_is_using('127.0.0.1', port):
|
||||
continue
|
||||
path = self.tmp_dir / str(port)
|
||||
if path.exists():
|
||||
try:
|
||||
rmtree(path)
|
||||
except:
|
||||
continue
|
||||
PortFinder.used_port.add(port)
|
||||
PortFinder.prev_time = perf_counter()
|
||||
return port, str(path)
|
||||
raise BrowserConnectError(_S._lang.NO_AVAILABLE_PORT_FOUND)
|
||||
|
||||
|
||||
def port_is_using(ip, port):
|
||||
from socket import socket, AF_INET, SOCK_STREAM
|
||||
s = socket(AF_INET, SOCK_STREAM)
|
||||
s.settimeout(.1)
|
||||
result = s.connect_ex((ip, int(port)))
|
||||
s.close()
|
||||
return result == 0
|
||||
|
||||
|
||||
def clean_folder(folder_path, ignore=None):
|
||||
ignore = [] if not ignore else ignore
|
||||
p = Path(folder_path)
|
||||
|
||||
for f in p.iterdir():
|
||||
if f.name not in ignore:
|
||||
if f.is_file():
|
||||
f.unlink()
|
||||
elif f.is_dir():
|
||||
rmtree(f, True)
|
||||
|
||||
|
||||
def show_or_hide_browser(tab, hide=True):
|
||||
if not tab.browser.address.startswith(('127.0.0.1', 'localhost')):
|
||||
return
|
||||
|
||||
if system().lower() != 'windows':
|
||||
raise EnvironmentError(_S._lang.WIN_SYS_ONLY)
|
||||
|
||||
try:
|
||||
from win32gui import ShowWindow
|
||||
from win32con import SW_HIDE, SW_SHOW
|
||||
except (ImportError, ModuleNotFoundError):
|
||||
raise EnvironmentError(_S._lang.join(_S._lang.NEED_LIB_, 'pypiwin32', TIP='pip install pypiwin32'))
|
||||
|
||||
pid = tab.browser.process_id
|
||||
if not pid:
|
||||
return None
|
||||
hds = get_hwnds_from_pid(pid, tab.title)
|
||||
sw = SW_HIDE if hide else SW_SHOW
|
||||
for hd in hds:
|
||||
ShowWindow(hd, sw)
|
||||
|
||||
|
||||
def get_browser_progress_id(progress, address):
|
||||
if progress:
|
||||
return progress.pid
|
||||
|
||||
from os import popen
|
||||
port = address.split(':')[-1]
|
||||
txt = ''
|
||||
progresses = popen(f'netstat -nao | findstr :{port}').read().split('\n')
|
||||
for progress in progresses:
|
||||
if 'LISTENING' in progress:
|
||||
txt = progress
|
||||
break
|
||||
if not txt:
|
||||
return None
|
||||
|
||||
return txt.split(' ')[-1]
|
||||
|
||||
|
||||
def get_hwnds_from_pid(pid, title):
|
||||
try:
|
||||
from win32gui import IsWindow, GetWindowText, EnumWindows
|
||||
from win32process import GetWindowThreadProcessId
|
||||
except (ImportError, ModuleNotFoundError):
|
||||
raise EnvironmentError(_S._lang.join(_S._lang.NEED_LIB_, 'win32gui, win32process',
|
||||
TIP='pip install pypiwin32\npip install win32process'))
|
||||
|
||||
def callback(hwnd, hds):
|
||||
if IsWindow(hwnd) and title in GetWindowText(hwnd):
|
||||
_, found_pid = GetWindowThreadProcessId(hwnd)
|
||||
if str(found_pid) == str(pid):
|
||||
hds.append(hwnd)
|
||||
return True
|
||||
|
||||
hwnds = []
|
||||
EnumWindows(callback, hwnds)
|
||||
return hwnds
|
||||
|
||||
|
||||
def wait_until(function, kwargs=None, timeout=10):
|
||||
if kwargs is None:
|
||||
kwargs = {}
|
||||
end_time = perf_counter() + timeout
|
||||
while perf_counter() < end_time:
|
||||
value = function(**kwargs)
|
||||
if value:
|
||||
return value
|
||||
sleep(.01)
|
||||
raise TimeoutError
|
||||
|
||||
|
||||
def configs_to_here(save_name=None):
|
||||
om = OptionsManager('default')
|
||||
save_name = f'{save_name}.ini' if save_name is not None else 'dp_configs.ini'
|
||||
om.save(save_name)
|
||||
|
||||
|
||||
def raise_error(result, browser, ignore=None, user=False):
|
||||
error = result['error']
|
||||
if error in ('Cannot find context with specified id', 'Inspected target navigated or closed',
|
||||
'No frame with given id found'):
|
||||
r = ContextLostError()
|
||||
elif error in ('Could not find node with given id', 'Could not find object with given id',
|
||||
'No node with given id found', 'Node with given id does not belong to the document',
|
||||
'No node found for given backend id'):
|
||||
r = ElementLostError()
|
||||
elif error in ('connection disconnected', 'No target with given id found'):
|
||||
r = PageDisconnectedError()
|
||||
elif error == 'alert exists.':
|
||||
r = AlertExistsError()
|
||||
elif error in ('Node does not have a layout object', 'Could not compute box model.'):
|
||||
r = NoRectError()
|
||||
elif error == 'Cannot navigate to invalid URL':
|
||||
r = IncorrectURLError(_S._lang.INVALID_URL, url=result["args"]["url"])
|
||||
elif error == 'Frame corresponds to an opaque origin and its storage key cannot be serialized':
|
||||
r = StorageError()
|
||||
elif error == 'Sanitizing cookie failed':
|
||||
r = CookieFormatError(cookies=result["args"])
|
||||
elif error == 'Invalid header name':
|
||||
r = ValueError(_S._lang.join(_S._lang.INVALID_HEADER_NAME, headers=result["args"]["headers"]))
|
||||
elif error == 'Given expression does not evaluate to a function':
|
||||
r = JavaScriptError(_S._lang.NOT_A_FUNCTION, JS=result["args"]["functionDeclaration"])
|
||||
elif error.endswith("' wasn't found"):
|
||||
r = RuntimeError(_S._lang.join(_S._lang.METHOD_NOT_FOUND, BROWSER_VER=browser.version, METHOD=result["method"]))
|
||||
elif result['type'] == 'timeout':
|
||||
r = TimeoutError(_S._lang.join(_S._lang.NO_RESPONSE, INFO=result["error"],
|
||||
METHOD=result["method"], ARGS=result["args"]))
|
||||
elif result['type'] == 'call_method_error' and not user:
|
||||
r = CDPError(_S._lang.UNKNOWN_ERR, INFO=result["error"], METHOD=result["method"],
|
||||
ARGS=result["args"], TIP=_S._lang.FEEDBACK)
|
||||
else:
|
||||
r = RuntimeError(_S._lang.join(_S._lang.UNKNOWN_ERR, INFO=result, TIP=_S._lang.FEEDBACK))
|
||||
|
||||
if not ignore or not isinstance(r, ignore):
|
||||
raise r
|
||||
@@ -0,0 +1,110 @@
|
||||
# -*- coding:utf-8 -*-
|
||||
"""
|
||||
@Author : g1879
|
||||
@Contact : g1879@qq.com
|
||||
@Website : https://DrissionPage.cn
|
||||
@Copyright: (c) 2020 by g1879, Inc. All Rights Reserved.
|
||||
"""
|
||||
from os import popen
|
||||
from pathlib import Path
|
||||
from threading import Lock
|
||||
from typing import Union, Tuple
|
||||
|
||||
from .._base.chromium import Chromium
|
||||
from .._pages.chromium_base import ChromiumBase
|
||||
|
||||
|
||||
class PortFinder(object):
|
||||
used_port: set = ...
|
||||
prev_time: float = ...
|
||||
lock: Lock = ...
|
||||
tmp_dir: Path = ...
|
||||
checked_paths: set = ...
|
||||
|
||||
def __init__(self, path: Union[str, Path] = None):
|
||||
"""
|
||||
:param path: 临时文件保存路径,为None时使用系统临时文件夹
|
||||
"""
|
||||
...
|
||||
|
||||
@staticmethod
|
||||
def get_port(scope: Tuple[int, int] = None) -> Tuple[int, str]:
|
||||
"""查找一个可用端口
|
||||
:param scope: 指定端口范围,不含最后的数字,为None则使用[9600-59600)
|
||||
:return: 可以使用的端口和用户文件夹路径组成的元组
|
||||
"""
|
||||
...
|
||||
|
||||
|
||||
def port_is_using(ip: str, port: Union[str, int]) -> bool:
|
||||
"""检查端口是否被占用
|
||||
:param ip: 浏览器地址
|
||||
:param port: 浏览器端口
|
||||
:return: bool
|
||||
"""
|
||||
...
|
||||
|
||||
|
||||
def clean_folder(folder_path: Union[str, Path], ignore: Union[tuple, list] = None) -> None:
|
||||
"""清空一个文件夹,除了ignore里的文件和文件夹
|
||||
:param folder_path: 要清空的文件夹路径
|
||||
:param ignore: 忽略列表
|
||||
:return: None
|
||||
"""
|
||||
...
|
||||
|
||||
|
||||
def show_or_hide_browser(tab: ChromiumBase, hide: bool = True) -> None:
|
||||
"""执行显示或隐藏浏览器窗口
|
||||
:param tab: ChromiumTab对象
|
||||
:param hide: 是否隐藏
|
||||
:return: None
|
||||
"""
|
||||
...
|
||||
|
||||
|
||||
def get_browser_progress_id(progress: Union[popen, None], address: str) -> Union[str, None]:
|
||||
"""获取浏览器进程id
|
||||
:param progress: 已知的进程对象,没有时传入None
|
||||
:param address: 浏览器管理地址,含端口
|
||||
:return: 进程id或None
|
||||
"""
|
||||
...
|
||||
|
||||
|
||||
def get_hwnds_from_pid(pid: Union[str, int], title: str) -> list:
|
||||
"""通过PID查询句柄ID
|
||||
:param pid: 进程id
|
||||
:param title: 窗口标题
|
||||
:return: 进程句柄组成的列表
|
||||
"""
|
||||
...
|
||||
|
||||
|
||||
def wait_until(function: callable, kwargs: dict = None, timeout: float = 10):
|
||||
"""等待传入的方法返回值不为假
|
||||
:param function: 要执行的方法
|
||||
:param kwargs: 方法参数
|
||||
:param timeout: 超时时间(秒)
|
||||
:return: 执行结果,超时抛出TimeoutError
|
||||
"""
|
||||
...
|
||||
|
||||
|
||||
def configs_to_here(save_name: Union[Path, str] = None) -> None:
|
||||
"""把默认ini文件复制到当前目录
|
||||
:param save_name: 指定文件名,为None则命名为'dp_configs.ini'
|
||||
:return: None
|
||||
"""
|
||||
...
|
||||
|
||||
|
||||
def raise_error(result: dict, browser: Chromium, ignore=None, user: bool = False) -> None:
|
||||
"""抛出error对应报错
|
||||
:param result: 包含error的dict
|
||||
:param browser: 浏览器对象
|
||||
:param ignore: 要忽略的错误
|
||||
:param user: 是否用户调用的
|
||||
:return: None
|
||||
"""
|
||||
...
|
||||
@@ -0,0 +1,318 @@
|
||||
# -*- coding:utf-8 -*-
|
||||
"""
|
||||
@Author : g1879
|
||||
@Contact : g1879@qq.com
|
||||
@Website : https://DrissionPage.cn
|
||||
@Copyright: (c) 2020 by g1879, Inc. All Rights Reserved.
|
||||
"""
|
||||
from html import unescape
|
||||
from os.path import sep
|
||||
from pathlib import Path
|
||||
from re import sub
|
||||
from urllib.parse import urlparse, urljoin, urlunparse
|
||||
|
||||
from DrissionRecord.tools import make_valid_name
|
||||
from requests.structures import CaseInsensitiveDict
|
||||
|
||||
from .._functions.settings import Settings as _S
|
||||
|
||||
|
||||
def get_ele_txt(e):
|
||||
# 前面无须换行的元素
|
||||
nowrap_list = ('br', 'sub', 'sup', 'em', 'strong', 'a', 'font', 'b', 'span', 's', 'i', 'del', 'ins', 'img', 'td',
|
||||
'th', 'abbr', 'bdi', 'bdo', 'cite', 'code', 'data', 'dfn', 'kbd', 'mark', 'q', 'rp', 'rt', 'ruby',
|
||||
'samp', 'small', 'time', 'u', 'var', 'wbr', 'button', 'slot', 'content')
|
||||
# 后面添加换行的元素
|
||||
wrap_after_list = ('p', 'div', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'ol', 'li', 'blockquote', 'header',
|
||||
'footer', 'address' 'article', 'aside', 'main', 'nav', 'section', 'figcaption', 'summary')
|
||||
# 不获取文本的元素
|
||||
noText_list = ('script', 'style', 'video', 'audio', 'iframe', 'embed', 'noscript', 'canvas', 'template')
|
||||
# 用/t分隔的元素
|
||||
tab_list = ('td', 'th')
|
||||
|
||||
if e.tag in noText_list:
|
||||
return e.raw_text
|
||||
|
||||
def get_node_txt(ele, pre=False) -> list:
|
||||
tag = ele.tag
|
||||
if tag == 'br':
|
||||
return [True]
|
||||
if not pre and tag == 'pre':
|
||||
pre = True
|
||||
|
||||
str_list = []
|
||||
if tag in noText_list and not pre: # 标签内的文本不返回
|
||||
return str_list
|
||||
|
||||
nodes = ele.eles('xpath:./text() | *')
|
||||
prev_ele = ''
|
||||
for el in nodes:
|
||||
if isinstance(el, str): # 字符节点
|
||||
if pre:
|
||||
str_list.append(el)
|
||||
|
||||
else:
|
||||
if sub('[ \n\t\r]', '', el) != '': # 字符除了回车和空格还有其它内容
|
||||
txt = el
|
||||
if not pre:
|
||||
txt = txt.replace('\r\n', ' ').replace('\n', ' ')
|
||||
txt = sub(r' {2,}', ' ', txt)
|
||||
str_list.append(txt)
|
||||
|
||||
else: # 元素节点
|
||||
if el.tag not in nowrap_list and str_list and str_list[-1] != '\n': # 元素间换行的情况
|
||||
str_list.append('\n')
|
||||
if el.tag in tab_list and prev_ele in tab_list: # 表格的行
|
||||
str_list.append('\t')
|
||||
|
||||
str_list.extend(get_node_txt(el, pre))
|
||||
prev_ele = el.tag
|
||||
|
||||
if tag in wrap_after_list and str_list and str_list[-1] not in ('\n', True): # 有些元素后面要添加回车
|
||||
str_list.append('\n')
|
||||
|
||||
return str_list
|
||||
|
||||
re_str = get_node_txt(e)
|
||||
if re_str and re_str[-1] == '\n':
|
||||
re_str.pop()
|
||||
|
||||
l = len(re_str)
|
||||
if l > 1:
|
||||
r = []
|
||||
for i in range(l - 1):
|
||||
i1 = re_str[i]
|
||||
i2 = re_str[i + 1]
|
||||
if i1 is True:
|
||||
r.append('\n')
|
||||
continue
|
||||
elif i2 is True:
|
||||
r.append(i1)
|
||||
continue
|
||||
elif i1.endswith(' ') and i2.startswith(' '):
|
||||
i1 = i1[:-1]
|
||||
r.append(i1)
|
||||
r.append('\n' if re_str[-1] is True else re_str[-1])
|
||||
re_str = ''.join(r)
|
||||
|
||||
elif not l:
|
||||
re_str = ''
|
||||
else:
|
||||
re_str = re_str[0] if re_str[0] is not True else '\n'
|
||||
|
||||
return format_html(re_str.strip())
|
||||
|
||||
|
||||
def format_html(text):
|
||||
return unescape(text).replace('\xa0', ' ') if text else text
|
||||
|
||||
|
||||
def location_in_viewport(page, loc_x, loc_y):
|
||||
js = f'''function(){{let x = {loc_x}; let y = {loc_y};
|
||||
const scrollLeft = document.documentElement.scrollLeft;
|
||||
const scrollTop = document.documentElement.scrollTop;
|
||||
const vWidth = document.documentElement.clientWidth;
|
||||
const vHeight = document.documentElement.clientHeight;
|
||||
if (x< scrollLeft || y < scrollTop || x > vWidth + scrollLeft || y > vHeight + scrollTop){{return false;}}
|
||||
return true;}}'''
|
||||
return page._run_js(js)
|
||||
|
||||
|
||||
def offset_scroll(ele, offset_x, offset_y):
|
||||
loc_x, loc_y = ele.rect.location
|
||||
cp_x, cp_y = ele.rect.click_point
|
||||
lx = loc_x + offset_x if offset_x else cp_x
|
||||
ly = loc_y + offset_y if offset_y else cp_y
|
||||
if not location_in_viewport(ele.owner, lx, ly):
|
||||
clientWidth = ele.owner._run_js('return document.body.clientWidth;')
|
||||
clientHeight = ele.owner._run_js('return document.body.clientHeight;')
|
||||
ele.owner.scroll.to_location(lx - clientWidth // 2, ly - clientHeight // 2)
|
||||
cl_x, cl_y = ele.rect.viewport_location
|
||||
ccp_x, ccp_y = ele.rect.viewport_click_point
|
||||
cx = cl_x + offset_x if offset_x else ccp_x
|
||||
cy = cl_y + offset_y if offset_y else ccp_y
|
||||
return cx, cy
|
||||
|
||||
|
||||
def make_absolute_link(link, baseURI=None):
|
||||
if not link:
|
||||
return link
|
||||
|
||||
link = link.strip().replace('\\', '/')
|
||||
parsed = urlparse(link)._asdict()
|
||||
# if baseURI:
|
||||
# baseURI = baseURI.rstrip('/\\')
|
||||
|
||||
# 是相对路径,与页面url拼接并返回
|
||||
if not parsed['netloc']:
|
||||
return urljoin(baseURI, link) if baseURI else link
|
||||
|
||||
# 是绝对路径但缺少协议,从页面url获取协议并修复
|
||||
if not parsed['scheme'] and baseURI:
|
||||
parsed['scheme'] = urlparse(baseURI).scheme
|
||||
parsed = tuple(v for v in parsed.values())
|
||||
return urlunparse(parsed)
|
||||
|
||||
# 绝对路径且不缺协议,直接返回
|
||||
return link
|
||||
|
||||
|
||||
def is_js_func(func):
|
||||
func = func.strip()
|
||||
if (func.startswith('function') or func.startswith('async ')) and func.endswith('}'):
|
||||
return True
|
||||
# elif '=>' in func:
|
||||
# return True
|
||||
return False
|
||||
|
||||
|
||||
def get_blob(page, url, as_bytes=True):
|
||||
if not url.startswith('blob'):
|
||||
raise ValueError(_S._lang.join(_S._lang.NOT_BLOB, url=url))
|
||||
js = """
|
||||
function fetchData(url) {
|
||||
return new Promise((resolve, reject) => {
|
||||
let xhr = new XMLHttpRequest();
|
||||
xhr.responseType = 'blob';
|
||||
xhr.onload = function() {
|
||||
let reader = new FileReader();
|
||||
reader.onloadend = function(){resolve(reader.result);}
|
||||
reader.readAsDataURL(xhr.response);
|
||||
};
|
||||
xhr.open('GET', url, true);
|
||||
xhr.send();
|
||||
});
|
||||
}
|
||||
"""
|
||||
try:
|
||||
result = page._run_js(js, url)
|
||||
except:
|
||||
raise RuntimeError(_S._lang.join(_S._lang.GET_BLOB_FAILED))
|
||||
if as_bytes:
|
||||
from base64 import b64decode
|
||||
return b64decode(result.split(',', 1)[-1])
|
||||
else:
|
||||
return result
|
||||
|
||||
|
||||
def save_page(tab, path=None, name=None, as_pdf=False, kwargs=None):
|
||||
if name:
|
||||
if name.endswith('.pdf'):
|
||||
name = name[:-4]
|
||||
as_pdf = True
|
||||
elif name.endswith('.mhtml'):
|
||||
name = name[:-6]
|
||||
as_pdf = False
|
||||
|
||||
if path:
|
||||
path = Path(path)
|
||||
if path.suffix.lower() == '.mhtml':
|
||||
name = path.stem
|
||||
path = path.parent
|
||||
as_pdf = False
|
||||
elif path.suffix.lower() == '.pdf':
|
||||
name = path.stem
|
||||
path = path.parent
|
||||
as_pdf = True
|
||||
|
||||
return get_pdf(tab, path, name, kwargs) if as_pdf else get_mhtml(tab, path, name)
|
||||
|
||||
|
||||
def get_mhtml(page, path=None, name=None):
|
||||
r = page._run_cdp('Page.captureSnapshot')['data']
|
||||
if path is None and name is None:
|
||||
return r
|
||||
|
||||
path = path or '.'
|
||||
Path(path).mkdir(parents=True, exist_ok=True)
|
||||
name = make_valid_name(name or page.title)
|
||||
with open(f'{path}{sep}{name}.mhtml', 'w', encoding='utf-8', newline='\n') as f:
|
||||
f.write(r.replace('\r\n', '\n'))
|
||||
return r
|
||||
|
||||
|
||||
def get_pdf(page, path=None, name=None, kwargs=None):
|
||||
if not kwargs:
|
||||
kwargs = {}
|
||||
kwargs['transferMode'] = 'ReturnAsBase64'
|
||||
if 'printBackground' not in kwargs:
|
||||
kwargs['printBackground'] = True
|
||||
try:
|
||||
r = page._run_cdp('Page.printToPDF', **kwargs)['data']
|
||||
except:
|
||||
raise RuntimeError(_S._lang.join(_S._lang.GET_PDF_FAILED))
|
||||
from base64 import b64decode
|
||||
r = b64decode(r)
|
||||
if path is None and name is None:
|
||||
return r
|
||||
|
||||
path = path or '.'
|
||||
Path(path).mkdir(parents=True, exist_ok=True)
|
||||
name = make_valid_name(name or page.title)
|
||||
with open(f'{path}{sep}{name}.pdf', 'wb', newline='\n') as f:
|
||||
f.write(r)
|
||||
return r
|
||||
|
||||
|
||||
def tree(ele_or_page, text=False, show_js=False, show_css=False):
|
||||
def _tree(obj, last_one=True, body=''):
|
||||
list_ele = obj.children()
|
||||
length = len(list_ele)
|
||||
body_unit = ' ' if last_one else '│ '
|
||||
tail = '├───'
|
||||
new_body = body + body_unit
|
||||
|
||||
if length > 0:
|
||||
new_last_one = False
|
||||
for i in range(length):
|
||||
if i == length - 1:
|
||||
tail = '└───'
|
||||
new_last_one = True
|
||||
e = list_ele[i]
|
||||
|
||||
attrs = ' '.join([f"{k}='{v}'" for k, v in e.attrs.items()])
|
||||
show_text = f'{new_body}{tail}<{e.tag} {attrs}>'.replace('\n', ' ')
|
||||
if text:
|
||||
t = e('x:/text()')
|
||||
if t:
|
||||
t = t.replace('\n', ' ')
|
||||
if (e.tag not in ('script', 'style') or (e.tag == 'script' and show_js)
|
||||
or (e.tag == 'style' and show_css)):
|
||||
if text is not True:
|
||||
t = t[:text]
|
||||
show_text = f'{show_text} {t}'
|
||||
print(show_text)
|
||||
|
||||
_tree(e, new_last_one, new_body)
|
||||
|
||||
ele = ele_or_page.s_ele()
|
||||
attrs = ' '.join([f"{k}='{v}'" for k, v in ele.attrs.items()])
|
||||
show_text = f'<{ele.tag} {attrs}>'.replace('\n', ' ')
|
||||
if text:
|
||||
t = ele('x:/text()')
|
||||
if t:
|
||||
t = t.replace('\n', ' ')
|
||||
if (ele.tag not in ('script', 'style') or (ele.tag == 'script' and show_js)
|
||||
or (ele.tag == 'style' and show_css)):
|
||||
if text is not True:
|
||||
t = t[:text]
|
||||
show_text = f'{show_text} {t}'
|
||||
print(show_text)
|
||||
_tree(ele)
|
||||
|
||||
|
||||
def format_headers(txt):
|
||||
if isinstance(txt, (dict, CaseInsensitiveDict)):
|
||||
for k, v in txt.items():
|
||||
if v not in (None, False, True):
|
||||
txt[k] = str(v)
|
||||
for i in (':method', ':scheme', ':authority', ':path'):
|
||||
txt.pop(i, None)
|
||||
return txt
|
||||
headers = {}
|
||||
for header in txt.split('\n'):
|
||||
if header:
|
||||
name, value = header.split(': ', maxsplit=1)
|
||||
if name not in (':method', ':scheme', ':authority', ':path'):
|
||||
headers[name] = value
|
||||
return headers
|
||||
@@ -0,0 +1,140 @@
|
||||
# -*- coding:utf-8 -*-
|
||||
"""
|
||||
@Author : g1879
|
||||
@Contact : g1879@qq.com
|
||||
@Website : https://DrissionPage.cn
|
||||
@Copyright: (c) 2020 by g1879, Inc. All Rights Reserved.
|
||||
"""
|
||||
from pathlib import Path
|
||||
from typing import Union, Optional, Tuple
|
||||
|
||||
from .._base.base import DrissionElement, BaseParser
|
||||
from .._elements.chromium_element import ChromiumElement
|
||||
from .._pages.chromium_base import ChromiumBase
|
||||
from .._pages.chromium_page import ChromiumPage
|
||||
from .._pages.chromium_tab import ChromiumTab
|
||||
|
||||
|
||||
def get_ele_txt(e: DrissionElement) -> str:
|
||||
"""获取元素内所有文本
|
||||
:param e: 元素对象
|
||||
:return: 元素内所有文本
|
||||
"""
|
||||
...
|
||||
|
||||
|
||||
def format_html(text: str) -> str:
|
||||
"""处理html编码字符
|
||||
:param text: html文本
|
||||
:return: 格式化后的html文本
|
||||
"""
|
||||
...
|
||||
|
||||
|
||||
def location_in_viewport(page: ChromiumBase, loc_x: float, loc_y: float) -> bool:
|
||||
"""判断给定的坐标是否在视口中 |n
|
||||
:param page: ChromePage对象
|
||||
:param loc_x: 页面绝对坐标x
|
||||
:param loc_y: 页面绝对坐标y
|
||||
:return: bool
|
||||
"""
|
||||
...
|
||||
|
||||
|
||||
def offset_scroll(ele: ChromiumElement, offset_x: float, offset_y: float) -> Tuple[int, int]:
|
||||
"""接收元素及偏移坐标,把坐标滚动到页面中间,返回该点坐标
|
||||
有偏移量时以元素左上角坐标为基准,没有时以click_point为基准
|
||||
:param ele: 元素对象
|
||||
:param offset_x: 偏移量x
|
||||
:param offset_y: 偏移量y
|
||||
:return: 相对坐标
|
||||
"""
|
||||
...
|
||||
|
||||
|
||||
def make_absolute_link(link: str, baseURI: str = None) -> str:
|
||||
"""获取绝对url
|
||||
:param link: 超链接
|
||||
:param baseURI: 页面或iframe的url
|
||||
:return: 绝对链接
|
||||
"""
|
||||
...
|
||||
|
||||
|
||||
def is_js_func(func: str) -> bool:
|
||||
"""检查文本是否js函数"""
|
||||
...
|
||||
|
||||
|
||||
def get_blob(page: ChromiumBase, url: str, as_bytes: bool = True) -> bytes:
|
||||
"""获取知道blob资源
|
||||
:param page: 资源所在页面对象
|
||||
:param url: 资源url
|
||||
:param as_bytes: 是否以字节形式返回
|
||||
:return: 资源内容
|
||||
"""
|
||||
...
|
||||
|
||||
|
||||
def save_page(tab: Union[ChromiumPage, ChromiumTab],
|
||||
path: Union[Path, str, None] = None,
|
||||
name: Optional[str] = None,
|
||||
as_pdf: bool = False,
|
||||
kwargs: dict = None) -> Union[bytes, str]:
|
||||
"""把当前页面保存为文件,如果path和name参数都为None,只返回文本
|
||||
:param tab: Tab或Page对象
|
||||
:param path: 保存路径,为None且name不为None时保存在当前路径
|
||||
:param name: 文件名,为None且path不为None时用title属性值
|
||||
:param as_pdf: 为Ture保存为pdf,否则为mhtml且忽略kwargs参数
|
||||
:param kwargs: pdf生成参数
|
||||
:return: as_pdf为True时返回bytes,否则返回文件文本
|
||||
"""
|
||||
...
|
||||
|
||||
|
||||
def get_mhtml(page: Union[ChromiumPage, ChromiumTab],
|
||||
path: Optional[Path] = None,
|
||||
name: Optional[str] = None) -> Union[bytes, str]:
|
||||
"""把当前页面保存为mhtml文件,如果path和name参数都为None,只返回mhtml文本
|
||||
:param page: 要保存的页面对象
|
||||
:param path: 保存路径,为None且name不为None时保存在当前路径
|
||||
:param name: 文件名,为None且path不为None时用title属性值
|
||||
:return: mhtml文本
|
||||
"""
|
||||
...
|
||||
|
||||
|
||||
def get_pdf(page: Union[ChromiumPage, ChromiumTab],
|
||||
path: Optional[Path] = None,
|
||||
name: Optional[str] = None,
|
||||
kwargs: dict = None) -> Union[bytes, str]:
|
||||
"""把当前页面保存为pdf文件,如果path和name参数都为None,只返回字节
|
||||
:param page: 要保存的页面对象
|
||||
:param path: 保存路径,为None且name不为None时保存在当前路径
|
||||
:param name: 文件名,为None且path不为None时用title属性值
|
||||
:param kwargs: pdf生成参数
|
||||
:return: pdf文本
|
||||
"""
|
||||
...
|
||||
|
||||
|
||||
def tree(ele_or_page: BaseParser,
|
||||
text: Union[int, bool] = False,
|
||||
show_js: bool = False,
|
||||
show_css: bool = False) -> None:
|
||||
"""把页面或元素对象DOM结构打印出来
|
||||
:param ele_or_page: 页面或元素对象
|
||||
:param text: 是否打印文本,输入数字可指定打印文本长度上线
|
||||
:param show_js: 打印文本时是否包含<script>内文本,text参数为False时无效
|
||||
:param show_css: 打印文本时是否包含<style>内文本,text参数为False时无效
|
||||
:return: None
|
||||
"""
|
||||
...
|
||||
|
||||
|
||||
def format_headers(txt: str) -> dict:
|
||||
"""从浏览器复制的文本生成dict格式headers,文本用换行分隔
|
||||
:param txt: 从浏览器复制的原始文本格式headers
|
||||
:return: dict格式headers
|
||||
"""
|
||||
...
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,950 @@
|
||||
# -*- coding:utf-8 -*-
|
||||
"""
|
||||
@Author : g1879
|
||||
@Contact : g1879@qq.com
|
||||
@Website : https://DrissionPage.cn
|
||||
@Copyright: (c) 2020 by g1879, Inc. All Rights Reserved.
|
||||
"""
|
||||
from json import loads, JSONDecodeError
|
||||
from os.path import sep
|
||||
from pathlib import Path
|
||||
from re import findall
|
||||
from threading import Thread
|
||||
from time import perf_counter, sleep
|
||||
|
||||
from DrissionRecord.tools import make_valid_name
|
||||
|
||||
from .._base.base import BasePage
|
||||
from .._elements.chromium_element import run_js, make_chromium_eles
|
||||
from .._elements.none_element import NoneElement
|
||||
from .._elements.session_element import make_session_ele
|
||||
from .._functions.cookies import CookiesList
|
||||
from .._functions.elements import SessionElementsList, get_frame, ChromiumElementsList
|
||||
from .._functions.locator import get_loc
|
||||
from .._functions.settings import Settings as _S
|
||||
from .._functions.tools import raise_error
|
||||
from .._functions.web import location_in_viewport
|
||||
from .._units.actions import Actions
|
||||
from .._units.console import Console
|
||||
from .._units.listener import Listener
|
||||
from .._units.rect import TabRect
|
||||
from .._units.screencast import Screencast
|
||||
from .._units.scroller import PageScroller
|
||||
from .._units.setter import ChromiumBaseSetter
|
||||
from .._units.states import PageStates
|
||||
from .._units.waiter import BaseWaiter
|
||||
from ..errors import (ContextLostError, CDPError, PageDisconnectedError, ElementLostError, JavaScriptError,
|
||||
BrowserConnectError, LocatorError)
|
||||
|
||||
__ERROR__ = 'error'
|
||||
|
||||
|
||||
class ChromiumBase(BasePage):
|
||||
def __init__(self, browser, target_id=None):
|
||||
super().__init__()
|
||||
self._browser = browser
|
||||
self._is_loading = None
|
||||
self._root_id = None # object id
|
||||
self._set = None
|
||||
self._screencast = None
|
||||
self._actions = None
|
||||
self._states = None
|
||||
self._has_alert = False
|
||||
self._ready_state = None
|
||||
self._rect = None
|
||||
self._wait = None
|
||||
self._scroll = None
|
||||
self._console = None
|
||||
self._upload_list = None
|
||||
self._doc_got = False # 用于在LoadEventFired和FrameStoppedLoading间标记是否已获取doc
|
||||
self._auto_handle_alert = None
|
||||
self._load_end_time = 0
|
||||
self._init_jss = []
|
||||
self._disconnect_flag = False
|
||||
self._type = 'ChromiumBase'
|
||||
if not hasattr(self, '_listener'):
|
||||
self._listener = None
|
||||
|
||||
self._d_set_runtime_settings()
|
||||
self._connect_browser(target_id)
|
||||
|
||||
def __call__(self, locator, index=1, timeout=None):
|
||||
return self.ele(locator, index, timeout)
|
||||
|
||||
def _d_set_runtime_settings(self):
|
||||
pass
|
||||
|
||||
def _connect_browser(self, target_id=None):
|
||||
self._is_reading = False
|
||||
|
||||
if not target_id:
|
||||
if self.browser._ws_only:
|
||||
tabs = self._run_cdp('Target.getTargets')['targetInfos']
|
||||
_id = 'targetId'
|
||||
else:
|
||||
tabs = self.browser._driver.get(f'http://{self.browser.address}/json').json()
|
||||
_id = 'id'
|
||||
tabs = [(i[_id], i['url']) for i in tabs
|
||||
if i['type'] in ('page', 'webview') and not i['url'].startswith('devtools://')]
|
||||
dialog = None
|
||||
if len(tabs) > 1:
|
||||
for k, t in enumerate(tabs):
|
||||
if t[1] == 'chrome://privacy-sandbox-dialog/notice':
|
||||
dialog = k
|
||||
elif not target_id:
|
||||
target_id = t[0]
|
||||
|
||||
if target_id and dialog is not None:
|
||||
break
|
||||
|
||||
if dialog is not None:
|
||||
close_privacy_dialog(self, tabs[dialog][0])
|
||||
|
||||
else:
|
||||
target_id = tabs[0][0]
|
||||
|
||||
self._driver_init(target_id)
|
||||
if self._js_ready_state == 'complete' and self._ready_state is None:
|
||||
self._get_document()
|
||||
self._ready_state = 'complete'
|
||||
|
||||
def _driver_init(self, target_id):
|
||||
self._is_loading = True
|
||||
self._driver = self.browser._get_driver(target_id, self)
|
||||
|
||||
self._alert = Alert(self._auto_handle_alert)
|
||||
self._driver.set_callback('Page.javascriptDialogOpening', self._on_alert_open, immediate=True)
|
||||
self._driver.set_callback('Page.javascriptDialogClosed', self._on_alert_close)
|
||||
|
||||
self._driver.run('Page.enable')
|
||||
self._driver.run('DOM.enable')
|
||||
self._driver.run('Emulation.setFocusEmulationEnabled', enabled=True)
|
||||
|
||||
r = self._run_cdp('Page.getFrameTree')
|
||||
for i in findall(r"'id': '(.*?)'", str(r)):
|
||||
self.browser._frames[i] = self.tab_id
|
||||
if not hasattr(self, '_frame_id'):
|
||||
self._frame_id = r['frameTree']['frame']['id']
|
||||
|
||||
self._driver.set_callback('Page.frameStartedLoading', self._onFrameStartedLoading)
|
||||
self._driver.set_callback('Page.frameNavigated', self._onFrameNavigated)
|
||||
self._driver.set_callback('Page.domContentEventFired', self._onDomContentEventFired)
|
||||
self._driver.set_callback('Page.loadEventFired', self._onLoadEventFired)
|
||||
self._driver.set_callback('Page.frameStoppedLoading', self._onFrameStoppedLoading)
|
||||
self._driver.set_callback('Page.frameAttached', self._onFrameAttached)
|
||||
self._driver.set_callback('Page.frameDetached', self._onFrameDetached)
|
||||
|
||||
def _get_document(self, timeout=10):
|
||||
if self._is_reading:
|
||||
return
|
||||
self._is_reading = True
|
||||
timeout = max(timeout, 2)
|
||||
end_time = perf_counter() + timeout
|
||||
while perf_counter() < end_time:
|
||||
try:
|
||||
b_id = self._run_cdp('DOM.getDocument', _timeout=timeout)['root']['backendNodeId']
|
||||
timeout = end_time - perf_counter()
|
||||
timeout = 1 if timeout <= 1 else timeout
|
||||
self._root_id = self._run_cdp('DOM.resolveNode', backendNodeId=b_id,
|
||||
_timeout=timeout)['object']['objectId']
|
||||
result = True
|
||||
break
|
||||
|
||||
except PageDisconnectedError:
|
||||
result = False
|
||||
break
|
||||
except:
|
||||
timeout = end_time - perf_counter()
|
||||
timeout = .5 if timeout <= 0 else timeout
|
||||
sleep(.05)
|
||||
|
||||
else:
|
||||
result = False
|
||||
|
||||
if result:
|
||||
r = self._run_cdp('Page.getFrameTree', _ignore=PageDisconnectedError)
|
||||
for i in findall(r"'id': '(.*?)'", str(r)):
|
||||
self.browser._frames[i] = self.tab_id
|
||||
|
||||
self._is_loading = False
|
||||
self._is_reading = False
|
||||
return result
|
||||
|
||||
def _onFrameDetached(self, **kwargs):
|
||||
self.browser._frames.pop(kwargs['frameId'], None)
|
||||
|
||||
def _onFrameAttached(self, **kwargs):
|
||||
self.browser._frames[kwargs['frameId']] = self.tab_id
|
||||
|
||||
def _onFrameStartedLoading(self, **kwargs):
|
||||
self.browser._frames[kwargs['frameId']] = self.tab_id
|
||||
if kwargs['frameId'] == self._frame_id:
|
||||
self._doc_got = False
|
||||
self._ready_state = 'connecting'
|
||||
self._is_loading = True
|
||||
self._load_end_time = perf_counter() + self.timeouts.page_load
|
||||
if self._load_mode == 'eager':
|
||||
t = Thread(target=self._wait_to_stop)
|
||||
t.daemon = True
|
||||
t.start()
|
||||
|
||||
def _onFrameNavigated(self, **kwargs):
|
||||
if kwargs['frame']['id'] == self._frame_id:
|
||||
self._doc_got = False
|
||||
self._ready_state = 'loading'
|
||||
self._is_loading = True
|
||||
if kwargs.get('type', None) == 'BackForwardCacheRestore':
|
||||
self._get_document()
|
||||
|
||||
def _onDomContentEventFired(self, **kwargs):
|
||||
if self._load_mode == 'eager':
|
||||
self._run_cdp('Page.stopLoading')
|
||||
if self._get_document(self._load_end_time - perf_counter() - .1):
|
||||
self._doc_got = True
|
||||
self._ready_state = 'interactive'
|
||||
|
||||
def _onLoadEventFired(self, **kwargs):
|
||||
if self._doc_got is False and self._get_document(self._load_end_time - perf_counter() - .1):
|
||||
self._doc_got = True
|
||||
self._ready_state = 'complete'
|
||||
|
||||
def _onFrameStoppedLoading(self, **kwargs):
|
||||
self.browser._frames[kwargs['frameId']] = self.tab_id
|
||||
if kwargs['frameId'] == self._frame_id:
|
||||
if self._doc_got is False:
|
||||
self._get_document(self._load_end_time - perf_counter() - .1)
|
||||
self._ready_state = 'complete'
|
||||
|
||||
def _onFileChooserOpened(self, **kwargs):
|
||||
if self._upload_list:
|
||||
if 'backendNodeId' not in kwargs:
|
||||
raise RuntimeError(_S._lang.join(_S._lang.CANNOT_INPUT_FILE))
|
||||
files = self._upload_list if kwargs['mode'] == 'selectMultiple' else self._upload_list[:1]
|
||||
self._run_cdp('DOM.setFileInputFiles', files=files, backendNodeId=kwargs['backendNodeId'])
|
||||
|
||||
self.driver.set_callback('Page.fileChooserOpened', None)
|
||||
self._run_cdp('Page.setInterceptFileChooserDialog', enabled=False)
|
||||
self._upload_list = None
|
||||
|
||||
def _wait_to_stop(self):
|
||||
end_time = perf_counter() + self.timeouts.page_load
|
||||
while perf_counter() < end_time:
|
||||
sleep(.02)
|
||||
if self._ready_state in ('interactive', 'complete') and self._is_loading:
|
||||
self.stop_loading()
|
||||
|
||||
# ----------挂件----------
|
||||
@property
|
||||
def wait(self):
|
||||
if self._wait is None:
|
||||
self._wait = BaseWaiter(self)
|
||||
return self._wait
|
||||
|
||||
@property
|
||||
def set(self):
|
||||
if self._set is None:
|
||||
self._set = ChromiumBaseSetter(self)
|
||||
return self._set
|
||||
|
||||
@property
|
||||
def screencast(self):
|
||||
if self._screencast is None:
|
||||
self._screencast = Screencast(self)
|
||||
return self._screencast
|
||||
|
||||
@property
|
||||
def actions(self):
|
||||
if self._actions is None:
|
||||
self._actions = Actions(self)
|
||||
self.wait.doc_loaded()
|
||||
return self._actions
|
||||
|
||||
@property
|
||||
def listen(self):
|
||||
if self._listener is None:
|
||||
self._listener = Listener(self)
|
||||
return self._listener
|
||||
|
||||
@property
|
||||
def states(self):
|
||||
if self._states is None:
|
||||
self._states = PageStates(self)
|
||||
return self._states
|
||||
|
||||
@property
|
||||
def scroll(self):
|
||||
self.wait.doc_loaded()
|
||||
if self._scroll is None:
|
||||
self._scroll = PageScroller(self)
|
||||
return self._scroll
|
||||
|
||||
@property
|
||||
def rect(self):
|
||||
# self.wait.doc_loaded()
|
||||
if self._rect is None:
|
||||
self._rect = TabRect(self)
|
||||
return self._rect
|
||||
|
||||
@property
|
||||
def console(self):
|
||||
if self._console is None:
|
||||
self._console = Console(self)
|
||||
return self._console
|
||||
|
||||
@property
|
||||
def timeout(self):
|
||||
return self._timeouts.base
|
||||
|
||||
@property
|
||||
def timeouts(self):
|
||||
return self._timeouts
|
||||
|
||||
# ----------挂件结束----------
|
||||
|
||||
@property
|
||||
def browser(self):
|
||||
return self._browser
|
||||
|
||||
@property
|
||||
def driver(self):
|
||||
if self._driver is None:
|
||||
raise BrowserConnectError(_S._lang.BROWSER_DISCONNECTED)
|
||||
return self._driver
|
||||
|
||||
@property
|
||||
def title(self):
|
||||
return self._run_cdp_loaded('Target.getTargetInfo', targetId=self._target_id)['targetInfo']['title']
|
||||
|
||||
@property
|
||||
def url(self):
|
||||
return self._run_cdp_loaded('Target.getTargetInfo', targetId=self._target_id)['targetInfo']['url']
|
||||
|
||||
@property
|
||||
def _browser_url(self):
|
||||
return self.url
|
||||
|
||||
@property
|
||||
def html(self):
|
||||
self.wait.doc_loaded()
|
||||
return self._run_cdp('DOM.getOuterHTML', objectId=self._root_id)['outerHTML']
|
||||
|
||||
@property
|
||||
def json(self):
|
||||
try:
|
||||
return loads(self('t:pre', timeout=.5).text)
|
||||
except JSONDecodeError:
|
||||
return None
|
||||
|
||||
@property
|
||||
def tab_id(self):
|
||||
return self.driver.id
|
||||
|
||||
@property
|
||||
def _target_id(self):
|
||||
return self.driver.id
|
||||
|
||||
@property
|
||||
def active_ele(self):
|
||||
return self._run_js_loaded('return document.activeElement;')
|
||||
|
||||
@property
|
||||
def load_mode(self):
|
||||
return self._load_mode
|
||||
|
||||
@property
|
||||
def user_agent(self):
|
||||
return self._run_cdp('Runtime.evaluate', expression='navigator.userAgent;')['result']['value']
|
||||
|
||||
@property
|
||||
def upload_list(self):
|
||||
return self._upload_list
|
||||
|
||||
@property
|
||||
def session(self):
|
||||
if self._session is None:
|
||||
self._create_session()
|
||||
return self._session
|
||||
|
||||
@property
|
||||
def _js_ready_state(self):
|
||||
try:
|
||||
return self._run_cdp('Runtime.evaluate', expression='document.readyState;', _timeout=3)['result']['value']
|
||||
except ContextLostError:
|
||||
return None
|
||||
except TimeoutError:
|
||||
return 'timeout'
|
||||
|
||||
def run_cdp(self, cmd, **cmd_args):
|
||||
r = self.driver.run(cmd, **cmd_args)
|
||||
return r if __ERROR__ not in r else raise_error(r, self.browser, user=True)
|
||||
|
||||
def run_cdp_loaded(self, cmd, **cmd_args):
|
||||
self.wait.doc_loaded()
|
||||
r = self.driver.run(cmd, **cmd_args)
|
||||
return r if __ERROR__ not in r else raise_error(r, self.browser, user=True)
|
||||
|
||||
def _run_cdp(self, cmd, **cmd_args):
|
||||
ignore = cmd_args.pop('_ignore', None)
|
||||
r = self.driver.run(cmd, **cmd_args)
|
||||
return r if __ERROR__ not in r else raise_error(r, self.browser, ignore)
|
||||
|
||||
def _run_cdp_loaded(self, cmd, **cmd_args):
|
||||
self.wait.doc_loaded()
|
||||
return self._run_cdp(cmd, **cmd_args)
|
||||
|
||||
def run_js(self, script, *args, as_expr=False, timeout=None):
|
||||
return self._run_js(script, *args, as_expr=as_expr, timeout=timeout)
|
||||
|
||||
def run_js_loaded(self, script, *args, as_expr=False, timeout=None):
|
||||
self.wait.doc_loaded()
|
||||
return self._run_js(script, *args, as_expr=as_expr, timeout=timeout)
|
||||
|
||||
def _run_js(self, script, *args, as_expr=False, timeout=None):
|
||||
return run_js(self, script, as_expr, self.timeouts.script if timeout is None else timeout, args)
|
||||
|
||||
def _run_js_loaded(self, script, *args, as_expr=False, timeout=None):
|
||||
self.wait.doc_loaded()
|
||||
return run_js(self, script, as_expr, self.timeouts.script if timeout is None else timeout, args)
|
||||
|
||||
def run_async_js(self, script, *args, as_expr=False):
|
||||
run_js(self, script, as_expr, 0, args)
|
||||
|
||||
def get(self, url, show_errmsg=False, retry=None, interval=None, timeout=None):
|
||||
retry, interval, is_file = self._before_connect(url, retry, interval)
|
||||
self._url_available = self._d_connect(self._url, times=retry, interval=interval,
|
||||
show_errmsg=show_errmsg, timeout=timeout)
|
||||
return self._url_available
|
||||
|
||||
def cookies(self, all_domains=False, all_info=False):
|
||||
txt = 'Storage' if all_domains else 'Network'
|
||||
cookies = self._run_cdp_loaded(f'{txt}.getCookies')['cookies']
|
||||
|
||||
if all_info:
|
||||
r = cookies
|
||||
else:
|
||||
r = [{'name': cookie['name'], 'value': cookie['value'], 'domain': cookie['domain']} for cookie in cookies]
|
||||
|
||||
return CookiesList(r)
|
||||
|
||||
def ele(self, locator, index=1, timeout=None):
|
||||
return self._ele(locator, timeout=timeout, index=index, method='ele()')
|
||||
|
||||
def eles(self, locator, timeout=None):
|
||||
return self._ele(locator, timeout=timeout, index=None)
|
||||
|
||||
def s_ele(self, locator=None, index=1, timeout=None):
|
||||
if timeout is None:
|
||||
timeout = self.timeout
|
||||
return (NoneElement(self, method='s_ele()', args={'locator': locator, 'index': index, 'timeout': timeout})
|
||||
if locator and not self.wait.eles_loaded(locator, timeout=timeout)
|
||||
else make_session_ele(self, locator, index=index, method='s_ele()'))
|
||||
|
||||
def s_eles(self, locator, timeout=None):
|
||||
return (make_session_ele(self, locator, index=None)
|
||||
if self.wait.eles_loaded(locator, timeout=timeout) else SessionElementsList())
|
||||
|
||||
def _find_elements(self, locator, timeout, index=1, relative=False, raise_err=None):
|
||||
if isinstance(locator, (str, tuple)):
|
||||
loc = get_loc(locator)[1]
|
||||
elif locator._type in ('ChromiumElement', 'ChromiumFrame'):
|
||||
return locator
|
||||
else:
|
||||
raise LocatorError(_S._lang.join(ALLOW_TYPE=_S._lang.ELE_OR_LOC, CURR_VAL=locator))
|
||||
|
||||
self.wait.doc_loaded()
|
||||
end_time = perf_counter() + timeout
|
||||
|
||||
search_ids = []
|
||||
timeout = .5 if timeout <= 0 else timeout
|
||||
result = self.driver.run('DOM.performSearch', query=loc, _timeout=timeout, includeUserAgentShadowDOM=True)
|
||||
if not result or __ERROR__ in result:
|
||||
num = 0
|
||||
else:
|
||||
num = result['resultCount']
|
||||
search_ids.append(result['searchId'])
|
||||
|
||||
while True:
|
||||
if num > 0:
|
||||
from_index = index_arg = 0
|
||||
if index is None:
|
||||
end_index = num
|
||||
index_arg = None
|
||||
elif index < 0:
|
||||
from_index = index + num
|
||||
end_index = from_index + 1
|
||||
else:
|
||||
from_index = index - 1
|
||||
end_index = from_index + 1
|
||||
|
||||
if from_index <= num - 1:
|
||||
nIds = self._driver.run('DOM.getSearchResults', searchId=result['searchId'],
|
||||
fromIndex=from_index, toIndex=end_index)
|
||||
if __ERROR__ not in nIds:
|
||||
if nIds['nodeIds'][0] != 0:
|
||||
r = make_chromium_eles(self, _ids=nIds['nodeIds'], index=index_arg,
|
||||
is_obj_id=False, ele_only=True)
|
||||
if r is not False:
|
||||
break
|
||||
|
||||
elif nIds[__ERROR__] == 'connection disconnected':
|
||||
raise PageDisconnectedError
|
||||
|
||||
if perf_counter() >= end_time:
|
||||
return NoneElement(self) if index is not None else ChromiumElementsList(owner=self)
|
||||
|
||||
sleep(.01)
|
||||
timeout = end_time - perf_counter()
|
||||
timeout = .5 if timeout <= 0 else timeout
|
||||
result = self.driver.run('DOM.performSearch', query=loc, _timeout=timeout, includeUserAgentShadowDOM=True)
|
||||
if result and __ERROR__ not in result:
|
||||
num = result['resultCount']
|
||||
search_ids.append(result['searchId'])
|
||||
elif result and result[__ERROR__] == 'connection disconnected':
|
||||
raise PageDisconnectedError
|
||||
|
||||
for _id in search_ids:
|
||||
self._driver.run('DOM.discardSearchResults', searchId=_id)
|
||||
|
||||
return r
|
||||
|
||||
def refresh(self, ignore_cache=False):
|
||||
self._is_loading = True
|
||||
self._run_cdp('Page.reload', ignoreCache=ignore_cache)
|
||||
self.wait.load_start()
|
||||
|
||||
def forward(self, steps=1):
|
||||
self._forward_or_back(steps)
|
||||
|
||||
def back(self, steps=1):
|
||||
self._forward_or_back(-steps)
|
||||
|
||||
def _forward_or_back(self, steps):
|
||||
if steps == 0:
|
||||
return
|
||||
|
||||
history = self._run_cdp('Page.getNavigationHistory')
|
||||
index = history['currentIndex']
|
||||
history = history['entries']
|
||||
direction = 1 if steps > 0 else -1
|
||||
curr_url = history[index]['url']
|
||||
nid = None
|
||||
for num in range(abs(steps)):
|
||||
for i in history[index::direction]:
|
||||
index += direction
|
||||
if i['url'] != curr_url:
|
||||
nid = i['id']
|
||||
curr_url = i['url']
|
||||
break
|
||||
|
||||
if nid:
|
||||
self._is_loading = True
|
||||
self._run_cdp('Page.navigateToHistoryEntry', entryId=nid)
|
||||
|
||||
def stop_loading(self):
|
||||
try:
|
||||
self._run_cdp('Page.stopLoading')
|
||||
end_time = perf_counter() + 5
|
||||
while self._ready_state != 'complete' and perf_counter() < end_time:
|
||||
sleep(.02)
|
||||
except (PageDisconnectedError, CDPError):
|
||||
pass
|
||||
finally:
|
||||
self._ready_state = 'complete'
|
||||
|
||||
def remove_ele(self, loc_or_ele):
|
||||
if not loc_or_ele:
|
||||
return
|
||||
ele = self._ele(loc_or_ele, raise_err=False)
|
||||
if ele:
|
||||
self._run_cdp('DOM.removeNode', nodeId=ele._node_id, _ignore=ElementLostError)
|
||||
|
||||
def add_ele(self, html_or_info, insert_to=None, before=None):
|
||||
if isinstance(html_or_info, str):
|
||||
insert_to = self.ele(insert_to) if insert_to else self.ele('t:body')
|
||||
args = [html_or_info, insert_to]
|
||||
if before:
|
||||
args.append(insert_to.ele(before))
|
||||
js = '''
|
||||
ele = document.createElement(null);
|
||||
arguments[1].insertBefore(ele, arguments[2]);
|
||||
ele.outerHTML = arguments[0];
|
||||
return arguments[2].previousElementSibling;
|
||||
'''
|
||||
else:
|
||||
js = '''
|
||||
ele = document.createElement(null);
|
||||
arguments[1].appendChild(ele);
|
||||
ele.outerHTML = arguments[0];
|
||||
return arguments[1].lastElementChild;
|
||||
'''
|
||||
|
||||
elif isinstance(html_or_info, tuple):
|
||||
args = [html_or_info[0], html_or_info[1]]
|
||||
txt = ''
|
||||
if insert_to:
|
||||
args.append(self.ele(insert_to))
|
||||
if before:
|
||||
args.append(self.ele(before))
|
||||
txt = '''
|
||||
arguments[2].insertBefore(ele, arguments[3]);
|
||||
'''
|
||||
else:
|
||||
txt = '''
|
||||
arguments[2].appendChild(ele);
|
||||
'''
|
||||
js = f'''
|
||||
ele = document.createElement(arguments[0]);
|
||||
for(let k in arguments[1]){{
|
||||
if(k=="innerHTML"){{ele.innerHTML=arguments[1][k]}}
|
||||
else if(k=="innerText"){{ele.innerText=arguments[1][k]}}
|
||||
else{{ele.setAttribute(k, arguments[1][k]);}}
|
||||
}}
|
||||
{txt}
|
||||
return ele;
|
||||
'''
|
||||
|
||||
else:
|
||||
raise ValueError(_S._lang.join(_S._lang.INCORRECT_VAL_, 'html_or_info', ALLOW_TYPE='html, tuple',
|
||||
TIP=_S._lang.NEW_ELE_INFO, CURR_VAL=html_or_info))
|
||||
|
||||
try:
|
||||
ele = self._run_js(js, *args)
|
||||
except JavaScriptError:
|
||||
raise RuntimeError(_S._lang.join(_S._lang.DICT_TO_NEW_ELE))
|
||||
return ele
|
||||
|
||||
def get_frame(self, loc_ind_ele, timeout=None):
|
||||
return get_frame(self, loc_ind_ele=loc_ind_ele, timeout=timeout)
|
||||
|
||||
def get_frames(self, locator=None, timeout=None):
|
||||
locator = locator or 'xpath://*[name()="iframe" or name()="frame"]'
|
||||
frames = self._ele(locator, timeout=timeout, index=None, raise_err=False)
|
||||
return ChromiumElementsList(self, frames)
|
||||
|
||||
def session_storage(self, item=None):
|
||||
js = f'sessionStorage.getItem("{item}")' if item else 'sessionStorage'
|
||||
return self._run_js_loaded(js, as_expr=True)
|
||||
|
||||
def local_storage(self, item=None):
|
||||
js = f'localStorage.getItem("{item}")' if item else 'localStorage'
|
||||
return self._run_js_loaded(js, as_expr=True)
|
||||
|
||||
def get_screenshot(self, path=None, name=None, as_bytes=None, as_base64=None,
|
||||
full_page=False, left_top=None, right_bottom=None):
|
||||
return self._get_screenshot(path=path, name=name, as_bytes=as_bytes, as_base64=as_base64,
|
||||
full_page=full_page, left_top=left_top, right_bottom=right_bottom)
|
||||
|
||||
def add_init_js(self, script):
|
||||
js_id = self._run_cdp('Page.addScriptToEvaluateOnNewDocument', source=script,
|
||||
includeCommandLineAPI=True)['identifier']
|
||||
self._init_jss.append(js_id)
|
||||
return js_id
|
||||
|
||||
def remove_init_js(self, script_id=None):
|
||||
if script_id is None:
|
||||
for js_id in self._init_jss:
|
||||
self._run_cdp('Page.removeScriptToEvaluateOnNewDocument', identifier=js_id)
|
||||
self._init_jss.clear()
|
||||
|
||||
elif script_id in self._init_jss:
|
||||
self._run_cdp('Page.removeScriptToEvaluateOnNewDocument', identifier=script_id)
|
||||
self._init_jss.remove(script_id)
|
||||
|
||||
def clear_cache(self, session_storage=True, local_storage=True, cache=True, cookies=True):
|
||||
if session_storage and local_storage and cache and cookies:
|
||||
self._run_cdp_loaded("Storage.clearDataForOrigin", origin="*", storageTypes="all")
|
||||
|
||||
if session_storage or local_storage:
|
||||
self._run_cdp_loaded('DOMStorage.enable')
|
||||
i = self._run_cdp('Storage.getStorageKeyForFrame', frameId=self._frame_id)['storageKey']
|
||||
if session_storage:
|
||||
self._run_cdp('DOMStorage.clear', storageId={'storageKey': i, 'isLocalStorage': False})
|
||||
if local_storage:
|
||||
self._run_cdp('DOMStorage.clear', storageId={'storageKey': i, 'isLocalStorage': True})
|
||||
self._run_cdp_loaded('DOMStorage.disable')
|
||||
|
||||
if cache:
|
||||
self._run_cdp_loaded('Network.clearBrowserCache')
|
||||
|
||||
if cookies:
|
||||
self._run_cdp_loaded('Network.clearBrowserCookies')
|
||||
|
||||
def disconnect(self):
|
||||
if self._driver:
|
||||
self._disconnect_flag = True
|
||||
self._driver.stop()
|
||||
self.browser._all_drivers.get(self._driver.id, set()).discard(self._driver)
|
||||
self._disconnect_flag = False
|
||||
|
||||
def reconnect(self, wait=0):
|
||||
t_id = self._target_id
|
||||
self.disconnect()
|
||||
sleep(wait)
|
||||
self.browser.reconnect()
|
||||
self._driver = self.browser._get_driver(t_id, self)
|
||||
self._driver_init(t_id)
|
||||
self._get_document()
|
||||
|
||||
def handle_alert(self, accept=True, send=None, timeout=None, next_one=False):
|
||||
if not isinstance(accept, bool):
|
||||
return self._handle_alert(accept=accept, send=send, timeout=timeout, next_one=next_one)
|
||||
r = self._handle_alert(accept=accept, send=send, timeout=timeout, next_one=next_one)
|
||||
while self._has_alert:
|
||||
sleep(.0001)
|
||||
return r
|
||||
|
||||
def _handle_alert(self, accept=True, send=None, timeout=None, next_one=False):
|
||||
if next_one:
|
||||
self._alert.handle_next = accept
|
||||
self._alert.next_text = send
|
||||
return
|
||||
if timeout is None:
|
||||
timeout = self.timeout
|
||||
timeout = .1 if timeout <= 0 else timeout
|
||||
end_time = perf_counter() + timeout
|
||||
while not self._alert.activated and perf_counter() < end_time:
|
||||
sleep(.01)
|
||||
if not self._alert.activated:
|
||||
return False
|
||||
|
||||
res_text = self._alert.text
|
||||
if not isinstance(accept, bool):
|
||||
return res_text
|
||||
d = {'accept': accept, '_timeout': 0}
|
||||
if self._alert.type == 'prompt' and send is not None:
|
||||
d['promptText'] = send
|
||||
self.driver.run('Page.handleJavaScriptDialog', **d)
|
||||
return res_text
|
||||
|
||||
def _on_alert_open(self, **kwargs):
|
||||
self._alert.activated = True
|
||||
self._alert.text = kwargs['message']
|
||||
self._alert.type = kwargs['type']
|
||||
self._alert.defaultPrompt = kwargs.get('defaultPrompt', None)
|
||||
self._alert.response_accept = None
|
||||
self._alert.response_text = None
|
||||
self._has_alert = True
|
||||
|
||||
if self._alert.auto is not None:
|
||||
if self._alert.auto != 'close':
|
||||
self._handle_alert(self._alert.auto)
|
||||
elif _S.auto_handle_alert is not None:
|
||||
self._handle_alert(_S.auto_handle_alert)
|
||||
elif self._alert.handle_next is not None:
|
||||
self._handle_alert(self._alert.handle_next, self._alert.next_text)
|
||||
self._alert.handle_next = None
|
||||
|
||||
def _on_alert_close(self, **kwargs):
|
||||
self._alert.activated = False
|
||||
self._alert.text = None
|
||||
self._alert.type = None
|
||||
self._alert.defaultPrompt = None
|
||||
self._alert.response_accept = kwargs.get('result')
|
||||
self._alert.response_text = kwargs['userInput']
|
||||
self._has_alert = False
|
||||
|
||||
def _wait_loaded(self, timeout=None):
|
||||
if timeout is None:
|
||||
timeout = self.timeouts.page_load
|
||||
end_time = perf_counter() + timeout
|
||||
while perf_counter() < end_time:
|
||||
if self._ready_state == 'complete':
|
||||
return True
|
||||
elif self._load_mode == 'eager' and self._ready_state in ('interactive',
|
||||
'complete') and not self._is_loading:
|
||||
return True
|
||||
|
||||
sleep(.01)
|
||||
|
||||
try:
|
||||
self.stop_loading()
|
||||
except CDPError:
|
||||
pass
|
||||
return False
|
||||
|
||||
def _d_connect(self, to_url, times=0, interval=1, show_errmsg=False, timeout=None):
|
||||
err = None
|
||||
self._is_loading = True
|
||||
timeout = timeout if timeout is not None else self.timeouts.page_load
|
||||
for t in range(times + 1):
|
||||
err = None
|
||||
end_time = perf_counter() + timeout
|
||||
try:
|
||||
result = self._run_cdp('Page.navigate', frameId=self._frame_id, url=to_url, _timeout=timeout)
|
||||
if 'errorText' in result:
|
||||
err = ConnectionError(_S._lang.join(_S._lang.CONNECT_ERR, INFO=result['errorText']))
|
||||
except TimeoutError:
|
||||
err = TimeoutError(_S._lang.join(_S._lang.TIMEOUT_, _S._lang.PAGE_CONNECT, timeout))
|
||||
|
||||
if err:
|
||||
if t < times:
|
||||
sleep(interval)
|
||||
if show_errmsg:
|
||||
print(f'{_S._lang.RETRY}{t + 1} {to_url}')
|
||||
end_time1 = end_time - perf_counter()
|
||||
while self._ready_state not in ('loading', 'complete') and perf_counter() < end_time1: # 等待出错信息显示
|
||||
sleep(.01)
|
||||
self.stop_loading()
|
||||
continue
|
||||
|
||||
if self._load_mode == 'none':
|
||||
return True
|
||||
|
||||
yu = end_time - perf_counter()
|
||||
ok = self._wait_loaded(1 if yu <= 0 else yu)
|
||||
if not ok:
|
||||
err = TimeoutError(_S._lang.join(_S._lang.TIMEOUT_, _S._lang.PAGE_CONNECT, timeout))
|
||||
if t < times:
|
||||
sleep(interval)
|
||||
if show_errmsg:
|
||||
print(f'{_S._lang.RETRY}{t + 1} {to_url}')
|
||||
continue
|
||||
|
||||
if not err:
|
||||
break
|
||||
|
||||
if err:
|
||||
if show_errmsg:
|
||||
raise err if err is not None else ConnectionError(_S._lang.join(_S._lang.CONNECT_ERR))
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
def _get_screenshot(self, path=None, name=None, as_bytes=None, as_base64=None,
|
||||
full_page=False, left_top=None, right_bottom=None, ele=None):
|
||||
if as_bytes:
|
||||
if as_bytes is True:
|
||||
pic_type = 'png'
|
||||
else:
|
||||
if as_bytes not in ('jpg', 'jpeg', 'png', 'webp'):
|
||||
raise ValueError(_S._lang.join(_S._lang.INCORRECT_VAL_, 'as_bytes',
|
||||
ALLOW_VAL='"jpg", "jpeg", "png", "webp"', CURR_VAL=as_bytes))
|
||||
pic_type = 'jpeg' if as_bytes == 'jpg' else as_bytes
|
||||
|
||||
elif as_base64:
|
||||
if as_base64 is True:
|
||||
pic_type = 'png'
|
||||
else:
|
||||
if as_base64 not in ('jpg', 'jpeg', 'png', 'webp'):
|
||||
raise ValueError(_S._lang.join(_S._lang.INCORRECT_VAL_, 'as_base64',
|
||||
ALLOW_VAL='"jpg", "jpeg", "png", "webp"', CURR_VAL=as_base64))
|
||||
pic_type = 'jpeg' if as_base64 == 'jpg' else as_base64
|
||||
|
||||
else:
|
||||
path = str(path).rstrip('\\/') if path else '.'
|
||||
if not path.endswith(('.jpg', '.jpeg', '.png', '.webp')):
|
||||
if not name:
|
||||
name = f'{self.title}.jpg'
|
||||
elif not name.endswith(('.jpg', '.jpeg', '.png', '.webp')):
|
||||
name = f'{name}.jpg'
|
||||
path = f'{path}{sep}{make_valid_name(name)}'
|
||||
|
||||
path = Path(path)
|
||||
pic_type = path.suffix.lower()
|
||||
pic_type = 'jpeg' if pic_type == '.jpg' else pic_type[1:]
|
||||
|
||||
if full_page:
|
||||
width, height = self.rect.size
|
||||
if width == 0 or height == 0:
|
||||
raise RuntimeError(_S._lang.join(_S._lang.ZERO_PAGE_SIZE))
|
||||
vp = {'x': 0, 'y': 0, 'width': width, 'height': height, 'scale': 1}
|
||||
args = {'format': pic_type, 'captureBeyondViewport': True, 'clip': vp}
|
||||
else:
|
||||
if left_top or right_bottom:
|
||||
if not left_top:
|
||||
left_top = (0, 0)
|
||||
if not right_bottom:
|
||||
right_bottom = self.rect.size
|
||||
x, y = left_top
|
||||
w = right_bottom[0] - x
|
||||
h = right_bottom[1] - y
|
||||
|
||||
v = not (location_in_viewport(self, x, y) and
|
||||
location_in_viewport(self, right_bottom[0], right_bottom[1]))
|
||||
if v and (self._run_js('return document.body.scrollHeight > window.innerHeight;') and
|
||||
not self._run_js('return document.body.scrollWidth > window.innerWidth;')):
|
||||
x += 10
|
||||
|
||||
vp = {'x': x, 'y': y, 'width': w, 'height': h, 'scale': 1}
|
||||
args = {'format': pic_type, 'captureBeyondViewport': v, 'clip': vp}
|
||||
|
||||
else:
|
||||
args = {'format': pic_type}
|
||||
|
||||
if pic_type == 'jpeg':
|
||||
args['quality'] = 100
|
||||
png = self._run_cdp_loaded('Page.captureScreenshot', **args)['data']
|
||||
|
||||
if as_base64:
|
||||
return png
|
||||
|
||||
from base64 import b64decode
|
||||
png = b64decode(png)
|
||||
|
||||
if as_bytes:
|
||||
return png
|
||||
|
||||
path.parent.mkdir(parents=True, exist_ok=True)
|
||||
with open(path, 'wb') as f:
|
||||
f.write(png)
|
||||
return str(path.absolute())
|
||||
|
||||
|
||||
class Timeout(object):
|
||||
def __init__(self, base=None, page_load=None, script=None):
|
||||
self.base = 10 if base is None else base
|
||||
self.page_load = 30 if page_load is None else page_load
|
||||
self.script = 30 if script is None else script
|
||||
|
||||
def __repr__(self):
|
||||
return str({'base': self.base, 'page_load': self.page_load, 'script': self.script})
|
||||
|
||||
@property
|
||||
def as_dict(self):
|
||||
return {'base': self.base, 'page_load': self.page_load, 'script': self.script}
|
||||
|
||||
|
||||
class Alert(object):
|
||||
def __init__(self, auto=None):
|
||||
self.activated = False
|
||||
self.text = None
|
||||
self.type = None
|
||||
self.defaultPrompt = None
|
||||
self.response_accept = None
|
||||
self.response_text = None
|
||||
self.handle_next = None
|
||||
self.next_text = None
|
||||
self.auto = auto
|
||||
|
||||
|
||||
def close_privacy_dialog(page, tid):
|
||||
"""关闭隐私声明弹窗
|
||||
:param page: ChromiumBase对象
|
||||
:param tid: tab id
|
||||
:return: None
|
||||
"""
|
||||
try:
|
||||
driver = page.browser._get_driver(tid)
|
||||
driver.run('Runtime.enable')
|
||||
driver.run('DOM.enable')
|
||||
driver.run('DOM.getDocument')
|
||||
sid = driver.run('DOM.performSearch', query='//*[name()="privacy-sandbox-notice-dialog-app"]',
|
||||
includeUserAgentShadowDOM=True)['searchId']
|
||||
r = driver.run('DOM.getSearchResults', searchId=sid, fromIndex=0, toIndex=1)['nodeIds'][0]
|
||||
end_time = perf_counter() + 3
|
||||
while perf_counter() < end_time:
|
||||
try:
|
||||
r = driver.run('DOM.describeNode', nodeId=r)['node']['shadowRoots'][0]['backendNodeId']
|
||||
break
|
||||
except KeyError:
|
||||
pass
|
||||
sleep(.05)
|
||||
driver.run('DOM.discardSearchResults', searchId=sid)
|
||||
r = driver.run('DOM.resolveNode', backendNodeId=r)['object']['objectId']
|
||||
r = driver.run('Runtime.callFunctionOn', objectId=r,
|
||||
functionDeclaration='function(){return this.getElementById("ackButton");}')['result']['objectId']
|
||||
driver.run('Runtime.callFunctionOn', objectId=r, functionDeclaration='function(){return this.click();}')
|
||||
driver.close()
|
||||
|
||||
except:
|
||||
pass
|
||||
@@ -0,0 +1,681 @@
|
||||
# -*- coding:utf-8 -*-
|
||||
"""
|
||||
@Author : g1879
|
||||
@Contact : g1879@qq.com
|
||||
@Website : https://DrissionPage.cn
|
||||
@Copyright: (c) 2020 by g1879, Inc. All Rights Reserved.
|
||||
"""
|
||||
from pathlib import Path
|
||||
from typing import Union, Tuple, Any, Optional, Literal
|
||||
|
||||
from requests import Session
|
||||
|
||||
from .chromium_page import ChromiumPage
|
||||
from .chromium_tab import ChromiumTab
|
||||
from .mix_tab import MixTab
|
||||
from .web_page import WebPage
|
||||
from .._base.base import BasePage
|
||||
from .._base.chromium import Chromium
|
||||
from .._base.driver import Driver
|
||||
from .._elements.chromium_element import ChromiumElement
|
||||
from .._elements.session_element import SessionElement
|
||||
from .._functions.cookies import CookiesList
|
||||
from .._functions.elements import SessionElementsList, ChromiumElementsList
|
||||
from .._pages.chromium_frame import ChromiumFrame
|
||||
from .._units.actions import Actions
|
||||
from .._units.console import Console
|
||||
from .._units.listener import Listener
|
||||
from .._units.rect import TabRect, FrameRect
|
||||
from .._units.screencast import Screencast
|
||||
from .._units.scroller import Scroller, PageScroller
|
||||
from .._units.setter import ChromiumBaseSetter
|
||||
from .._units.states import PageStates
|
||||
from .._units.waiter import BaseWaiter
|
||||
|
||||
PIC_TYPE = Literal['jpg', 'jpeg', 'png', 'webp', True]
|
||||
|
||||
|
||||
class ChromiumBase(BasePage):
|
||||
"""标签页、Frame、Page基类"""
|
||||
_tab: Union[ChromiumTab, MixTab, ChromiumFrame, ChromiumPage, WebPage] = ...
|
||||
_browser: Chromium = ...
|
||||
_driver: Optional[Driver] = ...
|
||||
_frame_id: str = ...
|
||||
_is_reading: bool = ...
|
||||
_is_timeout: bool = ...
|
||||
_timeouts: Timeout = ...
|
||||
_first_run: bool = ...
|
||||
_is_loading: Optional[bool] = ...
|
||||
_load_mode: str = ...
|
||||
_scroll: Optional[Scroller] = ...
|
||||
_url: str = ...
|
||||
_root_id: Optional[str] = ...
|
||||
_upload_list: Optional[list] = ...
|
||||
_wait: Optional[BaseWaiter] = ...
|
||||
_set: Optional[ChromiumBaseSetter] = ...
|
||||
_screencast: Optional[Screencast] = ...
|
||||
_actions: Optional[Actions] = ...
|
||||
_listener: Optional[Listener] = ...
|
||||
_states: Optional[PageStates] = ...
|
||||
_alert: Alert = ...
|
||||
_has_alert: bool = ...
|
||||
_auto_handle_alert: Optional[bool] = ...
|
||||
_doc_got: bool = ...
|
||||
_load_end_time: float = ...
|
||||
_init_jss: list = ...
|
||||
_ready_state: Optional[str] = ...
|
||||
_rect: Optional[TabRect] = ...
|
||||
_console: Optional[Console] = ...
|
||||
_disconnect_flag: bool = ...
|
||||
_type: str = ...
|
||||
|
||||
def __init__(self,
|
||||
browser: Chromium,
|
||||
tab_id: str = None):
|
||||
"""
|
||||
:param browser: Chromium
|
||||
:param tab_id: 要控制的tab id,不指定默认为激活的标签页
|
||||
"""
|
||||
...
|
||||
|
||||
def __call__(self,
|
||||
locator: Union[Tuple[str, str], str, ChromiumElement],
|
||||
index: int = 1,
|
||||
timeout: float = None) -> ChromiumElement:
|
||||
"""在内部查找元素
|
||||
例:ele = page('@id=ele_id')
|
||||
:param locator: 元素的定位信息,可以是loc元组,或查询字符串
|
||||
:param index: 获取第几个元素,从1开始,可传入负数获取倒数第几个
|
||||
:param timeout: 超时时间(秒)
|
||||
:return: ChromiumElement对象
|
||||
"""
|
||||
...
|
||||
|
||||
def _d_set_runtime_settings(self) -> None: ...
|
||||
|
||||
def _connect_browser(self, target_id: str = None) -> None:
|
||||
"""连接浏览器,在第一次时运行
|
||||
:param target_id: 要控制的target id,不指定默认为激活的标签页
|
||||
:return: None
|
||||
"""
|
||||
...
|
||||
|
||||
def _driver_init(self, target_id: str) -> None:
|
||||
"""新建页面、页面刷新后要进行的cdp参数初始化
|
||||
:param target_id: 要跳转到的target id
|
||||
:return: None
|
||||
"""
|
||||
...
|
||||
|
||||
def _get_document(self, timeout: float = 10) -> bool:
|
||||
"""获取页面文档
|
||||
:param timeout: 超时时间(秒)
|
||||
:return: 是否获取成功
|
||||
"""
|
||||
...
|
||||
|
||||
def _onFrameDetached(self, **kwargs) -> None: ...
|
||||
|
||||
def _onFrameAttached(self, **kwargs) -> None: ...
|
||||
|
||||
def _onFrameStartedLoading(self, **kwargs):
|
||||
"""页面开始加载时执行"""
|
||||
...
|
||||
|
||||
def _onFrameNavigated(self, **kwargs):
|
||||
"""页面跳转时执行"""
|
||||
...
|
||||
|
||||
def _onDomContentEventFired(self, **kwargs):
|
||||
"""在页面刷新、变化后重新读取页面内容"""
|
||||
...
|
||||
|
||||
def _onLoadEventFired(self, **kwargs):
|
||||
"""在页面刷新、变化后重新读取页面内容"""
|
||||
...
|
||||
|
||||
def _onFrameStoppedLoading(self, **kwargs):
|
||||
"""页面加载完成后执行"""
|
||||
...
|
||||
|
||||
def _onFileChooserOpened(self, **kwargs):
|
||||
"""文件选择框打开时执行"""
|
||||
...
|
||||
|
||||
def _wait_to_stop(self):
|
||||
"""eager策略超时时使页面停止加载"""
|
||||
...
|
||||
|
||||
@property
|
||||
def wait(self) -> BaseWaiter:
|
||||
"""返回用于等待的对象"""
|
||||
...
|
||||
|
||||
@property
|
||||
def set(self) -> ChromiumBaseSetter:
|
||||
"""返回用于设置的对象"""
|
||||
...
|
||||
|
||||
@property
|
||||
def screencast(self) -> Screencast:
|
||||
"""返回用于录屏的对象"""
|
||||
...
|
||||
|
||||
@property
|
||||
def actions(self) -> Actions:
|
||||
"""返回用于执行动作链的对象"""
|
||||
...
|
||||
|
||||
@property
|
||||
def listen(self) -> Listener:
|
||||
"""返回用于聆听数据包的对象"""
|
||||
...
|
||||
|
||||
@property
|
||||
def states(self) -> PageStates:
|
||||
"""返回用于获取状态信息的对象"""
|
||||
...
|
||||
|
||||
@property
|
||||
def scroll(self) -> PageScroller:
|
||||
"""返回用于滚动滚动条的对象"""
|
||||
...
|
||||
|
||||
@property
|
||||
def rect(self) -> Union[TabRect, FrameRect]:
|
||||
"""返回获取窗口坐标和大小的对象"""
|
||||
...
|
||||
|
||||
@property
|
||||
def console(self) -> Console:
|
||||
"""返回获取控制台信息的对象"""
|
||||
...
|
||||
|
||||
@property
|
||||
def timeout(self) -> float:
|
||||
"""返回timeout设置"""
|
||||
...
|
||||
|
||||
@property
|
||||
def timeouts(self) -> Timeout:
|
||||
"""返回timeouts设置"""
|
||||
...
|
||||
|
||||
@property
|
||||
def browser(self) -> Chromium:
|
||||
"""返回浏览器对象"""
|
||||
...
|
||||
|
||||
@property
|
||||
def driver(self) -> Driver:
|
||||
"""返回用于控制浏览器的Driver对象"""
|
||||
...
|
||||
|
||||
@property
|
||||
def title(self) -> str:
|
||||
"""返回当前页面title"""
|
||||
...
|
||||
|
||||
@property
|
||||
def url(self) -> str:
|
||||
"""返回当前页面url"""
|
||||
...
|
||||
|
||||
@property
|
||||
def _browser_url(self) -> str:
|
||||
"""用于被MixTab覆盖"""
|
||||
...
|
||||
|
||||
@property
|
||||
def html(self) -> str:
|
||||
"""返回当前页面html文本"""
|
||||
...
|
||||
|
||||
@property
|
||||
def json(self) -> Union[dict, None]:
|
||||
"""当返回内容是json格式时,返回对应的字典,非json格式时返回None"""
|
||||
...
|
||||
|
||||
@property
|
||||
def tab_id(self) -> str:
|
||||
"""返回当前标签页id"""
|
||||
...
|
||||
|
||||
@property
|
||||
def _target_id(self) -> str:
|
||||
"""返回当前标签页id"""
|
||||
...
|
||||
|
||||
@property
|
||||
def active_ele(self) -> ChromiumElement:
|
||||
"""返回当前焦点所在元素"""
|
||||
...
|
||||
|
||||
@property
|
||||
def load_mode(self) -> Literal['none', 'normal', 'eager']:
|
||||
"""返回页面加载策略,有3种:'none'、'normal'、'eager'"""
|
||||
...
|
||||
|
||||
@property
|
||||
def user_agent(self) -> str:
|
||||
"""返回user agent"""
|
||||
...
|
||||
|
||||
@property
|
||||
def upload_list(self) -> list:
|
||||
"""返回等待上传文件列表"""
|
||||
...
|
||||
|
||||
@property
|
||||
def session(self) -> Session:
|
||||
"""返回用于转换模式或download的Session对象"""
|
||||
...
|
||||
|
||||
@property
|
||||
def _js_ready_state(self) -> str:
|
||||
"""返回js获取的ready state信息"""
|
||||
...
|
||||
|
||||
def run_cdp(self, cmd: str, **cmd_args) -> dict:
|
||||
"""执行Chrome DevTools Protocol语句
|
||||
:param cmd: 协议项目
|
||||
:param cmd_args: 参数
|
||||
:return: 执行的结果
|
||||
"""
|
||||
...
|
||||
|
||||
def run_cdp_loaded(self, cmd: str, **cmd_args) -> dict:
|
||||
"""执行Chrome DevTools Protocol语句,执行前等待页面加载完毕
|
||||
:param cmd: 协议项目
|
||||
:param cmd_args: 参数
|
||||
:return: 执行的结果
|
||||
"""
|
||||
...
|
||||
|
||||
def _run_cdp(self, cmd: str, **cmd_args) -> dict:
|
||||
"""执行Chrome DevTools Protocol语句
|
||||
:param cmd: 协议项目
|
||||
:param cmd_args: 参数
|
||||
:return: 执行的结果
|
||||
"""
|
||||
...
|
||||
|
||||
def _run_cdp_loaded(self, cmd: str, **cmd_args) -> dict:
|
||||
"""执行Chrome DevTools Protocol语句,执行前等待页面加载完毕
|
||||
:param cmd: 协议项目
|
||||
:param cmd_args: 参数
|
||||
:return: 执行的结果
|
||||
"""
|
||||
...
|
||||
|
||||
def run_js(self, script: Union[str, Path], *args, as_expr: bool = False, timeout: float = None) -> Any:
|
||||
"""运行javascript代码
|
||||
:param script: js文本或js文件路径
|
||||
:param args: 参数,按顺序在js文本中对应arguments[0]、arguments[1]...
|
||||
:param as_expr: 是否作为表达式运行,为True时args无效
|
||||
:param timeout: js超时时间(秒),为None则使用页面timeouts.script设置
|
||||
:return: 运行的结果
|
||||
"""
|
||||
...
|
||||
|
||||
def run_js_loaded(self, script: Union[str, Path], *args, as_expr: bool = False, timeout: float = None) -> Any:
|
||||
"""运行javascript代码,执行前等待页面加载完毕
|
||||
:param script: js文本或js文件路径
|
||||
:param args: 参数,按顺序在js文本中对应arguments[0]、arguments[1]...
|
||||
:param as_expr: 是否作为表达式运行,为True时args无效
|
||||
:param timeout: js超时时间(秒),为None则使用页面timeouts.script属性值
|
||||
:return: 运行的结果
|
||||
"""
|
||||
...
|
||||
|
||||
def _run_js(self, script: Union[str, Path], *args, as_expr: bool = False, timeout: float = None) -> Any:
|
||||
"""运行javascript代码
|
||||
:param script: js文本或js文件路径
|
||||
:param args: 参数,按顺序在js文本中对应arguments[0]、arguments[1]...
|
||||
:param as_expr: 是否作为表达式运行,为True时args无效
|
||||
:param timeout: js超时时间(秒),为None则使用页面timeouts.script设置
|
||||
:return: 运行的结果
|
||||
"""
|
||||
...
|
||||
|
||||
def _run_js_loaded(self, script: Union[str, Path], *args, as_expr: bool = False, timeout: float = None) -> Any:
|
||||
"""运行javascript代码,执行前等待页面加载完毕
|
||||
:param script: js文本或js文件路径
|
||||
:param args: 参数,按顺序在js文本中对应arguments[0]、arguments[1]...
|
||||
:param as_expr: 是否作为表达式运行,为True时args无效
|
||||
:param timeout: js超时时间(秒),为None则使用页面timeouts.script属性值
|
||||
:return: 运行的结果
|
||||
"""
|
||||
...
|
||||
|
||||
def run_async_js(self, script: Union[str, Path], *args, as_expr: bool = False) -> None:
|
||||
"""以异步方式执行js代码或js文件路径
|
||||
:param script: js文本
|
||||
:param args: 参数,按顺序在js文本中对应arguments[0]、arguments[1]...
|
||||
:param as_expr: 是否作为表达式运行,为True时args无效
|
||||
:return: None
|
||||
"""
|
||||
...
|
||||
|
||||
def get(self, url: str, show_errmsg: bool = False, retry: int = None,
|
||||
interval: float = None, timeout: float = None) -> Union[None, bool]:
|
||||
"""访问url
|
||||
:param url: 目标url
|
||||
:param show_errmsg: 是否显示和抛出异常
|
||||
:param retry: 重试次数,为None时使用页面对象retry_times属性值
|
||||
:param interval: 重试间隔(秒),为None时使用页面对象retry_interval属性值
|
||||
:param timeout: 连接超时时间(秒),为None时使用页面对象timeouts.page_load属性值
|
||||
:return: 目标url是否可用
|
||||
"""
|
||||
...
|
||||
|
||||
def cookies(self, all_domains: bool = False, all_info: bool = False) -> CookiesList:
|
||||
"""返回cookies信息
|
||||
:param all_domains: 是否返回所有域的cookies
|
||||
:param all_info: 是否返回所有信息,为False时只返回name、value、domain
|
||||
:return: cookies信息
|
||||
"""
|
||||
...
|
||||
|
||||
def ele(self,
|
||||
locator: Union[Tuple[str, str], str, ChromiumElement, ChromiumFrame],
|
||||
index: int = 1,
|
||||
timeout: float = None) -> ChromiumElement:
|
||||
"""获取一个符合条件的元素对象
|
||||
:param locator: 定位符或元素对象
|
||||
:param index: 获取第几个元素,从1开始,可传入负数获取倒数第几个
|
||||
:param timeout: 查找超时时间(秒),默认与页面等待时间一致
|
||||
:return: ChromiumElement对象
|
||||
"""
|
||||
...
|
||||
|
||||
def eles(self,
|
||||
locator: Union[Tuple[str, str], str],
|
||||
timeout: float = None) -> ChromiumElementsList:
|
||||
"""获取所有符合条件的元素对象
|
||||
:param locator: 定位符或元素对象
|
||||
:param timeout: 查找超时时间(秒),默认与页面等待时间一致
|
||||
:return: ChromiumElement对象组成的列表
|
||||
"""
|
||||
...
|
||||
|
||||
def s_ele(self,
|
||||
locator: Union[Tuple[str, str], str] = None,
|
||||
index: int = 1,
|
||||
timeout: float = None) -> SessionElement:
|
||||
"""查找一个符合条件的元素以SessionElement形式返回,处理复杂页面时效率很高
|
||||
:param locator: 元素的定位信息,可以是loc元组,或查询字符串
|
||||
:param index: 获取第几个,从1开始,可传入负数获取倒数第几个
|
||||
:param timeout: 查找元素超时时间(秒),默认与页面等待时间一致
|
||||
:return: SessionElement对象或属性、文本
|
||||
"""
|
||||
...
|
||||
|
||||
def s_eles(self,
|
||||
locator: Union[Tuple[str, str], str],
|
||||
timeout: float = None) -> SessionElementsList:
|
||||
"""查找所有符合条件的元素以SessionElement列表形式返回
|
||||
:param locator: 元素的定位信息,可以是loc元组,或查询字符串
|
||||
:param timeout: 查找元素超时时间(秒),默认与页面等待时间一致
|
||||
:return: SessionElement对象组成的列表
|
||||
"""
|
||||
...
|
||||
|
||||
def _find_elements(self,
|
||||
locator: Union[Tuple[str, str], str, ChromiumElement, ChromiumFrame],
|
||||
timeout: float,
|
||||
index: Optional[int] = 1,
|
||||
relative: bool = False,
|
||||
raise_err: bool = None) -> Union[ChromiumElement, ChromiumFrame, ChromiumElementsList]:
|
||||
"""执行元素查找
|
||||
:param locator: 定位符或元素对象
|
||||
:param timeout: 查找超时时间(秒)
|
||||
:param index: 第几个结果,从1开始,可传入负数获取倒数第几个,为None返回所有
|
||||
:param relative: MixTab用的表示是否相对定位的参数
|
||||
:param raise_err: 找不到元素是是否抛出异常,为None时根据全局设置
|
||||
:return: ChromiumElement对象或元素对象组成的列表
|
||||
"""
|
||||
...
|
||||
|
||||
def refresh(self, ignore_cache: bool = False) -> None:
|
||||
"""刷新当前页面
|
||||
:param ignore_cache: 是否忽略缓存
|
||||
:return: None
|
||||
"""
|
||||
...
|
||||
|
||||
def forward(self, steps: int = 1) -> None:
|
||||
"""在浏览历史中前进若干步
|
||||
:param steps: 前进步数
|
||||
:return: None
|
||||
"""
|
||||
...
|
||||
|
||||
def back(self, steps: int = 1) -> None:
|
||||
"""在浏览历史中后退若干步
|
||||
:param steps: 后退步数
|
||||
:return: None
|
||||
"""
|
||||
...
|
||||
|
||||
def _forward_or_back(self, steps: int) -> None:
|
||||
"""执行浏览器前进或后退,会跳过url相同的历史记录
|
||||
:param steps: 步数
|
||||
:return: None
|
||||
"""
|
||||
...
|
||||
|
||||
def stop_loading(self) -> None:
|
||||
"""页面停止加载"""
|
||||
...
|
||||
|
||||
def remove_ele(self, loc_or_ele: Union[ChromiumElement, ChromiumFrame, str, Tuple[str, str]]) -> None:
|
||||
"""从页面上删除一个元素
|
||||
:param loc_or_ele: 元素对象或定位符
|
||||
:return: None
|
||||
"""
|
||||
...
|
||||
|
||||
def add_ele(self,
|
||||
html_or_info: Union[str, Tuple[str, dict]],
|
||||
insert_to: Union[ChromiumElement, str, Tuple[str, str], None] = None,
|
||||
before: Union[ChromiumElement, str, Tuple[str, str], None] = None) -> Union[
|
||||
ChromiumElement, ChromiumFrame]:
|
||||
"""新建一个元素
|
||||
:param html_or_info: 新元素的html文本或信息。信息格式为:(tag, {attr1: value, ...})
|
||||
:param insert_to: 插入到哪个元素中,可接收元素对象和定位符,为None且为html添加到body,不为html不插入
|
||||
:param before: 在哪个子节点前面插入,可接收对象和定位符,为None插入到父元素末尾
|
||||
:return: 元素对象
|
||||
"""
|
||||
...
|
||||
|
||||
def get_frame(self,
|
||||
loc_ind_ele: Union[str, int, tuple, ChromiumFrame, ChromiumElement],
|
||||
timeout: float = None) -> ChromiumFrame:
|
||||
"""获取页面中一个frame对象
|
||||
:param loc_ind_ele: 定位符、iframe序号、ChromiumFrame对象,序号从1开始,可传入负数获取倒数第几个
|
||||
:param timeout: 查找元素超时时间(秒)
|
||||
:return: ChromiumFrame对象
|
||||
"""
|
||||
...
|
||||
|
||||
def get_frames(self,
|
||||
locator: Union[str, tuple] = None,
|
||||
timeout: float = None) -> ChromiumElementsList:
|
||||
"""获取所有符合条件的frame对象
|
||||
:param locator: 定位符,为None时返回所有
|
||||
:param timeout: 查找超时时间(秒)
|
||||
:return: ChromiumFrame对象组成的列表
|
||||
"""
|
||||
...
|
||||
|
||||
def session_storage(self, item: str = None) -> Union[str, dict, None]:
|
||||
"""返回sessionStorage信息,不设置item则获取全部
|
||||
:param item: 要获取的项,不设置则返回全部
|
||||
:return: sessionStorage一个或所有项内容
|
||||
"""
|
||||
...
|
||||
|
||||
def local_storage(self, item: str = None) -> Union[str, dict, None]:
|
||||
"""返回localStorage信息,不设置item则获取全部
|
||||
:param item: 要获取的项目,不设置则返回全部
|
||||
:return: localStorage一个或所有项内容
|
||||
"""
|
||||
...
|
||||
|
||||
def get_screenshot(self, path: [str, Path] = None, name: str = None, as_bytes: PIC_TYPE = None,
|
||||
as_base64: PIC_TYPE = None, full_page: bool = False, left_top: Tuple[int, int] = None,
|
||||
right_bottom: Tuple[int, int] = None) -> Union[str, bytes]:
|
||||
"""对页面进行截图,可对整个网页、可见网页、指定范围截图。对可视范围外截图需要90以上版本浏览器支持
|
||||
:param path: 保存路径
|
||||
:param name: 完整文件名,后缀可选 'jpg','jpeg','png','webp'
|
||||
:param as_bytes: 是否以字节形式返回图片,可选 'jpg','jpeg','png','webp',生效时path参数和as_base64参数无效
|
||||
:param as_base64: 是否以base64字符串形式返回图片,可选 'jpg','jpeg','png','webp',生效时path参数无效
|
||||
:param full_page: 是否整页截图,为True截取整个网页,为False截取可视窗口
|
||||
:param left_top: 截取范围左上角坐标
|
||||
:param right_bottom: 截取范围右下角角坐标
|
||||
:return: 图片完整路径或字节文本
|
||||
"""
|
||||
...
|
||||
|
||||
def add_init_js(self, script: str) -> str:
|
||||
"""添加初始化脚本,在页面加载任何脚本前执行
|
||||
:param script: js文本
|
||||
:return: 添加的脚本的id
|
||||
"""
|
||||
...
|
||||
|
||||
def remove_init_js(self, script_id: str = None) -> None:
|
||||
"""删除初始化脚本,js_id传入None时删除所有
|
||||
:param script_id: 脚本的id
|
||||
:return: None
|
||||
"""
|
||||
...
|
||||
|
||||
def clear_cache(self, session_storage: bool = True, local_storage: bool = True, cache: bool = True,
|
||||
cookies: bool = True) -> None:
|
||||
"""清除缓存,可选要清除的项
|
||||
:param session_storage: 是否清除sessionStorage
|
||||
:param local_storage: 是否清除localStorage
|
||||
:param cache: 是否清除cache
|
||||
:param cookies: 是否清除cookies
|
||||
:return: None
|
||||
"""
|
||||
...
|
||||
|
||||
def disconnect(self) -> None:
|
||||
"""断开与页面的连接,不关闭页面"""
|
||||
...
|
||||
|
||||
def reconnect(self, wait: float = 0) -> None:
|
||||
"""断开与页面原来的页面,重新建立连接
|
||||
:param wait: 断开后等待若干秒再连接
|
||||
:return: None
|
||||
"""
|
||||
...
|
||||
|
||||
def handle_alert(self,
|
||||
accept: Optional[bool] = True,
|
||||
send: str = None,
|
||||
timeout: float = None,
|
||||
next_one: bool = False) -> Union[str, False]:
|
||||
"""处理提示框,可以自动等待提示框出现
|
||||
:param accept: True表示确认,False表示取消,为None不会按按钮但依然返回文本值
|
||||
:param send: 处理prompt提示框时可输入文本
|
||||
:param timeout: 等待提示框出现的超时时间(秒),为None则使用self.timeout属性的值
|
||||
:param next_one: 是否处理下一个出现的提示框,为True时timeout参数无效
|
||||
:return: 提示框内容文本,未等到提示框则返回False
|
||||
"""
|
||||
...
|
||||
|
||||
def _handle_alert(self,
|
||||
accept: Optional[bool] = True,
|
||||
send: str = None,
|
||||
timeout: float = None,
|
||||
next_one: bool = False) -> Union[str, False]:
|
||||
"""处理提示框,可以自动等待提示框出现
|
||||
:param accept: True表示确认,False表示取消,其它值不会按按钮但依然返回文本值
|
||||
:param send: 处理prompt提示框时可输入文本
|
||||
:param timeout: 等待提示框出现的超时时间(秒),为None则使用self.timeout属性的值
|
||||
:param next_one: 是否处理下一个出现的提示框,为True时timeout参数无效
|
||||
:return: 提示框内容文本,未等到提示框则返回False
|
||||
"""
|
||||
...
|
||||
|
||||
def _on_alert_open(self, **kwargs):
|
||||
"""alert出现时触发的方法"""
|
||||
...
|
||||
|
||||
def _on_alert_close(self, **kwargs):
|
||||
"""alert关闭时触发的方法"""
|
||||
...
|
||||
|
||||
def _wait_loaded(self, timeout: float = None) -> bool:
|
||||
"""等待页面加载完成,超时触发停止加载
|
||||
:param timeout: 超时时间(秒)
|
||||
:return: 是否成功,超时返回False
|
||||
"""
|
||||
...
|
||||
|
||||
def _d_connect(self, to_url: str, times: int = 0, interval: float = 1, show_errmsg: bool = False,
|
||||
timeout: float = None) -> Union[bool, None]:
|
||||
"""尝试连接,重试若干次
|
||||
:param to_url: 要访问的url
|
||||
:param times: 重试次数
|
||||
:param interval: 重试间隔(秒)
|
||||
:param show_errmsg: 是否抛出异常
|
||||
:param timeout: 连接超时时间(秒)
|
||||
:return: 是否成功,返回None表示不确定
|
||||
"""
|
||||
...
|
||||
|
||||
def _get_screenshot(self, path: [str, Path] = None, name: str = None, as_bytes: PIC_TYPE = None,
|
||||
as_base64: PIC_TYPE = None, full_page: bool = False, left_top: Tuple[float, float] = None,
|
||||
right_bottom: Tuple[float, float] = None, ele: ChromiumElement = None) -> Union[str, bytes]:
|
||||
"""对页面进行截图,可对整个网页、可见网页、指定范围截图。对可视范围外截图需要90以上版本浏览器支持
|
||||
:param path: 保存路径
|
||||
:param name: 完整文件名,后缀可选 'jpg','jpeg','png','webp'
|
||||
:param as_bytes: 是否以字节形式返回图片,可选 'jpg','jpeg','png','webp',生效时path参数和as_base64参数无效
|
||||
:param as_base64: 是否以base64字符串形式返回图片,可选 'jpg','jpeg','png','webp',生效时path参数无效
|
||||
:param full_page: 是否整页截图,为True截取整个网页,为False截取可视窗口
|
||||
:param left_top: 截取范围左上角坐标
|
||||
:param right_bottom: 截取范围右下角角坐标
|
||||
:param ele: 为异域iframe内元素截图设置
|
||||
:return: 图片完整路径或字节文本
|
||||
"""
|
||||
...
|
||||
|
||||
|
||||
class Timeout(object):
|
||||
"""用于保存d模式timeout信息的类"""
|
||||
base: float = ...
|
||||
page_load: float = ...
|
||||
script: float = ...
|
||||
|
||||
def __init__(self, base=None, page_load=None, script=None):
|
||||
"""
|
||||
:param base: 默认超时时间
|
||||
:param page_load: 页面加载超时时间
|
||||
:param script: js超时时间
|
||||
"""
|
||||
...
|
||||
|
||||
@property
|
||||
def as_dict(self) -> dict:
|
||||
"""以dict格式返回timeout设置"""
|
||||
...
|
||||
|
||||
|
||||
class Alert(object):
|
||||
"""用于保存alert信息的类"""
|
||||
activated: Optional[bool] = ...
|
||||
text: Optional[str] = ...
|
||||
type: Optional[str] = ...
|
||||
defaultPrompt: Optional[str] = ...
|
||||
response_accept: Optional[str] = ...
|
||||
response_text: Optional[str] = ...
|
||||
handle_next: Optional[bool] = ...
|
||||
next_text: Optional[str] = ...
|
||||
auto: Optional[bool] = ...
|
||||
|
||||
def __init__(self, auto: bool = None): ...
|
||||
@@ -0,0 +1,457 @@
|
||||
# -*- coding:utf-8 -*-
|
||||
"""
|
||||
@Author : g1879
|
||||
@Contact : g1879@qq.com
|
||||
@Website : https://DrissionPage.cn
|
||||
@Copyright: (c) 2020 by g1879, Inc. All Rights Reserved.
|
||||
"""
|
||||
from copy import copy
|
||||
from re import search, findall, DOTALL
|
||||
from time import sleep, perf_counter
|
||||
|
||||
from .._elements.chromium_element import ChromiumElement
|
||||
from .._functions.settings import Settings as _S
|
||||
from .._pages.chromium_base import ChromiumBase
|
||||
from .._units.listener import FrameListener
|
||||
from .._units.rect import FrameRect
|
||||
from .._units.scroller import FrameScroller
|
||||
from .._units.setter import ChromiumFrameSetter
|
||||
from .._units.states import FrameStates
|
||||
from .._units.waiter import FrameWaiter
|
||||
from ..errors import ContextLostError, ElementLostError, PageDisconnectedError, JavaScriptError
|
||||
|
||||
|
||||
class ChromiumFrame(ChromiumBase):
|
||||
_Frames = {}
|
||||
|
||||
def __new__(cls, owner, ele, info=None):
|
||||
fid = info['node']['frameId'] if info else owner._run_cdp('DOM.describeNode',
|
||||
backendNodeId=ele._backend_id)['node']['frameId']
|
||||
if _S.singleton_tab_obj and fid in cls._Frames:
|
||||
r = cls._Frames[fid]
|
||||
while not hasattr(r, '_type') or r._type != 'ChromiumFrame':
|
||||
sleep(.01)
|
||||
return r
|
||||
r = object.__new__(cls)
|
||||
cls._Frames[fid] = r
|
||||
return r
|
||||
|
||||
def __init__(self, owner, ele, info=None):
|
||||
if _S.singleton_tab_obj and hasattr(self, '_created'):
|
||||
return
|
||||
self._created = True
|
||||
|
||||
self._tab = owner._tab
|
||||
self._target_page = owner
|
||||
self._backend_id = ele._backend_id
|
||||
self._frame_ele = ele
|
||||
self._reloading = False
|
||||
|
||||
try:
|
||||
node = info['node'] if info else owner._run_cdp('DOM.describeNode', backendNodeId=ele._backend_id)['node']
|
||||
self._frame_id = node['frameId']
|
||||
if self._is_inner_frame():
|
||||
self._is_diff_domain = False
|
||||
self.doc_ele = ChromiumElement(self._target_page, backend_id=node['contentDocument']['backendNodeId'])
|
||||
super().__init__(owner.browser, owner.driver.id)
|
||||
else:
|
||||
self._is_diff_domain = True
|
||||
delattr(self, '_frame_id')
|
||||
super().__init__(owner.browser, node['frameId'])
|
||||
obj_id = super()._run_js('document;', as_expr=True)['objectId']
|
||||
self.doc_ele = ChromiumElement(self, obj_id=obj_id)
|
||||
|
||||
except Exception as e:
|
||||
ChromiumFrame._Frames.pop(self._frame_id, None)
|
||||
raise e
|
||||
|
||||
self._type = 'ChromiumFrame'
|
||||
|
||||
def __call__(self, locator, index=1, timeout=None):
|
||||
return self.ele(locator, index=index, timeout=timeout)
|
||||
|
||||
def __repr__(self):
|
||||
attrs = [f"{k}='{v}'" for k, v in self._frame_ele.attrs.items()]
|
||||
return f'<ChromiumFrame {self.frame_ele.tag} {" ".join(attrs)}>'
|
||||
|
||||
def __eq__(self, other):
|
||||
return self._frame_id == getattr(other, '_frame_id', None)
|
||||
|
||||
def _d_set_runtime_settings(self):
|
||||
if not hasattr(self, '_timeouts'):
|
||||
self._timeouts = copy(self._target_page.timeouts)
|
||||
self.retry_times = self._target_page.retry_times
|
||||
self.retry_interval = self._target_page.retry_interval
|
||||
self._download_path = self._target_page.download_path
|
||||
self._auto_handle_alert = self._target_page._auto_handle_alert
|
||||
self._load_mode = self._target_page._load_mode if not self._is_diff_domain else 'normal'
|
||||
|
||||
def _driver_init(self, target_id, is_init=True):
|
||||
try:
|
||||
super()._driver_init(target_id)
|
||||
except:
|
||||
if not self.browser._ws_only:
|
||||
self.browser._driver.get(f'http://{self._browser.address}/json')
|
||||
super()._driver_init(target_id)
|
||||
self._driver.set_callback('Inspector.detached', self._onInspectorDetached, immediate=True)
|
||||
self._driver.set_callback('Page.frameDetached', None)
|
||||
self._driver.set_callback('Page.frameDetached', self._onFrameDetached, immediate=True)
|
||||
|
||||
def _reload(self):
|
||||
self._is_loading = True
|
||||
self._reloading = True
|
||||
self._doc_got = False
|
||||
|
||||
self._driver.stop()
|
||||
try:
|
||||
self._frame_ele = ChromiumElement(self._target_page, backend_id=self._backend_id)
|
||||
end_time = perf_counter() + 2
|
||||
while perf_counter() < end_time:
|
||||
node = self._target_page._run_cdp('DOM.describeNode', backendNodeId=self._frame_ele._backend_id)['node']
|
||||
if 'frameId' in node:
|
||||
break
|
||||
sleep(.05)
|
||||
|
||||
else:
|
||||
return
|
||||
|
||||
except (ElementLostError, PageDisconnectedError):
|
||||
return
|
||||
|
||||
if self._is_inner_frame():
|
||||
self._is_diff_domain = False
|
||||
self.doc_ele = ChromiumElement(self._target_page, backend_id=node['contentDocument']['backendNodeId'])
|
||||
self._frame_id = node['frameId']
|
||||
if self._listener:
|
||||
self._listener._to_target(self._target_page.tab_id, self._browser._ws_address, self)
|
||||
super().__init__(self._browser, self._target_page.tab_id)
|
||||
|
||||
else:
|
||||
self._is_diff_domain = True
|
||||
if self._listener:
|
||||
self._listener._to_target(node['frameId'], self._browser.address, self)
|
||||
end_time = perf_counter() + self.timeouts.page_load
|
||||
super().__init__(self._browser, node['frameId'])
|
||||
timeout = end_time - perf_counter()
|
||||
if timeout <= 0:
|
||||
timeout = .5
|
||||
self._wait_loaded(timeout)
|
||||
|
||||
self._is_loading = False
|
||||
self._reloading = False
|
||||
|
||||
def _get_document(self, timeout=10):
|
||||
if self._is_reading:
|
||||
return
|
||||
|
||||
self._is_reading = True
|
||||
try:
|
||||
if self._is_diff_domain is False:
|
||||
node = self._target_page._run_cdp('DOM.describeNode', backendNodeId=self._backend_id)['node']
|
||||
self.doc_ele = ChromiumElement(self._target_page, backend_id=node['contentDocument']['backendNodeId'])
|
||||
|
||||
else:
|
||||
timeout = max(timeout, 2)
|
||||
b_id = self._run_cdp('DOM.getDocument', _timeout=timeout)['root']['backendNodeId']
|
||||
self.doc_ele = ChromiumElement(self, backend_id=b_id)
|
||||
|
||||
self._root_id = self.doc_ele._obj_id
|
||||
|
||||
r = self._run_cdp('Page.getFrameTree', _ignore=PageDisconnectedError)
|
||||
for i in findall(r"'id': '(.*?)'", str(r)):
|
||||
self.browser._frames[i] = self.tab_id
|
||||
return True
|
||||
|
||||
except:
|
||||
return False
|
||||
|
||||
finally:
|
||||
if not self._reloading: # 阻止reload时标识
|
||||
self._is_loading = False
|
||||
self._is_reading = False
|
||||
|
||||
def _onInspectorDetached(self, **kwargs):
|
||||
# 异域转同域或退出
|
||||
self._reload()
|
||||
|
||||
def _onFrameDetached(self, **kwargs):
|
||||
# 同域变异域
|
||||
self.browser._frames.pop(kwargs['frameId'], None)
|
||||
ChromiumFrame._Frames.pop(kwargs['frameId'], None)
|
||||
if kwargs['frameId'] == self._frame_id:
|
||||
self._reload()
|
||||
|
||||
# ----------挂件----------
|
||||
@property
|
||||
def scroll(self):
|
||||
self.wait.doc_loaded()
|
||||
if self._scroll is None:
|
||||
self._scroll = FrameScroller(self)
|
||||
return self._scroll
|
||||
|
||||
@property
|
||||
def set(self):
|
||||
if self._set is None:
|
||||
self._set = ChromiumFrameSetter(self)
|
||||
return self._set
|
||||
|
||||
@property
|
||||
def states(self):
|
||||
if self._states is None:
|
||||
self._states = FrameStates(self)
|
||||
return self._states
|
||||
|
||||
@property
|
||||
def wait(self):
|
||||
if self._wait is None:
|
||||
self._wait = FrameWaiter(self)
|
||||
return self._wait
|
||||
|
||||
@property
|
||||
def rect(self):
|
||||
if self._rect is None:
|
||||
self._rect = FrameRect(self)
|
||||
return self._rect
|
||||
|
||||
@property
|
||||
def listen(self):
|
||||
if self._listener is None:
|
||||
self._listener = FrameListener(self)
|
||||
return self._listener
|
||||
|
||||
# ----------挂件结束----------
|
||||
|
||||
@property
|
||||
def _obj_id(self):
|
||||
return self.frame_ele._obj_id
|
||||
|
||||
@property
|
||||
def _node_id(self):
|
||||
return self.frame_ele._node_id
|
||||
|
||||
@property
|
||||
def owner(self):
|
||||
return self.frame_ele.owner
|
||||
|
||||
@property
|
||||
def frame_ele(self):
|
||||
return self._frame_ele
|
||||
|
||||
@property
|
||||
def tag(self):
|
||||
return self.frame_ele.tag
|
||||
|
||||
@property
|
||||
def url(self):
|
||||
try:
|
||||
return self.doc_ele._run_js('return this.location.href;')
|
||||
except JavaScriptError:
|
||||
return None
|
||||
|
||||
@property
|
||||
def html(self):
|
||||
tag = self.tag
|
||||
out_html = self._target_page._run_cdp('DOM.getOuterHTML', backendNodeId=self.frame_ele._backend_id)['outerHTML']
|
||||
sign = search(rf'<{tag}.*?>', out_html, DOTALL).group(0)
|
||||
return f'{sign}{self.inner_html}</{tag}>'
|
||||
|
||||
@property
|
||||
def inner_html(self):
|
||||
return self.doc_ele._run_js('return this.documentElement.outerHTML;')
|
||||
|
||||
@property
|
||||
def link(self):
|
||||
return self.frame_ele.link
|
||||
|
||||
@property
|
||||
def title(self):
|
||||
r = self._ele('t:title', raise_err=False)
|
||||
return r.text if r else None
|
||||
|
||||
@property
|
||||
def attrs(self):
|
||||
return self.frame_ele.attrs
|
||||
|
||||
@property
|
||||
def active_ele(self):
|
||||
return self.doc_ele._run_js('return this.activeElement;')
|
||||
|
||||
@property
|
||||
def xpath(self):
|
||||
return self.frame_ele.xpath
|
||||
|
||||
@property
|
||||
def css_path(self):
|
||||
return self.frame_ele.css_path
|
||||
|
||||
@property
|
||||
def tab(self):
|
||||
return self._tab
|
||||
|
||||
@property
|
||||
def tab_id(self):
|
||||
return self.tab.tab_id
|
||||
|
||||
@property
|
||||
def download_path(self):
|
||||
return self._download_path
|
||||
|
||||
@property
|
||||
def sr(self):
|
||||
return self.frame_ele.sr
|
||||
|
||||
@property
|
||||
def shadow_root(self):
|
||||
return self.frame_ele.sr
|
||||
|
||||
@property
|
||||
def child_count(self):
|
||||
return int(self._ele('xpath:count(./*)'))
|
||||
|
||||
@property
|
||||
def _js_ready_state(self):
|
||||
if self._is_diff_domain:
|
||||
return super()._js_ready_state
|
||||
|
||||
else:
|
||||
try:
|
||||
return self.doc_ele._run_js('return this.readyState;')
|
||||
except ContextLostError:
|
||||
try:
|
||||
node = self._run_cdp('DOM.describeNode', backendNodeId=self.frame_ele._backend_id)['node']
|
||||
doc = ChromiumElement(self._target_page, backend_id=node['contentDocument']['backendNodeId'])
|
||||
return doc._run_js('return this.readyState;')
|
||||
except:
|
||||
return None
|
||||
|
||||
def refresh(self):
|
||||
self.doc_ele._run_js('this.location.reload();')
|
||||
|
||||
def property(self, name):
|
||||
return self.frame_ele.property(name)
|
||||
|
||||
def attr(self, name):
|
||||
return self.frame_ele.attr(name)
|
||||
|
||||
def remove_attr(self, name):
|
||||
self.frame_ele.remove_attr(name)
|
||||
|
||||
def style(self, style, pseudo_ele=''):
|
||||
return self.frame_ele.style(style=style, pseudo_ele=pseudo_ele)
|
||||
|
||||
def run_js(self, script, *args, as_expr=False, timeout=None):
|
||||
return self._run_js(script, *args, as_expr=as_expr, timeout=timeout)
|
||||
|
||||
def _run_js(self, script, *args, as_expr=False, timeout=None):
|
||||
if script.startswith('this.scrollIntoView'):
|
||||
return self.frame_ele._run_js(script, *args, as_expr=as_expr, timeout=timeout)
|
||||
else:
|
||||
return self.doc_ele._run_js(script, *args, as_expr=as_expr, timeout=timeout)
|
||||
|
||||
def parent(self, level_or_loc=1, index=1, timeout=0):
|
||||
return self.frame_ele.parent(level_or_loc, index, timeout=timeout)
|
||||
|
||||
def prev(self, locator='', index=1, timeout=0, ele_only=True):
|
||||
return self.frame_ele.prev(locator, index, timeout, ele_only=ele_only)
|
||||
|
||||
def next(self, locator='', index=1, timeout=0, ele_only=True):
|
||||
return self.frame_ele.next(locator, index, timeout, ele_only=ele_only)
|
||||
|
||||
def before(self, locator='', index=1, timeout=None, ele_only=True):
|
||||
return self.frame_ele.before(locator, index, timeout, ele_only=ele_only)
|
||||
|
||||
def after(self, locator='', index=1, timeout=None, ele_only=True):
|
||||
return self.frame_ele.after(locator, index, timeout, ele_only=ele_only)
|
||||
|
||||
def prevs(self, locator='', timeout=0, ele_only=True):
|
||||
return self.frame_ele.prevs(locator, timeout, ele_only=ele_only)
|
||||
|
||||
def nexts(self, locator='', timeout=0, ele_only=True):
|
||||
return self.frame_ele.nexts(locator, timeout, ele_only=ele_only)
|
||||
|
||||
def befores(self, locator='', timeout=None, ele_only=True):
|
||||
return self.frame_ele.befores(locator, timeout, ele_only=ele_only)
|
||||
|
||||
def afters(self, locator='', timeout=None, ele_only=True):
|
||||
return self.frame_ele.afters(locator, timeout, ele_only=ele_only)
|
||||
|
||||
def get_screenshot(self, path=None, name=None, as_bytes=None, as_base64=None):
|
||||
return self.frame_ele.get_screenshot(path=path, name=name, as_bytes=as_bytes, as_base64=as_base64)
|
||||
|
||||
def _get_screenshot(self, path=None, name=None, as_bytes: [bool, str] = None, as_base64: [bool, str] = None,
|
||||
full_page=False, left_top=None, right_bottom=None, ele=None):
|
||||
if not self._is_diff_domain:
|
||||
return super().get_screenshot(path=path, name=name, as_bytes=as_bytes, as_base64=as_base64,
|
||||
full_page=full_page, left_top=left_top, right_bottom=right_bottom)
|
||||
|
||||
if as_bytes:
|
||||
if as_bytes is True:
|
||||
pic_type = 'png'
|
||||
else:
|
||||
if as_bytes not in ('jpg', 'jpeg', 'png', 'webp'):
|
||||
raise ValueError(_S._lang.join(_S._lang.INCORRECT_VAL_, 'as_bytes',
|
||||
ALLOW_VAL='"jpg", "jpeg", "png", "webp"', CURR_VAL=as_bytes))
|
||||
pic_type = 'jpeg' if as_bytes == 'jpg' else as_bytes
|
||||
|
||||
elif as_base64:
|
||||
if as_base64 is True:
|
||||
pic_type = 'png'
|
||||
else:
|
||||
if as_base64 not in ('jpg', 'jpeg', 'png', 'webp'):
|
||||
raise ValueError(_S._lang.join(_S._lang.INCORRECT_VAL_, 'as_base64',
|
||||
ALLOW_VAL='"jpg", "jpeg", "png", "webp"', CURR_VAL=as_base64))
|
||||
pic_type = 'jpeg' if as_base64 == 'jpg' else as_base64
|
||||
|
||||
else:
|
||||
path = str(path).rstrip('\\/') if path else '.'
|
||||
if path and path.endswith(('.jpg', '.jpeg', '.png', '.webp')):
|
||||
pic_type = path.rsplit('.', 1)[-1]
|
||||
|
||||
elif name and name.endswith(('.jpg', '.jpeg', '.png', '.webp')):
|
||||
pic_type = name.rsplit('.', 1)[-1]
|
||||
|
||||
else:
|
||||
pic_type = 'jpeg'
|
||||
|
||||
if pic_type == 'jpg':
|
||||
pic_type = 'jpeg'
|
||||
|
||||
self.frame_ele.scroll.to_see(center=True)
|
||||
self.scroll.to_see(ele, center=True)
|
||||
cx, cy = ele.rect.viewport_location
|
||||
w, h = ele.rect.size
|
||||
img_data = f'data:image/{pic_type};base64,{self.frame_ele.get_screenshot(as_base64=True)}'
|
||||
body = self.tab('t:body')
|
||||
first_child = body('c::first-child')
|
||||
if not isinstance(first_child, ChromiumElement):
|
||||
first_child = first_child.frame_ele
|
||||
js = f'''
|
||||
img = document.createElement('img');
|
||||
img.src = "{img_data}";
|
||||
img.style.setProperty("z-index",9999999);
|
||||
img.style.setProperty("position","fixed");
|
||||
arguments[0].insertBefore(img, this);
|
||||
return img;'''
|
||||
new_ele = first_child._run_js(js, body)
|
||||
new_ele.scroll.to_see(center=True)
|
||||
top = int(self.frame_ele.style('border-top').split('px')[0])
|
||||
left = int(self.frame_ele.style('border-left').split('px')[0])
|
||||
|
||||
r = self.tab._run_cdp('Page.getLayoutMetrics')['visualViewport']
|
||||
sx = r['pageX']
|
||||
sy = r['pageY']
|
||||
r = self.tab.get_screenshot(path=path, name=name, as_bytes=as_bytes, as_base64=as_base64,
|
||||
left_top=(cx + left + sx, cy + top + sy),
|
||||
right_bottom=(cx + w + left + sx, cy + h + top + sy))
|
||||
self.tab.remove_ele(new_ele)
|
||||
return r
|
||||
|
||||
def _find_elements(self, locator, timeout, index=1, relative=False, raise_err=None):
|
||||
if isinstance(locator, ChromiumElement):
|
||||
return locator
|
||||
self.wait.doc_loaded()
|
||||
return self.doc_ele._ele(locator, index=index, timeout=timeout,
|
||||
raise_err=raise_err) if index is not None else self.doc_ele.eles(locator, timeout)
|
||||
|
||||
def _is_inner_frame(self):
|
||||
return self._frame_id in str(self._target_page._run_cdp('Page.getFrameTree')['frameTree'])
|
||||
@@ -0,0 +1,458 @@
|
||||
# -*- coding:utf-8 -*-
|
||||
"""
|
||||
@Author : g1879
|
||||
@Contact : g1879@qq.com
|
||||
@Website : https://DrissionPage.cn
|
||||
@Copyright: (c) 2020 by g1879, Inc. All Rights Reserved.
|
||||
"""
|
||||
from pathlib import Path
|
||||
from typing import Union, Tuple, List, Any, Optional, Literal
|
||||
|
||||
from .chromium_base import ChromiumBase
|
||||
from .chromium_tab import ChromiumTab
|
||||
from .mix_tab import MixTab
|
||||
from .._elements.chromium_element import ChromiumElement, ShadowRoot
|
||||
from .._functions.elements import ChromiumElementsList
|
||||
from .._units.listener import FrameListener
|
||||
from .._units.rect import FrameRect
|
||||
from .._units.scroller import FrameScroller
|
||||
from .._units.setter import ChromiumFrameSetter
|
||||
from .._units.states import FrameStates
|
||||
from .._units.waiter import FrameWaiter
|
||||
|
||||
|
||||
class ChromiumFrame(ChromiumBase):
|
||||
_Frames: dict = ...
|
||||
_target_page: Union[ChromiumTab, ChromiumFrame] = ...
|
||||
_tab: Union[MixTab, ChromiumTab] = ...
|
||||
_set: ChromiumFrameSetter = ...
|
||||
_frame_ele: ChromiumElement = ...
|
||||
_backend_id: int = ...
|
||||
_doc_ele: ChromiumElement = ...
|
||||
_is_diff_domain: bool = ...
|
||||
doc_ele: ChromiumElement = ...
|
||||
_states: FrameStates = ...
|
||||
_reloading: bool = ...
|
||||
_rect: Optional[FrameRect] = ...
|
||||
_listener: FrameListener = ...
|
||||
|
||||
def __init__(self,
|
||||
owner: Union[ChromiumTab, ChromiumFrame],
|
||||
ele: ChromiumElement,
|
||||
info: dict = None):
|
||||
"""
|
||||
:param owner: frame所在的页面对象
|
||||
:param ele: frame所在元素
|
||||
:param info: frame所在元素信息
|
||||
"""
|
||||
...
|
||||
|
||||
def __call__(self,
|
||||
locator: Union[Tuple[str, str], str],
|
||||
index: int = 1,
|
||||
timeout: float = None) -> ChromiumElement:
|
||||
"""在内部查找元素
|
||||
:param locator: 元素的定位信息,可以是loc元组,或查询字符串
|
||||
:param index: 获取第几个,从1开始,可传入负数获取倒数第几个
|
||||
:param timeout: 超时时间(秒)
|
||||
:return: ChromiumElement对象或属性、文本
|
||||
"""
|
||||
...
|
||||
|
||||
def __repr__(self) -> str: ...
|
||||
|
||||
def __eq__(self, other: ChromiumFrame) -> bool: ...
|
||||
|
||||
def _d_set_runtime_settings(self) -> None:
|
||||
"""重写设置浏览器运行参数方法"""
|
||||
...
|
||||
|
||||
def _driver_init(self, target_id: str, is_init: bool = True) -> None:
|
||||
"""避免出现服务器500错误
|
||||
:param target_id: 要跳转到的target id
|
||||
:return: None
|
||||
"""
|
||||
...
|
||||
|
||||
def _reload(self) -> None:
|
||||
"""重新获取document"""
|
||||
...
|
||||
|
||||
def _get_document(self, timeout: float = 10) -> bool:
|
||||
"""刷新cdp使用的document数据
|
||||
:param timeout: 超时时间(秒)
|
||||
:return: 是否获取成功
|
||||
"""
|
||||
...
|
||||
|
||||
def _onFrameStoppedLoading(self, **kwargs): ...
|
||||
|
||||
def _onInspectorDetached(self, **kwargs): ...
|
||||
|
||||
@property
|
||||
def scroll(self) -> FrameScroller:
|
||||
"""返回用于滚动的对象"""
|
||||
...
|
||||
|
||||
@property
|
||||
def set(self) -> ChromiumFrameSetter:
|
||||
"""返回用于设置的对象"""
|
||||
...
|
||||
|
||||
@property
|
||||
def states(self) -> FrameStates:
|
||||
"""返回用于获取状态信息的对象"""
|
||||
...
|
||||
|
||||
@property
|
||||
def wait(self) -> FrameWaiter:
|
||||
"""返回用于等待的对象"""
|
||||
...
|
||||
|
||||
@property
|
||||
def rect(self) -> FrameRect:
|
||||
"""返回获取坐标和大小的对象"""
|
||||
...
|
||||
|
||||
@property
|
||||
def listen(self) -> FrameListener:
|
||||
"""返回用于聆听数据包的对象"""
|
||||
...
|
||||
|
||||
@property
|
||||
def _obj_id(self) -> str: ...
|
||||
|
||||
@property
|
||||
def _node_id(self) -> int: ...
|
||||
|
||||
@property
|
||||
def owner(self) -> ChromiumBase:
|
||||
"""返回所属页面对象"""
|
||||
...
|
||||
|
||||
@property
|
||||
def frame_ele(self) -> ChromiumElement:
|
||||
"""返回总页面上的frame元素"""
|
||||
...
|
||||
|
||||
@property
|
||||
def tag(self) -> str:
|
||||
"""返回元素tag"""
|
||||
...
|
||||
|
||||
@property
|
||||
def url(self) -> str:
|
||||
"""返回frame当前访问的url"""
|
||||
...
|
||||
|
||||
@property
|
||||
def html(self) -> str:
|
||||
"""返回元素outerHTML文本"""
|
||||
...
|
||||
|
||||
@property
|
||||
def inner_html(self) -> str:
|
||||
"""返回元素innerHTML文本"""
|
||||
...
|
||||
|
||||
@property
|
||||
def link(self) -> str:
|
||||
"""返回href或src绝对url"""
|
||||
...
|
||||
|
||||
@property
|
||||
def title(self) -> str:
|
||||
"""返回页面title"""
|
||||
...
|
||||
|
||||
@property
|
||||
def attrs(self) -> dict:
|
||||
"""返回frame元素所有attribute属性"""
|
||||
...
|
||||
|
||||
@property
|
||||
def active_ele(self) -> ChromiumElement:
|
||||
"""返回当前焦点所在元素"""
|
||||
...
|
||||
|
||||
@property
|
||||
def xpath(self) -> str:
|
||||
"""返回frame的xpath绝对路径"""
|
||||
...
|
||||
|
||||
@property
|
||||
def css_path(self) -> str:
|
||||
"""返回frame的css selector绝对路径"""
|
||||
...
|
||||
|
||||
@property
|
||||
def tab(self) -> Union[ChromiumTab, MixTab]:
|
||||
"""返回frame所在的tab对象"""
|
||||
...
|
||||
|
||||
@property
|
||||
def tab_id(self) -> str:
|
||||
"""返回frame所在tab的id"""
|
||||
...
|
||||
|
||||
@property
|
||||
def download_path(self) -> str:
|
||||
"""返回下载文件保存路径"""
|
||||
...
|
||||
|
||||
@property
|
||||
def sr(self) -> Union[None, ShadowRoot]:
|
||||
"""返回iframe的shadow-root元素对象"""
|
||||
...
|
||||
|
||||
@property
|
||||
def shadow_root(self) -> Union[None, ShadowRoot]:
|
||||
"""返回iframe的shadow-root元素对象"""
|
||||
...
|
||||
|
||||
@property
|
||||
def child_count(self) -> int:
|
||||
"""返回直接子元素的个数"""
|
||||
...
|
||||
|
||||
@property
|
||||
def _js_ready_state(self) -> Literal['loading', 'interactive', 'complete']:
|
||||
"""返回当前页面加载状态"""
|
||||
...
|
||||
|
||||
def refresh(self) -> None:
|
||||
"""刷新frame页面"""
|
||||
...
|
||||
|
||||
def property(self, name: str) -> Union[str, None]:
|
||||
"""返回frame元素一个property属性值
|
||||
:param name: 属性名
|
||||
:return: 属性值文本,没有该属性返回None
|
||||
"""
|
||||
...
|
||||
|
||||
def attr(self, name: str) -> Union[str, None]:
|
||||
"""返回frame元素一个attribute属性值
|
||||
:param name: 属性名
|
||||
:return: 属性值文本,没有该属性返回None
|
||||
"""
|
||||
...
|
||||
|
||||
def remove_attr(self, name: str) -> None:
|
||||
"""删除frame元素attribute属性
|
||||
:param name: 属性名
|
||||
:return: None
|
||||
"""
|
||||
...
|
||||
|
||||
def style(self, style: str, pseudo_ele: str = '') -> str:
|
||||
"""返回frame元素样式属性值,可获取伪元素属性值
|
||||
:param style: 样式属性名称
|
||||
:param pseudo_ele: 伪元素名称(如有)
|
||||
:return: 样式属性的值
|
||||
"""
|
||||
...
|
||||
|
||||
def run_js(self,
|
||||
script: str,
|
||||
*args,
|
||||
as_expr: bool = False,
|
||||
timeout: float = None) -> Any:
|
||||
"""运行javascript代码
|
||||
:param script: js文本
|
||||
:param args: 参数,按顺序在js文本中对应arguments[0]、arguments[1]...
|
||||
:param as_expr: 是否作为表达式运行,为True时args无效
|
||||
:param timeout: js超时时间(秒),为None则使用页面timeouts.script设置
|
||||
:return: 运行的结果
|
||||
"""
|
||||
...
|
||||
|
||||
def _run_js(self,
|
||||
script: str,
|
||||
*args,
|
||||
as_expr: bool = False,
|
||||
timeout: float = None) -> Any:
|
||||
"""运行javascript代码
|
||||
:param script: js文本
|
||||
:param args: 参数,按顺序在js文本中对应arguments[0]、arguments[1]...
|
||||
:param as_expr: 是否作为表达式运行,为True时args无效
|
||||
:param timeout: js超时时间(秒),为None则使用页面timeouts.script设置
|
||||
:return: 运行的结果
|
||||
"""
|
||||
...
|
||||
|
||||
def parent(self,
|
||||
level_or_loc: Union[Tuple[str, str], str, int] = 1,
|
||||
index: int = 1,
|
||||
timeout: float = 0) -> ChromiumElement:
|
||||
"""返回上面某一级父元素,可指定层数或用查询语法定位
|
||||
:param level_or_loc: 第几级父元素,1开始,或定位符
|
||||
:param index: 当level_or_loc传入定位符,使用此参数选择第几个结果,1开始
|
||||
:param timeout: 查找超时时间(秒)
|
||||
:return: 上级元素对象
|
||||
"""
|
||||
...
|
||||
|
||||
def prev(self,
|
||||
locator: Union[Tuple[str, str], str, int] = '',
|
||||
index: int = 1,
|
||||
timeout: float = 0,
|
||||
ele_only: bool = True) -> Union[ChromiumElement, str]:
|
||||
"""返回当前元素前面一个符合条件的同级元素,可用查询语法筛选,可指定返回筛选结果的第几个
|
||||
:param locator: 用于筛选的查询语法
|
||||
:param index: 前面第几个查询结果,1开始
|
||||
:param timeout: 查找节点的超时时间(秒)
|
||||
:param ele_only: 是否只获取元素,为False时把文本、注释节点也纳入
|
||||
:return: 同级元素或节点
|
||||
"""
|
||||
...
|
||||
|
||||
def next(self,
|
||||
locator: Union[Tuple[str, str], str, int] = '',
|
||||
index: int = 1,
|
||||
timeout: float = 0,
|
||||
ele_only: bool = True) -> Union[ChromiumElement, str]:
|
||||
"""返回当前元素后面一个符合条件的同级元素,可用查询语法筛选,可指定返回筛选结果的第几个
|
||||
:param locator: 用于筛选的查询语法
|
||||
:param index: 后面第几个查询结果,1开始
|
||||
:param timeout: 查找节点的超时时间(秒)
|
||||
:param ele_only: 是否只获取元素,为False时把文本、注释节点也纳入
|
||||
:return: 同级元素或节点
|
||||
"""
|
||||
...
|
||||
|
||||
def before(self,
|
||||
locator: Union[Tuple[str, str], str, int] = '',
|
||||
index: int = 1,
|
||||
timeout: float = None,
|
||||
ele_only: bool = True) -> Union[ChromiumElement, str]:
|
||||
"""返回文档中当前元素前面符合条件的一个元素,可用查询语法筛选,可指定返回筛选结果的第几个
|
||||
查找范围不限同级元素,而是整个DOM文档
|
||||
:param locator: 用于筛选的查询语法
|
||||
:param index: 前面第几个查询结果,1开始
|
||||
:param timeout: 查找节点的超时时间(秒)
|
||||
:param ele_only: 是否只获取元素,为False时把文本、注释节点也纳入
|
||||
:return: 本元素前面的某个元素或节点
|
||||
"""
|
||||
...
|
||||
|
||||
def after(self,
|
||||
locator: Union[Tuple[str, str], str, int] = '',
|
||||
index: int = 1,
|
||||
timeout: float = None,
|
||||
ele_only: bool = True) -> Union[ChromiumElement, str]:
|
||||
"""返回文档中此当前元素后面符合条件的一个元素,可用查询语法筛选,可指定返回筛选结果的第几个
|
||||
查找范围不限同级元素,而是整个DOM文档
|
||||
:param locator: 用于筛选的查询语法
|
||||
:param index: 后面第几个查询结果,1开始
|
||||
:param timeout: 查找节点的超时时间(秒)
|
||||
:param ele_only: 是否只获取元素,为False时把文本、注释节点也纳入
|
||||
:return: 本元素后面的某个元素或节点
|
||||
"""
|
||||
...
|
||||
|
||||
def prevs(self,
|
||||
locator: Union[Tuple[str, str], str] = '',
|
||||
timeout: float = 0,
|
||||
ele_only: bool = True) -> List[Union[ChromiumElement, str]]:
|
||||
"""返回当前元素前面符合条件的同级元素或节点组成的列表,可用查询语法筛选
|
||||
:param locator: 用于筛选的查询语法
|
||||
:param timeout: 查找节点的超时时间(秒)
|
||||
:param ele_only: 是否只获取元素,为False时把文本、注释节点也纳入
|
||||
:return: 同级元素或节点文本组成的列表
|
||||
"""
|
||||
...
|
||||
|
||||
def nexts(self,
|
||||
locator: Union[Tuple[str, str], str] = '',
|
||||
timeout: float = 0,
|
||||
ele_only: bool = True) -> List[Union[ChromiumElement, str]]:
|
||||
"""返回当前元素后面符合条件的同级元素或节点组成的列表,可用查询语法筛选
|
||||
:param locator: 用于筛选的查询语法
|
||||
:param timeout: 查找节点的超时时间(秒)
|
||||
:param ele_only: 是否只获取元素,为False时把文本、注释节点也纳入
|
||||
:return: 同级元素或节点文本组成的列表
|
||||
"""
|
||||
...
|
||||
|
||||
def befores(self,
|
||||
locator: Union[Tuple[str, str], str] = '',
|
||||
timeout: float = None,
|
||||
ele_only: bool = True) -> List[Union[ChromiumElement, str]]:
|
||||
"""返回文档中当前元素前面符合条件的元素或节点组成的列表,可用查询语法筛选
|
||||
查找范围不限同级元素,而是整个DOM文档
|
||||
:param locator: 用于筛选的查询语法
|
||||
:param timeout: 查找节点的超时时间(秒)
|
||||
:param ele_only: 是否只获取元素,为False时把文本、注释节点也纳入
|
||||
:return: 本元素前面的元素或节点组成的列表
|
||||
"""
|
||||
...
|
||||
|
||||
def afters(self,
|
||||
locator: Union[Tuple[str, str], str] = '',
|
||||
timeout: float = None,
|
||||
ele_only: bool = True) -> List[Union[ChromiumElement, str]]:
|
||||
"""返回文档中当前元素后面符合条件的元素或节点组成的列表,可用查询语法筛选
|
||||
查找范围不限同级元素,而是整个DOM文档
|
||||
:param locator: 用于筛选的查询语法
|
||||
:param timeout: 查找节点的超时时间(秒)
|
||||
:param ele_only: 是否只获取元素,为False时把文本、注释节点也纳入
|
||||
:return: 本元素前面的元素或节点组成的列表
|
||||
"""
|
||||
...
|
||||
|
||||
def get_screenshot(self,
|
||||
path: [str, Path] = None,
|
||||
name: str = None,
|
||||
as_bytes: [bool, str] = None,
|
||||
as_base64: [bool, str] = None) -> Union[str, bytes]:
|
||||
"""对页面进行截图,可对整个网页、可见网页、指定范围截图。对可视范围外截图需要90以上版本浏览器支持
|
||||
:param path: 文件保存路径
|
||||
:param name: 完整文件名,后缀可选 'jpg','jpeg','png','webp'
|
||||
:param as_bytes: 是否以字节形式返回图片,可选 'jpg','jpeg','png','webp',生效时path参数和as_base64参数无效
|
||||
:param as_base64: 是否以base64字符串形式返回图片,可选 'jpg','jpeg','png','webp',生效时path参数无效
|
||||
:return: 图片完整路径或字节文本
|
||||
"""
|
||||
...
|
||||
|
||||
def _get_screenshot(self,
|
||||
path: [str, Path] = None,
|
||||
name: str = None,
|
||||
as_bytes: [bool, str] = None,
|
||||
as_base64: [bool, str] = None,
|
||||
full_page: bool = False,
|
||||
left_top: Tuple[int, int] = None,
|
||||
right_bottom: Tuple[int, int] = None,
|
||||
ele: ChromiumElement = None) -> Union[str, bytes]:
|
||||
"""实现截图
|
||||
:param path: 文件保存路径
|
||||
:param name: 完整文件名,后缀可选 'jpg','jpeg','png','webp'
|
||||
:param as_bytes: 是否以字节形式返回图片,可选 'jpg','jpeg','png','webp',生效时path参数和as_base64参数无效
|
||||
:param as_base64: 是否以base64字符串形式返回图片,可选 'jpg','jpeg','png','webp',生效时path参数无效
|
||||
:param full_page: 是否整页截图,为True截取整个网页,为False截取可视窗口
|
||||
:param left_top: 截取范围左上角坐标
|
||||
:param right_bottom: 截取范围右下角角坐标
|
||||
:param ele: 为异域iframe内元素截图设置
|
||||
:return: 图片完整路径或字节文本
|
||||
"""
|
||||
...
|
||||
|
||||
def _find_elements(self,
|
||||
locator: Union[Tuple[str, str], str, ChromiumElement, ChromiumFrame],
|
||||
timeout: float,
|
||||
index: Optional[int] = 1,
|
||||
relative: bool = False,
|
||||
raise_err: bool = None) -> Union[ChromiumElement, ChromiumFrame, None, ChromiumElementsList]:
|
||||
"""在frame内查找单个元素
|
||||
:param locator: 定位符或元素对象
|
||||
:param timeout: 查找超时时间(秒)
|
||||
:param index: 第几个结果,从1开始,可传入负数获取倒数第几个,为None返回所有
|
||||
:param relative: MixTab用的表示是否相对定位的参数
|
||||
:param raise_err: 找不到元素是是否抛出异常,为None时根据全局设置
|
||||
:return: ChromiumElement对象
|
||||
"""
|
||||
...
|
||||
|
||||
def _is_inner_frame(self) -> bool:
|
||||
"""返回当前frame是否同域"""
|
||||
...
|
||||
@@ -0,0 +1,131 @@
|
||||
# -*- coding:utf-8 -*-
|
||||
"""
|
||||
@Author : g1879
|
||||
@Contact : g1879@qq.com
|
||||
@Website : https://DrissionPage.cn
|
||||
@Copyright: (c) 2020 by g1879, Inc. All Rights Reserved.
|
||||
"""
|
||||
from time import sleep
|
||||
|
||||
from .._base.chromium import Chromium
|
||||
from .._functions.settings import Settings
|
||||
from .._functions.web import save_page
|
||||
from .._pages.chromium_base import ChromiumBase
|
||||
from .._units.setter import ChromiumPageSetter
|
||||
from .._units.waiter import ChromiumPageWaiter
|
||||
|
||||
|
||||
class ChromiumPage(ChromiumBase):
|
||||
_PAGES = {}
|
||||
|
||||
def __new__(cls, addr_or_opts=None, tab_id=None, timeout=None):
|
||||
# 即将废弃timeout
|
||||
browser = Chromium(addr_or_opts=addr_or_opts)
|
||||
if browser.id in cls._PAGES:
|
||||
r = cls._PAGES[browser.id]
|
||||
while not hasattr(r, '_frame_id'):
|
||||
sleep(.05)
|
||||
return r
|
||||
|
||||
r = object.__new__(cls)
|
||||
r._browser = browser
|
||||
cls._PAGES[browser.id] = r
|
||||
return r
|
||||
|
||||
def __init__(self, addr_or_opts=None, tab_id=None, timeout=None):
|
||||
# 即将废弃timeout
|
||||
if hasattr(self, '_created'):
|
||||
return
|
||||
self._created = True
|
||||
|
||||
self.tab = self
|
||||
super().__init__(self.browser, tab_id)
|
||||
self._type = 'ChromiumPage'
|
||||
self.set.timeouts(base=timeout) # 即将废弃
|
||||
self._tab = self
|
||||
self._browser._dl_mgr._page_id = self.tab_id
|
||||
|
||||
def __repr__(self):
|
||||
return f'<ChromiumPage browser_id={self.browser.id} tab_id={self.tab_id}>'
|
||||
|
||||
def _d_set_runtime_settings(self):
|
||||
"""设置运行时用到的属性"""
|
||||
self._timeouts = self.browser.timeouts
|
||||
self._load_mode = self.browser._load_mode
|
||||
self._download_path = self.browser.download_path
|
||||
self.retry_times = self.browser.retry_times
|
||||
self.retry_interval = self.browser.retry_interval
|
||||
self._auto_handle_alert = self.browser._auto_handle_alert
|
||||
|
||||
@property
|
||||
def set(self):
|
||||
if self._set is None:
|
||||
self._set = ChromiumPageSetter(self)
|
||||
return self._set
|
||||
|
||||
@property
|
||||
def wait(self):
|
||||
if self._wait is None:
|
||||
self._wait = ChromiumPageWaiter(self)
|
||||
return self._wait
|
||||
|
||||
@property
|
||||
def browser(self):
|
||||
return self._browser
|
||||
|
||||
@property
|
||||
def tabs_count(self):
|
||||
return self.browser.tabs_count
|
||||
|
||||
@property
|
||||
def tab_ids(self):
|
||||
return self.browser.tab_ids
|
||||
|
||||
@property
|
||||
def latest_tab(self):
|
||||
return self.browser._get_tab(id_or_num=self.tab_ids[0], as_id=not Settings.singleton_tab_obj)
|
||||
|
||||
@property
|
||||
def process_id(self):
|
||||
return self.browser.process_id
|
||||
|
||||
@property
|
||||
def browser_version(self):
|
||||
return self._browser.version
|
||||
|
||||
@property
|
||||
def address(self):
|
||||
return self.browser.address
|
||||
|
||||
@property
|
||||
def download_path(self):
|
||||
return self.browser.download_path
|
||||
|
||||
def save(self, path=None, name=None, as_pdf=False, **kwargs):
|
||||
return save_page(self, path, name, as_pdf, kwargs)
|
||||
|
||||
def get_tab(self, id_or_num=None, title=None, url=None, tab_type='page', as_id=False):
|
||||
return self.browser._get_tab(id_or_num=id_or_num, title=title, url=url,
|
||||
tab_type=tab_type, mix=False, as_id=as_id)
|
||||
|
||||
def get_tabs(self, title=None, url=None, tab_type='page', as_id=False):
|
||||
return self.browser._get_tabs(title=title, url=url, tab_type=tab_type, mix=False, as_id=as_id)
|
||||
|
||||
def new_tab(self, url=None, new_window=False, background=False, new_context=False):
|
||||
return self.browser._new_tab(False, url=url, new_window=new_window,
|
||||
background=background, new_context=new_context)
|
||||
|
||||
def activate_tab(self, id_ind_tab):
|
||||
self.browser.activate_tab(id_ind_tab)
|
||||
|
||||
def close(self):
|
||||
self.browser._close_tab(self)
|
||||
|
||||
def close_tabs(self, tabs_or_ids, others=False):
|
||||
self.browser.close_tabs(tabs_or_ids=tabs_or_ids, others=others)
|
||||
|
||||
def quit(self, timeout=5, force=True, del_data=False):
|
||||
self.browser.quit(timeout, force, del_data=del_data)
|
||||
|
||||
def _on_disconnect(self):
|
||||
ChromiumPage._PAGES.pop(self._browser.id, None)
|
||||
@@ -0,0 +1,224 @@
|
||||
# -*- coding:utf-8 -*-
|
||||
"""
|
||||
@Author : g1879
|
||||
@Contact : g1879@qq.com
|
||||
@Website : https://DrissionPage.cn
|
||||
@Copyright: (c) 2020 by g1879, Inc. All Rights Reserved.
|
||||
"""
|
||||
from pathlib import Path
|
||||
from typing import Union, Tuple, List, Optional
|
||||
|
||||
from .._base.chromium import Chromium
|
||||
from .._configs.chromium_options import ChromiumOptions
|
||||
from .._pages.chromium_base import ChromiumBase
|
||||
from .._pages.chromium_tab import ChromiumTab
|
||||
from .._units.rect import TabRect
|
||||
from .._units.setter import ChromiumPageSetter
|
||||
from .._units.waiter import ChromiumPageWaiter
|
||||
|
||||
|
||||
class ChromiumPage(ChromiumBase):
|
||||
"""用于管理浏览器和一个标签页的类"""
|
||||
_PAGES: dict = ...
|
||||
tab: ChromiumPage = ...
|
||||
_browser: Chromium = ...
|
||||
_rect: Optional[TabRect] = ...
|
||||
_is_exist: bool = ...
|
||||
|
||||
def __new__(cls,
|
||||
addr_or_opts: Union[str, int, ChromiumOptions] = None,
|
||||
tab_id: str = None):
|
||||
"""
|
||||
:param addr_or_opts: 浏览器地址:端口、ws地址、ChromiumOptions对象或端口数字(int)
|
||||
:param tab_id: 要控制的标签页id,不指定默认为激活的
|
||||
"""
|
||||
...
|
||||
|
||||
def __init__(self,
|
||||
addr_or_opts: Union[str, int, ChromiumOptions] = None,
|
||||
tab_id: str = None):
|
||||
"""
|
||||
:param addr_or_opts: 浏览器地址:端口、ws地址、ChromiumOptions对象或端口数字(int)
|
||||
:param tab_id: 要控制的标签页id,不指定默认为激活的
|
||||
"""
|
||||
...
|
||||
|
||||
@property
|
||||
def set(self) -> ChromiumPageSetter:
|
||||
"""返回用于设置的对象"""
|
||||
...
|
||||
|
||||
@property
|
||||
def wait(self) -> ChromiumPageWaiter:
|
||||
"""返回用于等待的对象"""
|
||||
...
|
||||
|
||||
@property
|
||||
def browser(self) -> Chromium:
|
||||
"""返回浏览器对象"""
|
||||
...
|
||||
|
||||
@property
|
||||
def tabs_count(self) -> int:
|
||||
"""返回标签页数量"""
|
||||
...
|
||||
|
||||
@property
|
||||
def tab_ids(self) -> List[str]:
|
||||
"""返回所有标签页id组成的列表"""
|
||||
...
|
||||
|
||||
@property
|
||||
def latest_tab(self) -> Union[ChromiumTab, ChromiumPage, str]:
|
||||
"""返回最新的标签页,最新标签页指最后创建或最后被激活的
|
||||
当Settings.singleton_tab_obj==True时返回Tab对象,否则返回tab id"""
|
||||
...
|
||||
|
||||
@property
|
||||
def process_id(self) -> Optional[int]:
|
||||
"""返回浏览器进程id"""
|
||||
...
|
||||
|
||||
@property
|
||||
def browser_version(self) -> str:
|
||||
"""返回所控制的浏览器版本号"""
|
||||
...
|
||||
|
||||
@property
|
||||
def address(self) -> str:
|
||||
"""返回浏览器地址ip:port"""
|
||||
...
|
||||
|
||||
def save(self,
|
||||
path: Union[str, Path] = None,
|
||||
name: str = None,
|
||||
as_pdf: bool = False,
|
||||
landscape: bool = ...,
|
||||
displayHeaderFooter: bool = ...,
|
||||
printBackground: bool = ...,
|
||||
scale: float = ...,
|
||||
paperWidth: float = ...,
|
||||
paperHeight: float = ...,
|
||||
marginTop: float = ...,
|
||||
marginBottom: float = ...,
|
||||
marginLeft: float = ...,
|
||||
marginRight: float = ...,
|
||||
pageRanges: str = ...,
|
||||
headerTemplate: str = ...,
|
||||
footerTemplate: str = ...,
|
||||
preferCSSPageSize: bool = ...,
|
||||
generateTaggedPDF: bool = ...,
|
||||
generateDocumentOutline: bool = ...) -> Union[bytes, str]:
|
||||
"""把当前页面保存为文件,如果path和name参数都为None,只返回文本
|
||||
:param path: 保存路径,为None且name不为None时保存在当前路径
|
||||
:param name: 文件名,为None且path不为None时用title属性值
|
||||
:param as_pdf: 为Ture保存为pdf,否则为mhtml且忽略kwargs参数
|
||||
:param landscape: 纸张方向,as_pdf为True时才生效
|
||||
:param displayHeaderFooter: 是否显示页头页脚,as_pdf为True时才生效
|
||||
:param printBackground: 是否打印背景图片,as_pdf为True时才生效
|
||||
:param scale: 缩放比例,as_pdf为True时才生效
|
||||
:param paperWidth: 页面宽度(英寸),as_pdf为True时才生效
|
||||
:param paperHeight: 页面高度(英寸),as_pdf为True时才生效
|
||||
:param marginTop: 上边距(英寸),as_pdf为True时才生效
|
||||
:param marginBottom: 下边距(英寸),as_pdf为True时才生效
|
||||
:param marginLeft: 左边距(英寸),as_pdf为True时才生效
|
||||
:param marginRight: 右边距(英寸),as_pdf为True时才生效
|
||||
:param pageRanges: 页面范围,格式'1-5, 8, 11-13',as_pdf为True时才生效
|
||||
:param headerTemplate: 页头HTML模板,as_pdf为True时才生效
|
||||
模板可包含以下class:
|
||||
- date:日期
|
||||
- title:文档标题
|
||||
- url:文档url
|
||||
- pageNumber:当前页码
|
||||
- totalPages:总页数
|
||||
示例:<span class=title></span>
|
||||
:param footerTemplate: 页脚HTML模板,格式与页头的一样,as_pdf为True时才生效
|
||||
:param preferCSSPageSize: 是否使用css定义的页面大小,as_pdf为True时才生效
|
||||
:param generateTaggedPDF: 是否生成带标签的(可访问的)PDF。默认为嵌入器选择,as_pdf为True时才生效
|
||||
:param generateDocumentOutline: 是否将文档大纲嵌入到PDF中。,as_pdf为True时才生效
|
||||
:return: as_pdf为True时返回bytes,否则返回文件文本
|
||||
"""
|
||||
...
|
||||
|
||||
def get_tab(self,
|
||||
id_or_num: Union[str, ChromiumTab, int] = None,
|
||||
title: str = None,
|
||||
url: str = None,
|
||||
tab_type: Union[str, list, tuple] = 'page',
|
||||
as_id: bool = False) -> Union[ChromiumTab, str, None]:
|
||||
"""获取一个标签页对象,id_or_num不为None时,后面几个参数无效
|
||||
:param id_or_num: 要获取的标签页id或序号,序号从1开始,可传入负数获取倒数第几个,不是视觉排列顺序,而是激活顺序
|
||||
:param title: 要匹配title的文本,模糊匹配,为None则匹配所有
|
||||
:param url: 要匹配url的文本,模糊匹配,为None则匹配所有
|
||||
:param tab_type: tab类型,可用列表输入多个,如 'page', 'iframe' 等,为None则匹配所有
|
||||
:param as_id: 是否返回标签页id而不是标签页对象
|
||||
:return: ChromiumTab对象
|
||||
"""
|
||||
...
|
||||
|
||||
def get_tabs(self,
|
||||
title: str = None,
|
||||
url: str = None,
|
||||
tab_type: Union[str, list, tuple] = 'page',
|
||||
as_id: bool = False) -> Union[List[ChromiumTab], List[str]]:
|
||||
"""查找符合条件的tab,返回它们组成的列表
|
||||
:param title: 要匹配title的文本,模糊匹配,为None则匹配所有
|
||||
:param url: 要匹配url的文本,模糊匹配,为None则匹配所有
|
||||
:param tab_type: tab类型,可用列表输入多个,如 'page', 'iframe' 等,为None则匹配所有
|
||||
:param as_id: 是否返回标签页id而不是标签页对象
|
||||
:return: ChromiumTab对象组成的列表
|
||||
"""
|
||||
...
|
||||
|
||||
def new_tab(self,
|
||||
url: str = None,
|
||||
new_window: bool = False,
|
||||
background: bool = False,
|
||||
new_context: bool = False) -> ChromiumTab:
|
||||
"""新建一个标签页
|
||||
:param url: 新标签页跳转到的网址
|
||||
:param new_window: 是否在新窗口打开标签页
|
||||
:param background: 是否不激活新标签页,如new_window为True则无效
|
||||
:param new_context: 是否创建新的上下文
|
||||
:return: 新标签页对象
|
||||
"""
|
||||
...
|
||||
|
||||
def activate_tab(self,
|
||||
id_ind_tab: Union[int, str, ChromiumTab]) -> None:
|
||||
"""使标签页变为活动状态
|
||||
:param id_ind_tab: 标签页id(str)、Tab对象或标签页序号(int),序号从1开始
|
||||
:return: None
|
||||
"""
|
||||
...
|
||||
|
||||
def close(self) -> None:
|
||||
"""关闭Page管理的标签页"""
|
||||
...
|
||||
|
||||
def close_tabs(self,
|
||||
tabs_or_ids: Union[str, ChromiumTab, List[Union[str, ChromiumTab]],
|
||||
Tuple[Union[str, ChromiumTab]]],
|
||||
others: bool = False) -> None:
|
||||
"""关闭传入的标签页,可传入多个
|
||||
:param tabs_or_ids: 要关闭的标签页对象或id,可传入列表或元组
|
||||
:param others: 是否关闭指定标签页之外的
|
||||
:return: None
|
||||
"""
|
||||
...
|
||||
|
||||
def quit(self,
|
||||
timeout: float = 5,
|
||||
force: bool = True,
|
||||
del_data: bool = False) -> None:
|
||||
"""关闭浏览器
|
||||
:param timeout: 等待浏览器关闭超时时间(秒)
|
||||
:param force: 关闭超时是否强制终止进程
|
||||
:param del_data: 是否删除用户文件夹
|
||||
:return: None
|
||||
"""
|
||||
...
|
||||
|
||||
def _on_disconnect(self) -> None:
|
||||
"""浏览器退出时执行"""
|
||||
...
|
||||
@@ -0,0 +1,76 @@
|
||||
# -*- coding:utf-8 -*-
|
||||
"""
|
||||
@Author : g1879
|
||||
@Contact : g1879@qq.com
|
||||
@Website : https://DrissionPage.cn
|
||||
@Copyright: (c) 2020 by g1879, Inc. All Rights Reserved.
|
||||
"""
|
||||
from copy import copy
|
||||
from time import sleep
|
||||
|
||||
from .._functions.settings import Settings
|
||||
from .._functions.web import save_page
|
||||
from .._pages.chromium_base import ChromiumBase
|
||||
from .._units.setter import TabSetter
|
||||
from .._units.waiter import TabWaiter
|
||||
|
||||
|
||||
class ChromiumTab(ChromiumBase):
|
||||
_TABS = {}
|
||||
|
||||
def __new__(cls, browser, tab_id):
|
||||
if Settings.singleton_tab_obj and tab_id in cls._TABS:
|
||||
r = cls._TABS[tab_id]
|
||||
while not hasattr(r, '_frame_id'):
|
||||
sleep(.05)
|
||||
return r
|
||||
r = object.__new__(cls)
|
||||
cls._TABS[tab_id] = r
|
||||
return r
|
||||
|
||||
def __init__(self, browser, tab_id):
|
||||
if Settings.singleton_tab_obj and hasattr(self, '_created'):
|
||||
return
|
||||
self._created = True
|
||||
|
||||
super().__init__(browser, tab_id)
|
||||
self._tab = self
|
||||
self._type = 'ChromiumTab'
|
||||
|
||||
def __repr__(self):
|
||||
return f'<ChromiumTab browser_id={self.browser.id} tab_id={self.tab_id}>'
|
||||
|
||||
def _d_set_runtime_settings(self):
|
||||
self._timeouts = copy(self.browser.timeouts)
|
||||
self.retry_times = self.browser.retry_times
|
||||
self.retry_interval = self.browser.retry_interval
|
||||
self._load_mode = self.browser._load_mode
|
||||
self._download_path = self.browser.download_path
|
||||
self._auto_handle_alert = self.browser._auto_handle_alert
|
||||
self._none_ele_return_value = self.browser._none_ele_return_value
|
||||
self._none_ele_value = self.browser._none_ele_value
|
||||
|
||||
def close(self, others=False):
|
||||
if others:
|
||||
self.browser.close_tabs(self.tab_id, others=True)
|
||||
else:
|
||||
self.browser._close_tab(self)
|
||||
|
||||
@property
|
||||
def set(self):
|
||||
if self._set is None:
|
||||
self._set = TabSetter(self)
|
||||
return self._set
|
||||
|
||||
@property
|
||||
def wait(self):
|
||||
if self._wait is None:
|
||||
self._wait = TabWaiter(self)
|
||||
return self._wait
|
||||
|
||||
def save(self, path=None, name=None, as_pdf=False, **kwargs):
|
||||
return save_page(self, path, name, as_pdf, kwargs)
|
||||
|
||||
def _on_disconnect(self):
|
||||
if not self._disconnect_flag:
|
||||
ChromiumTab._TABS.pop(self.tab_id, None)
|
||||
@@ -0,0 +1,110 @@
|
||||
# -*- coding:utf-8 -*-
|
||||
"""
|
||||
@Author : g1879
|
||||
@Contact : g1879@qq.com
|
||||
@Website : https://DrissionPage.cn
|
||||
@Copyright: (c) 2020 by g1879, Inc. All Rights Reserved.
|
||||
"""
|
||||
from pathlib import Path
|
||||
from typing import Union, Optional
|
||||
|
||||
from .chromium_base import ChromiumBase
|
||||
from .._base.chromium import Chromium
|
||||
from .._units.rect import TabRect
|
||||
from .._units.setter import TabSetter
|
||||
from .._units.waiter import TabWaiter
|
||||
|
||||
|
||||
class ChromiumTab(ChromiumBase):
|
||||
"""实现浏览器标签页的类"""
|
||||
_TABS: dict = ...
|
||||
_tab: ChromiumTab = ...
|
||||
_rect: Optional[TabRect] = ...
|
||||
|
||||
def __new__(cls, browser: Chromium, tab_id: str):
|
||||
"""
|
||||
:param browser: Browser对象
|
||||
:param tab_id: 标签页id
|
||||
"""
|
||||
...
|
||||
|
||||
def __init__(self, browser: Chromium, tab_id: str):
|
||||
"""
|
||||
:param browser: Browser对象
|
||||
:param tab_id: 标签页id
|
||||
"""
|
||||
...
|
||||
|
||||
def _d_set_runtime_settings(self) -> None:
|
||||
"""重写设置浏览器运行参数方法"""
|
||||
...
|
||||
|
||||
def close(self, others: bool = False) -> None:
|
||||
"""关闭标签页
|
||||
:param others: 是否关闭其它,保留自己
|
||||
:return: None
|
||||
"""
|
||||
...
|
||||
|
||||
@property
|
||||
def set(self) -> TabSetter:
|
||||
"""返回用于设置的对象"""
|
||||
...
|
||||
|
||||
@property
|
||||
def wait(self) -> TabWaiter:
|
||||
"""返回用于等待的对象"""
|
||||
...
|
||||
|
||||
def save(self,
|
||||
path: Union[str, Path] = None,
|
||||
name: str = None,
|
||||
as_pdf: bool = False,
|
||||
landscape: bool = False,
|
||||
displayHeaderFooter: bool = False,
|
||||
printBackground: bool = False,
|
||||
scale: float = 1,
|
||||
paperWidth: float = 8.5,
|
||||
paperHeight: float = 11,
|
||||
marginTop: float = 11,
|
||||
marginBottom: float = 1,
|
||||
marginLeft: float = 1,
|
||||
marginRight: float = 1,
|
||||
pageRanges: str = '',
|
||||
headerTemplate: str = '',
|
||||
footerTemplate: str = '',
|
||||
preferCSSPageSize: bool = False,
|
||||
generateTaggedPDF: bool = ...,
|
||||
generateDocumentOutline: bool = ...) -> Union[bytes, str]:
|
||||
"""把当前页面保存为文件,如果path和name参数都为None,只返回文本
|
||||
:param path: 保存路径,为None且name不为None时保存在当前路径
|
||||
:param name: 文件名,为None且path不为None时用title属性值
|
||||
:param as_pdf: 为Ture保存为pdf,否则为mhtml且忽略kwargs参数
|
||||
:param landscape: 纸张方向,as_pdf为True时才生效
|
||||
:param displayHeaderFooter: 是否显示页头页脚,as_pdf为True时才生效
|
||||
:param printBackground: 是否打印背景图片,as_pdf为True时才生效
|
||||
:param scale: 缩放比例,as_pdf为True时才生效
|
||||
:param paperWidth: 页面宽度(英寸),as_pdf为True时才生效
|
||||
:param paperHeight: 页面高度(英寸),as_pdf为True时才生效
|
||||
:param marginTop: 上边距(英寸),as_pdf为True时才生效
|
||||
:param marginBottom: 下边距(英寸),as_pdf为True时才生效
|
||||
:param marginLeft: 左边距(英寸),as_pdf为True时才生效
|
||||
:param marginRight: 右边距(英寸),as_pdf为True时才生效
|
||||
:param pageRanges: 页面范围,格式'1-5, 8, 11-13',as_pdf为True时才生效
|
||||
:param headerTemplate: 页头HTML模板,as_pdf为True时才生效
|
||||
模板可包含以下class:
|
||||
- date:日期
|
||||
- title:文档标题
|
||||
- url:文档url
|
||||
- pageNumber:当前页码
|
||||
- totalPages:总页数
|
||||
示例:<span class=title></span>
|
||||
:param footerTemplate: 页脚HTML模板,格式与页头的一样,as_pdf为True时才生效
|
||||
:param preferCSSPageSize: 是否使用css定义的页面大小,as_pdf为True时才生效
|
||||
:param generateTaggedPDF: 是否生成带标签的(可访问的)PDF。默认为嵌入器选择,as_pdf为True时才生效
|
||||
:param generateDocumentOutline: 是否将文档大纲嵌入到PDF中。,as_pdf为True时才生效
|
||||
:return: as_pdf为True时返回bytes,否则返回文件文本
|
||||
"""
|
||||
...
|
||||
|
||||
def _on_disconnect(self): ...
|
||||
@@ -0,0 +1,199 @@
|
||||
# -*- coding:utf-8 -*-
|
||||
"""
|
||||
@Author : g1879
|
||||
@Contact : g1879@qq.com
|
||||
@Website : https://DrissionPage.cn
|
||||
@Copyright: (c) 2020 by g1879, Inc. All Rights Reserved.
|
||||
"""
|
||||
from .chromium_tab import ChromiumTab
|
||||
from .._base.base import BasePage
|
||||
from .._configs.session_options import SessionOptions
|
||||
from .._functions.cookies import set_session_cookies, set_tab_cookies
|
||||
from .._functions.settings import Settings as _S
|
||||
from .._pages.session_page import SessionPage
|
||||
from .._units.setter import MixTabSetter
|
||||
|
||||
|
||||
class MixTab(SessionPage, ChromiumTab, BasePage):
|
||||
def __init__(self, browser, tab_id):
|
||||
if _S.singleton_tab_obj and hasattr(self, '_created'):
|
||||
return
|
||||
self._d_mode = True
|
||||
self._session_options = None
|
||||
self._headers = None
|
||||
self._response = None
|
||||
self._session = None
|
||||
self._encoding = None
|
||||
self._timeout = 10
|
||||
super(SessionPage, self).__init__(browser=browser, tab_id=tab_id)
|
||||
self._type = 'MixTab'
|
||||
|
||||
def __call__(self, locator, index=1, timeout=None):
|
||||
return super(SessionPage, self).__call__(locator, index=index, timeout=timeout) if self._d_mode \
|
||||
else super().__call__(locator, index=index)
|
||||
|
||||
def __repr__(self):
|
||||
return f'<MixTab browser_id={self.browser.id} tab_id={self.tab_id}>'
|
||||
|
||||
@property
|
||||
def set(self):
|
||||
if self._set is None:
|
||||
self._set = MixTabSetter(self)
|
||||
return self._set
|
||||
|
||||
@property
|
||||
def url(self):
|
||||
return self._browser_url if self._d_mode else self._session_url
|
||||
|
||||
@property
|
||||
def _browser_url(self):
|
||||
return super(SessionPage, self).url if self._driver else None
|
||||
|
||||
@property
|
||||
def title(self):
|
||||
return super(SessionPage, self).title if self._d_mode else super().title
|
||||
|
||||
@property
|
||||
def raw_data(self):
|
||||
return super(SessionPage, self).html if self._d_mode else super().raw_data
|
||||
|
||||
@property
|
||||
def html(self):
|
||||
return super(SessionPage, self).html if self._d_mode else super().html
|
||||
|
||||
@property
|
||||
def json(self):
|
||||
return super(SessionPage, self).json if self._d_mode else super().json
|
||||
|
||||
@property
|
||||
def response(self):
|
||||
return self._response
|
||||
|
||||
@property
|
||||
def mode(self):
|
||||
return 'd' if self._d_mode else 's'
|
||||
|
||||
@property
|
||||
def user_agent(self):
|
||||
return super(SessionPage, self).user_agent if self._d_mode else super().user_agent
|
||||
|
||||
@property
|
||||
def _session_url(self):
|
||||
return self._response.url if self._response else None
|
||||
|
||||
@property
|
||||
def timeout(self):
|
||||
return self.timeouts.base if self._d_mode else self._timeout
|
||||
|
||||
def get(self, url, show_errmsg=False, retry=None, interval=None, timeout=None, **kwargs):
|
||||
if self._d_mode:
|
||||
if kwargs:
|
||||
raise ValueError(_S._lang.join(_S._lang.S_MODE_ONLY, ARGS=", ".join(kwargs.keys())))
|
||||
return super(SessionPage, self).get(url, show_errmsg, retry, interval, timeout)
|
||||
|
||||
if timeout is None:
|
||||
timeout = self.timeouts.page_load
|
||||
return super().get(url, show_errmsg, retry, interval, timeout, **kwargs)
|
||||
|
||||
def post(self, url, show_errmsg=False, retry=None, interval=None, timeout=None, **kwargs):
|
||||
if self.mode == 'd':
|
||||
self.cookies_to_session()
|
||||
if timeout is None:
|
||||
kwargs['timeout'] = self.timeouts.page_load
|
||||
if self._session is None:
|
||||
self._create_session()
|
||||
super().post(url, show_errmsg, retry, interval, **kwargs)
|
||||
return self.response
|
||||
|
||||
def ele(self, locator, index=1, timeout=None):
|
||||
return (super(SessionPage, self).ele(locator, index=index, timeout=timeout)
|
||||
if self._d_mode else super().ele(locator, index=index))
|
||||
|
||||
def eles(self, locator, timeout=None):
|
||||
return super(SessionPage, self).eles(locator, timeout=timeout) if self._d_mode else super().eles(locator)
|
||||
|
||||
def s_ele(self, locator=None, index=1, timeout=None):
|
||||
return (super(SessionPage, self).s_ele(locator, index=index, timeout=timeout)
|
||||
if self._d_mode else super().s_ele(locator, index=index, timeout=timeout))
|
||||
|
||||
def s_eles(self, locator, timeout=None):
|
||||
return (super(SessionPage, self).s_eles(locator, timeout=timeout)
|
||||
if self._d_mode else super().s_eles(locator, timeout=timeout))
|
||||
|
||||
def change_mode(self, mode=None, go=True, copy_cookies=True):
|
||||
if mode:
|
||||
mode = mode.lower()
|
||||
if mode is not None and ((mode == 'd' and self._d_mode) or (mode == 's' and not self._d_mode)):
|
||||
return
|
||||
|
||||
self._d_mode = not self._d_mode
|
||||
|
||||
# s模式转d模式
|
||||
if self._d_mode:
|
||||
if self._driver is None or not self._driver.is_running:
|
||||
self._driver_init(self.tab_id)
|
||||
self._get_document()
|
||||
|
||||
self._url = super(SessionPage, self).url
|
||||
if self._session_url:
|
||||
if copy_cookies:
|
||||
self.cookies_to_browser()
|
||||
if go:
|
||||
self.get(self._session_url)
|
||||
|
||||
return
|
||||
|
||||
# d模式转s模式
|
||||
if self._session is None:
|
||||
self._set_session_options(
|
||||
self.browser._session_options or SessionOptions(read_file=self.browser._session_options is None))
|
||||
self._create_session()
|
||||
|
||||
self._url = self._session_url
|
||||
if self._driver:
|
||||
if copy_cookies:
|
||||
self.cookies_to_session()
|
||||
|
||||
if go:
|
||||
url = super(SessionPage, self).url
|
||||
if url.startswith('http'):
|
||||
self.get(url)
|
||||
|
||||
def cookies_to_session(self, copy_user_agent=True):
|
||||
if not self._session:
|
||||
return
|
||||
|
||||
if copy_user_agent:
|
||||
user_agent = self._run_cdp('Runtime.evaluate', expression='navigator.userAgent;')['result']['value']
|
||||
self._headers.update({"User-Agent": user_agent})
|
||||
|
||||
set_session_cookies(self.session, super(SessionPage, self).cookies())
|
||||
|
||||
def cookies_to_browser(self):
|
||||
if self._driver is None or not self._driver.is_running:
|
||||
return
|
||||
set_tab_cookies(self, super().cookies())
|
||||
|
||||
def cookies(self, all_domains=False, all_info=False):
|
||||
return super(SessionPage, self).cookies(all_domains, all_info) if self._d_mode \
|
||||
else super().cookies(all_domains, all_info)
|
||||
|
||||
def close(self, others=False, session=False):
|
||||
if others:
|
||||
self.browser.close_tabs(self.tab_id, others=True)
|
||||
else:
|
||||
self.browser._close_tab(self)
|
||||
if session and self._session:
|
||||
self._session.close()
|
||||
if self._response is not None:
|
||||
self._response.close()
|
||||
|
||||
def _find_elements(self, locator, timeout, index=1, relative=False, raise_err=None):
|
||||
return super(SessionPage, self)._find_elements(locator, timeout=timeout, index=index, relative=relative) \
|
||||
if self._d_mode else super()._find_elements(locator, index=index, timeout=timeout)
|
||||
|
||||
def _set_session_options(self, session_or_options=None):
|
||||
if session_or_options is None:
|
||||
session_or_options = self.browser._session_options or SessionOptions(
|
||||
read_file=self.browser._session_options is None)
|
||||
super()._set_session_options(session_or_options)
|
||||
@@ -0,0 +1,298 @@
|
||||
# -*- coding:utf-8 -*-
|
||||
"""
|
||||
@Author : g1879
|
||||
@Contact : g1879@qq.com
|
||||
@Website : https://DrissionPage.cn
|
||||
@Copyright: (c) 2020 by g1879, Inc. All Rights Reserved.
|
||||
"""
|
||||
from http.cookiejar import CookieJar
|
||||
from typing import Union, Tuple, Any, Optional, Literal
|
||||
|
||||
from requests import Session, Response
|
||||
|
||||
from .chromium_frame import ChromiumFrame
|
||||
from .chromium_tab import ChromiumTab
|
||||
from .session_page import SessionPage
|
||||
from .._base.chromium import Chromium
|
||||
from .._elements.chromium_element import ChromiumElement
|
||||
from .._elements.session_element import SessionElement
|
||||
from .._functions.cookies import CookiesList
|
||||
from .._functions.elements import SessionElementsList, ChromiumElementsList
|
||||
from .._units.setter import MixTabSetter
|
||||
from .._units.waiter import MixTabWaiter
|
||||
|
||||
|
||||
class MixTab(SessionPage, ChromiumTab):
|
||||
_tab: MixTab = ...
|
||||
_d_mode: bool = ...
|
||||
_set: MixTabSetter = ...
|
||||
|
||||
def __init__(self, browser: Chromium, tab_id: str):
|
||||
"""
|
||||
:param browser: Chromium对象
|
||||
:param tab_id: 标签页id
|
||||
"""
|
||||
...
|
||||
|
||||
def __call__(self,
|
||||
locator: Union[Tuple[str, str], str, ChromiumElement, SessionElement],
|
||||
index: int = 1,
|
||||
timeout: float = None) -> Union[ChromiumElement, SessionElement]:
|
||||
"""在内部查找元素
|
||||
例:ele = page('@id=ele_id')
|
||||
:param locator: 元素的定位信息,可以是loc元组,或查询字符串
|
||||
:param index: 获取第几个,从1开始,可传入负数获取倒数第几个
|
||||
:param timeout: 超时时间(秒)
|
||||
:return: 子元素对象
|
||||
"""
|
||||
...
|
||||
|
||||
@property
|
||||
def set(self) -> MixTabSetter:
|
||||
"""返回用于设置的对象"""
|
||||
...
|
||||
|
||||
@property
|
||||
def wait(self) -> MixTabWaiter:
|
||||
"""返回用于等待的对象"""
|
||||
...
|
||||
|
||||
@property
|
||||
def url(self) -> Union[str, None]:
|
||||
"""返回浏览器当前url"""
|
||||
...
|
||||
|
||||
@property
|
||||
def _browser_url(self) -> Union[str, None]:
|
||||
"""返回浏览器当前url"""
|
||||
...
|
||||
|
||||
@property
|
||||
def title(self) -> str:
|
||||
"""返回当前页面title"""
|
||||
...
|
||||
|
||||
@property
|
||||
def raw_data(self) -> Union[str, bytes]:
|
||||
"""返回页码原始数据数据"""
|
||||
...
|
||||
|
||||
@property
|
||||
def html(self) -> str:
|
||||
"""返回页面html文本"""
|
||||
...
|
||||
|
||||
@property
|
||||
def json(self) -> dict:
|
||||
"""当返回内容是json格式时,返回对应的字典"""
|
||||
...
|
||||
|
||||
@property
|
||||
def response(self) -> Response:
|
||||
"""返回 s 模式获取到的 Response 对象,切换到 s 模式"""
|
||||
...
|
||||
|
||||
@property
|
||||
def mode(self) -> Literal['s', 'd']:
|
||||
"""返回当前模式,'s'或'd' """
|
||||
...
|
||||
|
||||
@property
|
||||
def user_agent(self) -> str:
|
||||
"""返回user agent"""
|
||||
...
|
||||
|
||||
@property
|
||||
def session(self) -> Session:
|
||||
"""返回Session对象,如未初始化则按配置信息创建"""
|
||||
...
|
||||
|
||||
@property
|
||||
def _session_url(self) -> str:
|
||||
"""返回 session 保存的url"""
|
||||
...
|
||||
|
||||
@property
|
||||
def timeout(self) -> float:
|
||||
"""返回通用timeout设置"""
|
||||
...
|
||||
|
||||
def get(self,
|
||||
url: str,
|
||||
show_errmsg: bool = False,
|
||||
retry: Optional[int] = None,
|
||||
interval: Optional[float] = None,
|
||||
timeout: Optional[float] = None,
|
||||
params: Optional[dict] = None,
|
||||
data: Any = None,
|
||||
json: Any = None,
|
||||
headers: Optional[dict] = None,
|
||||
cookies: Union[CookieJar, dict] = None,
|
||||
files: Optional[Any] = None,
|
||||
auth: Optional[Any] = None,
|
||||
allow_redirects: bool = True,
|
||||
proxies: Optional[dict] = None,
|
||||
hooks: Optional[Any] = None,
|
||||
stream: bool = None,
|
||||
verify: Union[bool, str] = None,
|
||||
cert: [str, Tuple[str, str]] = None) -> Union[bool, None]:
|
||||
"""跳转到一个url
|
||||
:param url: 目标url
|
||||
:param show_errmsg: 是否显示和抛出异常
|
||||
:param retry: 重试次数,为None时使用页面对象retry_times属性值
|
||||
:param interval: 重试间隔(秒),为None时使用页面对象retry_interval属性值
|
||||
:param timeout: 连接超时时间
|
||||
:param params: url中的参数
|
||||
:param data: 携带的数据
|
||||
:param json: 要发送的 JSON 数据,会自动设置 Content-Type 为 application/json
|
||||
:param headers: 请求头
|
||||
:param cookies: cookies信息
|
||||
:param files: 要上传的文件,可以是一个字典,其中键是文件名,值是文件对象或文件路径
|
||||
:param auth: 身份认证信息
|
||||
:param allow_redirects: 是否允许重定向
|
||||
:param proxies: 代理信息
|
||||
:param hooks: 回调方法
|
||||
:param stream: 是否使用流式传输
|
||||
:param verify: 是否验证 SSL 证书
|
||||
:param cert: SSL客户端证书文件的路径(.pem格式),或('cert', 'key')元组
|
||||
:return: s模式时返回url是否可用,d模式时返回获取到的Response对象
|
||||
"""
|
||||
...
|
||||
|
||||
def post(self,
|
||||
url: str,
|
||||
show_errmsg: bool = False,
|
||||
retry: Optional[int] = None,
|
||||
interval: Optional[float] = None,
|
||||
timeout: Optional[float] = None,
|
||||
params: Optional[dict] = None,
|
||||
data: Any = None,
|
||||
json: Any = None,
|
||||
headers: Optional[dict] = None,
|
||||
cookies: Union[CookieJar, dict] = None,
|
||||
files: Optional[Any] = None,
|
||||
auth: Optional[Any] = None,
|
||||
allow_redirects: bool = True,
|
||||
proxies: Optional[dict] = None,
|
||||
hooks: Optional[Any] = None,
|
||||
stream: bool = None,
|
||||
verify: Union[bool, str] = None,
|
||||
cert: [str, Tuple[str, str]] = None) -> Response:
|
||||
"""用post方式跳转到url
|
||||
:param url: 目标url
|
||||
:param show_errmsg: 是否显示和抛出异常
|
||||
:param retry: 重试次数,为None时使用页面对象retry_times属性值
|
||||
:param interval: 重试间隔(秒),为None时使用页面对象retry_interval属性值
|
||||
:param timeout: 连接超时时间
|
||||
:param params: url中的参数
|
||||
:param data: 携带的数据
|
||||
:param json: 要发送的 JSON 数据,会自动设置 Content-Type 为 application/json
|
||||
:param headers: 请求头
|
||||
:param cookies: cookies信息
|
||||
:param files: 要上传的文件,可以是一个字典,其中键是文件名,值是文件对象或文件路径
|
||||
:param auth: 身份认证信息
|
||||
:param allow_redirects: 是否允许重定向
|
||||
:param proxies: 代理信息
|
||||
:param hooks: 回调方法
|
||||
:param stream: 是否使用流式传输
|
||||
:param verify: 是否验证 SSL 证书
|
||||
:param cert: SSL客户端证书文件的路径(.pem格式),或('cert', 'key')元组
|
||||
:return: 获取到的Response对象
|
||||
"""
|
||||
...
|
||||
|
||||
def ele(self,
|
||||
locator: Union[Tuple[str, str], str, ChromiumElement, SessionElement],
|
||||
index: int = 1,
|
||||
timeout: float = None) -> Union[ChromiumElement, SessionElement]:
|
||||
"""返回第一个符合条件的元素、属性或节点文本
|
||||
:param locator: 元素的定位信息,可以是元素对象,loc元组,或查询字符串
|
||||
:param index: 获取第几个,从1开始,可传入负数获取倒数第几个
|
||||
:param timeout: 查找元素超时时间(秒),默认与页面等待时间一致
|
||||
:return: 元素对象或属性、文本节点文本
|
||||
"""
|
||||
...
|
||||
|
||||
def eles(self,
|
||||
locator: Union[Tuple[str, str], str],
|
||||
timeout: float = None) -> Union[ChromiumElementsList, SessionElementsList]:
|
||||
"""返回页面中所有符合条件的元素、属性或节点文本
|
||||
:param locator: 元素的定位信息,可以是loc元组,或查询字符串
|
||||
:param timeout: 查找元素超时时间(秒),默认与页面等待时间一致
|
||||
:return: 元素对象或属性、文本组成的列表
|
||||
"""
|
||||
...
|
||||
|
||||
def s_ele(self,
|
||||
locator: Union[Tuple[str, str], str] = None,
|
||||
index: int = 1,
|
||||
timeout: float = None) -> SessionElement:
|
||||
"""查找第一个符合条件的元素以SessionElement形式返回,d模式处理复杂页面时效率很高
|
||||
:param locator: 元素的定位信息,可以是loc元组,或查询字符串
|
||||
:param index: 获取第几个,从1开始,可传入负数获取倒数第几个
|
||||
:param timeout: 查找元素超时时间(秒),默认与页面等待时间一致
|
||||
:return: SessionElement对象或属性、文本
|
||||
"""
|
||||
...
|
||||
|
||||
def s_eles(self, locator: Union[Tuple[str, str], str], timeout: float = None) -> SessionElementsList:
|
||||
"""查找所有符合条件的元素以SessionElement形式返回,d模式处理复杂页面时效率很高
|
||||
:param locator: 元素的定位信息,可以是loc元组,或查询字符串
|
||||
:param timeout: 查找元素超时时间(秒),默认与页面等待时间一致
|
||||
:return: SessionElement对象或属性、文本组成的列表
|
||||
"""
|
||||
...
|
||||
|
||||
def change_mode(self, mode: str = None, go: bool = True, copy_cookies: bool = True) -> None:
|
||||
"""切换模式,接收's'或'd',除此以外的字符串会切换为 d 模式
|
||||
如copy_cookies为True,切换时会把当前模式的cookies复制到目标模式
|
||||
切换后,如果go是True,调用相应的get函数使访问的页面同步
|
||||
:param mode: 模式字符串
|
||||
:param go: 是否跳转到原模式的url
|
||||
:param copy_cookies: 是否复制cookies到目标模式
|
||||
:return: None
|
||||
"""
|
||||
...
|
||||
|
||||
def cookies_to_session(self, copy_user_agent: bool = True) -> None:
|
||||
"""把浏览器的cookies复制到session对象
|
||||
:param copy_user_agent: 是否复制ua信息
|
||||
:return: None
|
||||
"""
|
||||
...
|
||||
|
||||
def cookies_to_browser(self) -> None:
|
||||
"""把session对象的cookies复制到浏览器"""
|
||||
...
|
||||
|
||||
def cookies(self, all_domains: bool = False, all_info: bool = False) -> CookiesList:
|
||||
"""返回cookies
|
||||
:param all_domains: 是否返回所有域的cookies
|
||||
:param all_info: 是否返回所有信息,False则只返回name、value、domain
|
||||
:return: cookies信息
|
||||
"""
|
||||
...
|
||||
|
||||
def close(self, others: bool = False) -> None:
|
||||
"""关闭标签页
|
||||
:param others: 是否关闭其它,保留自己
|
||||
:return: None
|
||||
"""
|
||||
...
|
||||
|
||||
def _find_elements(self,
|
||||
locator: Union[Tuple[str, str], str, ChromiumElement, SessionElement, ChromiumFrame],
|
||||
timeout: float,
|
||||
index: Optional[int] = 1,
|
||||
relative: bool = False,
|
||||
raise_err: bool = None) \
|
||||
-> Union[ChromiumElement, SessionElement, ChromiumFrame, SessionElementsList, ChromiumElementsList]:
|
||||
"""返回页面中符合条件的元素、属性或节点文本,默认返回第一个
|
||||
:param locator: 元素的定位信息,可以是元素对象,loc元组,或查询字符串
|
||||
:param timeout: 查找元素超时时间(秒),d模式专用
|
||||
:param index: 第几个结果,从1开始,可传入负数获取倒数第几个,为None返回所有
|
||||
:param relative: MixTab用的表示是否相对定位的参数
|
||||
:param raise_err: 找不到元素是是否抛出异常,为None时根据全局设置
|
||||
:return: 元素对象或属性、文本节点文本
|
||||
"""
|
||||
...
|
||||
@@ -0,0 +1,296 @@
|
||||
# -*- coding:utf-8 -*-
|
||||
"""
|
||||
@Author : g1879
|
||||
@Contact : g1879@qq.com
|
||||
@Website : https://DrissionPage.cn
|
||||
@Copyright: (c) 2020 by g1879, Inc. All Rights Reserved.
|
||||
"""
|
||||
from pathlib import Path
|
||||
from re import search, DOTALL
|
||||
from time import sleep
|
||||
from urllib.parse import urlparse
|
||||
|
||||
from requests import Response
|
||||
from requests.structures import CaseInsensitiveDict
|
||||
from tldextract import TLDExtract
|
||||
|
||||
from .._base.base import BasePage
|
||||
from .._elements.session_element import SessionElement, make_session_ele
|
||||
from .._functions.cookies import cookie_to_dict, CookiesList
|
||||
from .._functions.settings import Settings as _S
|
||||
from .._functions.web import format_headers
|
||||
from .._units.setter import SessionPageSetter
|
||||
|
||||
|
||||
class SessionPage(BasePage):
|
||||
def __init__(self, session_or_options=None, timeout=None):
|
||||
super().__init__()
|
||||
self._response = None
|
||||
self._set = None
|
||||
self._encoding = None
|
||||
self._type = 'SessionPage'
|
||||
self._page = self
|
||||
self._set_session_options(session_or_options)
|
||||
self._s_set_runtime_settings()
|
||||
if timeout is not None: # 即将废弃
|
||||
self._timeout = timeout
|
||||
if not self._session:
|
||||
self._create_session()
|
||||
|
||||
def __repr__(self):
|
||||
return f'<SessionPage url={self.url}>'
|
||||
|
||||
def _s_set_runtime_settings(self):
|
||||
self._timeout = self._session_options.timeout
|
||||
self._download_path = str(Path(self._session_options.download_path or '.').absolute())
|
||||
self.retry_times = self._session_options.retry_times
|
||||
self.retry_interval = self._session_options.retry_interval
|
||||
|
||||
def __call__(self, locator, index=1, timeout=None):
|
||||
return self.ele(locator, index=index)
|
||||
|
||||
# -----------------共有属性和方法-------------------
|
||||
@property
|
||||
def title(self):
|
||||
ele = self._ele('xpath://title', raise_err=False)
|
||||
return ele.text if ele else None
|
||||
|
||||
@property
|
||||
def url(self):
|
||||
return self._url
|
||||
|
||||
@property
|
||||
def _session_url(self):
|
||||
return self._url
|
||||
|
||||
@property
|
||||
def raw_data(self):
|
||||
return self.response.content if self.response else b''
|
||||
|
||||
@property
|
||||
def html(self):
|
||||
return self.response.text if self.response else ''
|
||||
|
||||
@property
|
||||
def json(self):
|
||||
try:
|
||||
return self.response.json()
|
||||
except Exception:
|
||||
return None
|
||||
|
||||
@property
|
||||
def user_agent(self):
|
||||
return self._headers.get('user-agent', '')
|
||||
|
||||
@property
|
||||
def session(self):
|
||||
return self._session
|
||||
|
||||
@property
|
||||
def response(self):
|
||||
return self._response
|
||||
|
||||
@property
|
||||
def encoding(self):
|
||||
return self._encoding
|
||||
|
||||
@property
|
||||
def set(self):
|
||||
if self._set is None:
|
||||
self._set = SessionPageSetter(self)
|
||||
return self._set
|
||||
|
||||
@property
|
||||
def timeout(self):
|
||||
return self._timeout
|
||||
|
||||
def get(self, url, show_errmsg=False, retry=None, interval=None, timeout=None, **kwargs):
|
||||
"""用get方式跳转到url,可输入文件路径
|
||||
:param url: 目标url,可指定本地文件路径
|
||||
:param show_errmsg: 是否显示和抛出异常
|
||||
:param retry: 重试次数,为None时使用页面对象retry_times属性值
|
||||
:param interval: 重试间隔(秒),为None时使用页面对象retry_interval属性值
|
||||
:param timeout: 连接超时时间(秒),为None时使用页面对象timeout属性值
|
||||
:param kwargs: 连接参数
|
||||
:return: url是否可用
|
||||
"""
|
||||
retry, interval, is_file = self._before_connect(url.replace('file:///', '', 1), retry, interval)
|
||||
if is_file:
|
||||
with open(self._url, 'rb') as f:
|
||||
r = Response()
|
||||
r._content = f.read()
|
||||
r.status_code = 200
|
||||
r.url = self._url
|
||||
self._response = r
|
||||
return True
|
||||
return self._s_connect(self._url, 'get', show_errmsg, retry, interval, **kwargs)
|
||||
|
||||
def post(self, url, show_errmsg=False, retry=None, interval=None, **kwargs):
|
||||
"""用post方式跳转到url
|
||||
:param url: 目标url
|
||||
:param show_errmsg: 是否显示和抛出异常
|
||||
:param retry: 重试次数,为None时使用页面对象retry_times属性值
|
||||
:param interval: 重试间隔(秒),为None时使用页面对象timeout属性值
|
||||
:param kwargs: 连接参数
|
||||
:return: url是否可用
|
||||
"""
|
||||
return self._s_connect(url, 'post', show_errmsg, retry, interval, **kwargs)
|
||||
|
||||
def ele(self, locator, index=1, timeout=None):
|
||||
return self._ele(locator, index=index, method='ele()')
|
||||
|
||||
def eles(self, locator, timeout=None):
|
||||
return self._ele(locator, index=None)
|
||||
|
||||
def s_ele(self, locator=None, index=1):
|
||||
return make_session_ele(self) if locator is None else self._ele(locator, index=index, method='s_ele()')
|
||||
|
||||
def s_eles(self, locator):
|
||||
return self._ele(locator, index=None)
|
||||
|
||||
def _find_elements(self, locator, timeout, index=1, relative=True, raise_err=None):
|
||||
return locator if isinstance(locator, SessionElement) else make_session_ele(self, locator, index=index)
|
||||
|
||||
def cookies(self, all_domains=False, all_info=False):
|
||||
if all_domains:
|
||||
cookies = self.session.cookies
|
||||
else:
|
||||
if self.url:
|
||||
ex_url = TLDExtract(
|
||||
suffix_list_urls=["https://publicsuffix.org/list/public_suffix_list.dat",
|
||||
f"file:///{_S.suffixes_list}"]).extract_str(self._session_url)
|
||||
domain = f'{ex_url.domain}.{ex_url.suffix}' if ex_url.suffix else ex_url.domain
|
||||
cookies = tuple(c for c in self.session.cookies if domain in c.domain or c.domain == '')
|
||||
else:
|
||||
cookies = tuple(c for c in self.session.cookies)
|
||||
|
||||
if all_info:
|
||||
r = CookiesList()
|
||||
for c in cookies:
|
||||
r.append(cookie_to_dict(c))
|
||||
else:
|
||||
r = CookiesList()
|
||||
for c in cookies:
|
||||
c = cookie_to_dict(c)
|
||||
r.append({'name': c['name'], 'value': c['value'], 'domain': c['domain']})
|
||||
return r
|
||||
|
||||
def close(self):
|
||||
self._session.close()
|
||||
if self._response is not None:
|
||||
self._response.close()
|
||||
|
||||
def _s_connect(self, url, mode, show_errmsg=False, retry=None, interval=None, **kwargs):
|
||||
retry, interval, is_file = self._before_connect(url, retry, interval)
|
||||
self._response = self._make_response(self._url, mode, retry, interval, show_errmsg, **kwargs)
|
||||
|
||||
if self._response is None:
|
||||
self._url_available = False
|
||||
|
||||
else:
|
||||
if self._response.ok:
|
||||
self._url_available = True
|
||||
|
||||
else:
|
||||
if show_errmsg:
|
||||
raise ConnectionError(_S._lang.join(_S._lang.STATUS_CODE_, self._response.status_code))
|
||||
self._url_available = False
|
||||
|
||||
return self._url_available
|
||||
|
||||
def _make_response(self, url, mode='get', retry=None, interval=None, show_errmsg=False, **kwargs):
|
||||
kwargs = CaseInsensitiveDict(kwargs)
|
||||
if 'headers' in kwargs:
|
||||
kwargs['headers'] = CaseInsensitiveDict(format_headers(kwargs['headers']))
|
||||
else:
|
||||
kwargs['headers'] = CaseInsensitiveDict()
|
||||
|
||||
# 设置referer和host值
|
||||
parsed_url = urlparse(url)
|
||||
hostname = parsed_url.netloc
|
||||
scheme = parsed_url.scheme
|
||||
if not check_headers(kwargs['headers'], self._headers, 'Referer'):
|
||||
kwargs['headers']['Referer'] = self.url if self.url else f'{scheme}://{hostname}'
|
||||
elif not kwargs['headers']['Referer']:
|
||||
kwargs['headers'].pop('Referer')
|
||||
if not check_headers(kwargs['headers'], self._headers, 'Host'):
|
||||
kwargs['headers']['Host'] = hostname
|
||||
elif not kwargs['headers']['Host']:
|
||||
kwargs['headers'].pop('Host')
|
||||
if not check_headers(kwargs, self._headers, 'timeout'):
|
||||
kwargs['timeout'] = self.timeout
|
||||
|
||||
h = CaseInsensitiveDict(self._headers)
|
||||
for k, v in kwargs['headers'].items():
|
||||
h[k] = v
|
||||
kwargs['headers'] = h
|
||||
|
||||
r = err = None
|
||||
retry = retry if retry is not None else self.retry_times
|
||||
interval = interval if interval is not None else self.retry_interval
|
||||
for i in range(retry + 1):
|
||||
try:
|
||||
if mode == 'get':
|
||||
r = self.session.get(url, **kwargs)
|
||||
elif mode == 'post':
|
||||
r = self.session.post(url, **kwargs)
|
||||
|
||||
if r and r.content:
|
||||
if self._encoding:
|
||||
r.encoding = self._encoding
|
||||
return r
|
||||
return set_charset(r)
|
||||
|
||||
except Exception as e:
|
||||
err = e
|
||||
|
||||
# if r and r.status_code in (403, 404):
|
||||
# break
|
||||
|
||||
if i < retry:
|
||||
sleep(interval)
|
||||
if show_errmsg:
|
||||
print(f'{_S._lang.RETRY} {url}')
|
||||
|
||||
if show_errmsg:
|
||||
if err:
|
||||
raise err
|
||||
elif r is not None:
|
||||
raise (ConnectionError(_S._lang.join(_S._lang.STATUS_CODE_, r.status_code)) if r.content
|
||||
else ConnectionError(_S._lang.join(_S._lang.CONTENT_IS_EMPTY)))
|
||||
else:
|
||||
raise ConnectionError(_S._lang.join(_S._lang.CONNECT_ERR))
|
||||
|
||||
else:
|
||||
if r is not None:
|
||||
return r if r.content else None
|
||||
else:
|
||||
return None
|
||||
|
||||
|
||||
def check_headers(kwargs, headers, arg):
|
||||
return arg in kwargs or arg in headers
|
||||
|
||||
|
||||
def set_charset(response):
|
||||
# 在headers中获取编码
|
||||
content_type = response.headers.get('content-type', '').lower()
|
||||
if not content_type.endswith(';'):
|
||||
content_type += ';'
|
||||
charset = search(r'charset[=: ]*(.*)?;?', content_type)
|
||||
|
||||
if charset:
|
||||
response.encoding = charset.group(1)
|
||||
|
||||
# 在headers中获取不到编码,且如果是网页
|
||||
elif content_type.replace(' ', '').startswith('text/html'):
|
||||
re_result = search(b'<meta.*?charset=[ \\\'"]*([^"\\\' />]+).*?>', response.content, DOTALL)
|
||||
|
||||
if re_result:
|
||||
charset = re_result.group(1).decode()
|
||||
else:
|
||||
charset = response.apparent_encoding
|
||||
|
||||
response.encoding = charset
|
||||
|
||||
return response
|
||||
@@ -0,0 +1,322 @@
|
||||
# -*- coding:utf-8 -*-
|
||||
"""
|
||||
@Author : g1879
|
||||
@Contact : g1879@qq.com
|
||||
@Website : https://DrissionPage.cn
|
||||
@Copyright: (c) 2020 by g1879, Inc. All Rights Reserved.
|
||||
"""
|
||||
from pathlib import Path
|
||||
from typing import Any, Union, Tuple, Optional
|
||||
|
||||
from requests import Session, Response
|
||||
from requests.structures import CaseInsensitiveDict
|
||||
|
||||
from .._base.base import BasePage
|
||||
from .._configs.session_options import SessionOptions
|
||||
from .._elements.session_element import SessionElement
|
||||
from .._functions.cookies import CookiesList
|
||||
from .._functions.elements import SessionElementsList
|
||||
from .._units.setter import SessionPageSetter
|
||||
|
||||
|
||||
class SessionPage(BasePage):
|
||||
"""SessionPage封装了页面操作的常用功能,使用requests来获取、解析网页"""
|
||||
_session_options: Optional[SessionOptions] = ...
|
||||
_url: str = ...
|
||||
_response: Optional[Response] = ...
|
||||
_url_available: bool = ...
|
||||
_timeout: float = ...
|
||||
retry_times: int = ...
|
||||
retry_interval: float = ...
|
||||
_set: Optional[SessionPageSetter] = ...
|
||||
_encoding: Optional[str] = ...
|
||||
_page: SessionPage = ...
|
||||
|
||||
def __init__(self, session_or_options: Union[Session, SessionOptions] = None):
|
||||
"""
|
||||
:param session_or_options: Session对象或SessionOptions对象
|
||||
"""
|
||||
...
|
||||
|
||||
def _s_set_runtime_settings(self) -> None:
|
||||
"""设置运行时用到的属性"""
|
||||
...
|
||||
|
||||
def __call__(self,
|
||||
locator: Union[Tuple[str, str], str, SessionElement],
|
||||
index: int = 1,
|
||||
timeout: float = None) -> SessionElement:
|
||||
"""在内部查找元素
|
||||
例:ele2 = ele1('@id=ele_id')
|
||||
:param locator: 元素的定位信息,可以是loc元组,或查询字符串
|
||||
:param index: 获取第几个,从1开始,可传入负数获取倒数第几个
|
||||
:param timeout: 不起实际作用,用于和ChromiumElement对应,便于无差别调用
|
||||
:return: SessionElement对象或属性文本
|
||||
"""
|
||||
...
|
||||
|
||||
@property
|
||||
def title(self) -> str:
|
||||
"""返回网页title"""
|
||||
...
|
||||
|
||||
@property
|
||||
def url(self) -> str:
|
||||
"""返回当前访问url"""
|
||||
...
|
||||
|
||||
@property
|
||||
def _session_url(self) -> str:
|
||||
"""返回当前访问url"""
|
||||
...
|
||||
|
||||
@property
|
||||
def raw_data(self) -> Union[str, bytes]:
|
||||
"""返回页面原始数据"""
|
||||
...
|
||||
|
||||
@property
|
||||
def html(self) -> str:
|
||||
"""返回页面的html文本"""
|
||||
...
|
||||
|
||||
@property
|
||||
def json(self) -> Union[dict, None]:
|
||||
"""当返回内容是json格式时,返回对应的字典,非json格式时返回None"""
|
||||
...
|
||||
|
||||
@property
|
||||
def user_agent(self) -> str:
|
||||
"""返回user agent"""
|
||||
...
|
||||
|
||||
@property
|
||||
def session(self) -> Session:
|
||||
"""返回Session对象"""
|
||||
...
|
||||
|
||||
@property
|
||||
def response(self) -> Response:
|
||||
"""返回访问url得到的Response对象"""
|
||||
...
|
||||
|
||||
@property
|
||||
def encoding(self) -> str:
|
||||
"""返回设置的编码,s模式专用"""
|
||||
...
|
||||
|
||||
@property
|
||||
def set(self) -> SessionPageSetter:
|
||||
"""返回用于设置的对象"""
|
||||
...
|
||||
|
||||
@property
|
||||
def timeout(self) -> float:
|
||||
"""返回超时设置"""
|
||||
...
|
||||
|
||||
def get(self,
|
||||
url: Union[Path, str],
|
||||
show_errmsg: bool | None = False,
|
||||
retry: int | None = None,
|
||||
interval: float | None = None,
|
||||
timeout: float | None = None,
|
||||
params: dict | None = ...,
|
||||
data: Any = ...,
|
||||
json: Any = ...,
|
||||
headers: Union[dict, str, None] = ...,
|
||||
cookies: Any | None = ...,
|
||||
files: Any | None = ...,
|
||||
auth: Any | None = ...,
|
||||
allow_redirects: bool = ...,
|
||||
proxies: dict | None = ...,
|
||||
hooks: Any | None = ...,
|
||||
stream: Any | None = ...,
|
||||
verify: Any | None = ...,
|
||||
cert: Any | None = ...) -> bool:
|
||||
"""用get方式跳转到url
|
||||
:param url: 目标url
|
||||
:param show_errmsg: 是否显示和抛出异常
|
||||
:param retry: 重试次数,为None时使用页面对象retry_times属性值
|
||||
:param interval: 重试间隔(秒),为None时使用页面对象retry_interval属性值
|
||||
:param timeout: 连接超时时间
|
||||
:param params: url中的参数
|
||||
:param data: 携带的数据
|
||||
:param json: 要发送的 JSON 数据,会自动设置 Content-Type 为 application/json
|
||||
:param headers: 请求头
|
||||
:param cookies: cookies信息
|
||||
:param files: 要上传的文件,可以是一个字典,其中键是文件名,值是文件对象或文件路径
|
||||
:param auth: 身份认证信息
|
||||
:param allow_redirects: 是否允许重定向
|
||||
:param proxies: 代理信息
|
||||
:param hooks: 回调方法
|
||||
:param stream: 是否使用流式传输
|
||||
:param verify: 是否验证 SSL 证书
|
||||
:param cert: SSL客户端证书文件的路径(.pem格式),或('cert', 'key')元组
|
||||
:return: s模式时返回url是否可用,d模式时返回获取到的Response对象
|
||||
"""
|
||||
...
|
||||
|
||||
def post(self,
|
||||
url: str,
|
||||
show_errmsg: bool = False,
|
||||
retry: int | None = None,
|
||||
interval: float | None = None,
|
||||
timeout: float | None = ...,
|
||||
params: dict | None = ...,
|
||||
data: Any = ...,
|
||||
json: Any = ...,
|
||||
headers: Union[dict, str, None] = ...,
|
||||
cookies: Any | None = ...,
|
||||
files: Any | None = ...,
|
||||
auth: Any | None = ...,
|
||||
allow_redirects: bool = ...,
|
||||
proxies: dict | None = ...,
|
||||
hooks: Any | None = ...,
|
||||
stream: Any | None = ...,
|
||||
verify: Any | None = ...,
|
||||
cert: Any | None = ...) -> bool:
|
||||
"""用post方式跳转到url
|
||||
:param url: 目标url
|
||||
:param show_errmsg: 是否显示和抛出异常
|
||||
:param retry: 重试次数,为None时使用页面对象retry_times属性值
|
||||
:param interval: 重试间隔(秒),为None时使用页面对象retry_interval属性值
|
||||
:param timeout: 连接超时时间
|
||||
:param params: url中的参数
|
||||
:param data: 携带的数据
|
||||
:param json: 要发送的 JSON 数据,会自动设置 Content-Type 为 application/json
|
||||
:param headers: 请求头
|
||||
:param cookies: cookies信息
|
||||
:param files: 要上传的文件,可以是一个字典,其中键是文件名,值是文件对象或文件路径
|
||||
:param auth: 身份认证信息
|
||||
:param allow_redirects: 是否允许重定向
|
||||
:param proxies: 代理信息
|
||||
:param hooks: 回调方法
|
||||
:param stream: 是否使用流式传输
|
||||
:param verify: 是否验证 SSL 证书
|
||||
:param cert: SSL客户端证书文件的路径(.pem格式),或('cert', 'key')元组
|
||||
:return: s模式时返回url是否可用,d模式时返回获取到的Response对象
|
||||
"""
|
||||
...
|
||||
|
||||
def ele(self,
|
||||
locator: Union[Tuple[str, str], str, SessionElement],
|
||||
index: int = 1,
|
||||
timeout: float = None) -> SessionElement:
|
||||
"""返回页面中符合条件的一个元素、属性或节点文本
|
||||
:param locator: 元素的定位信息,可以是元素对象,loc元组,或查询字符串
|
||||
:param index: 获取第几个,从1开始,可传入负数获取倒数第几个
|
||||
:param timeout: 不起实际作用,用于和ChromiumElement对应,便于无差别调用
|
||||
:return: SessionElement对象或属性、文本
|
||||
"""
|
||||
...
|
||||
|
||||
def eles(self,
|
||||
locator: Union[Tuple[str, str], str],
|
||||
timeout: float = None) -> SessionElementsList:
|
||||
"""返回页面中所有符合条件的元素、属性或节点文本
|
||||
:param locator: 元素的定位信息,可以是loc元组,或查询字符串
|
||||
:param timeout: 不起实际作用,用于和ChromiumElement对应,便于无差别调用
|
||||
:return: SessionElement对象或属性、文本组成的列表
|
||||
"""
|
||||
...
|
||||
|
||||
def s_ele(self,
|
||||
locator: Union[Tuple[str, str], str, SessionElement] = None,
|
||||
index: int = 1) -> SessionElement:
|
||||
"""返回页面中符合条件的一个元素、属性或节点文本
|
||||
:param locator: 元素的定位信息,可以是元素对象,loc元组,或查询字符串
|
||||
:param index: 获取第几个,从1开始,可传入负数获取倒数第几个
|
||||
:return: SessionElement对象或属性、文本
|
||||
"""
|
||||
...
|
||||
|
||||
def s_eles(self, locator: Union[Tuple[str, str], str]) -> SessionElementsList:
|
||||
"""返回页面中符合条件的所有元素、属性或节点文本
|
||||
:param locator: 元素的定位信息,可以是元素对象,loc元组,或查询字符串
|
||||
:return: SessionElement对象或属性、文本
|
||||
"""
|
||||
...
|
||||
|
||||
def _find_elements(self,
|
||||
locator: Union[Tuple[str, str], str, SessionElement],
|
||||
timeout: float,
|
||||
index: Optional[int] = 1,
|
||||
relative: bool = True,
|
||||
raise_err: bool = None) -> Union[SessionElement, SessionElementsList]:
|
||||
"""返回页面中符合条件的元素、属性或节点文本,默认返回第一个
|
||||
:param locator: 元素的定位信息,可以是元素对象,loc元组,或查询字符串
|
||||
:param timeout: 不起实际作用,用于和父类对应
|
||||
:param index: 第几个结果,从1开始,可传入负数获取倒数第几个,为None返回所有
|
||||
:param raise_err: 找不到元素是是否抛出异常,为None时根据全局设置
|
||||
:return: SessionElement对象
|
||||
"""
|
||||
...
|
||||
|
||||
def cookies(self,
|
||||
all_domains: bool = False,
|
||||
all_info: bool = False) -> CookiesList:
|
||||
"""返回cookies
|
||||
:param all_domains: 是否返回所有域的cookies
|
||||
:param all_info: 是否返回所有信息,False则只返回name、value、domain
|
||||
:return: cookies组成的列表
|
||||
"""
|
||||
...
|
||||
|
||||
def close(self) -> None:
|
||||
"""关闭Session对象"""
|
||||
...
|
||||
|
||||
def _s_connect(self,
|
||||
url: str,
|
||||
mode: str,
|
||||
show_errmsg: bool = False,
|
||||
retry: int = None,
|
||||
interval: float = None,
|
||||
**kwargs) -> bool:
|
||||
"""执行get或post连接
|
||||
:param url: 目标url
|
||||
:param mode: 'get' 或 'post'
|
||||
:param show_errmsg: 是否显示和抛出异常
|
||||
:param retry: 重试次数
|
||||
:param interval: 重试间隔(秒)
|
||||
:param kwargs: 连接参数
|
||||
:return: url是否可用
|
||||
"""
|
||||
...
|
||||
|
||||
def _make_response(self,
|
||||
url: str,
|
||||
mode: str = 'get',
|
||||
retry: int = None,
|
||||
interval: float = None,
|
||||
show_errmsg: bool = False,
|
||||
**kwargs) -> Response:
|
||||
"""生成Response对象
|
||||
:param url: 目标url
|
||||
:param mode: 'get' 或 'post'
|
||||
:param show_errmsg: 是否显示和抛出异常
|
||||
:param kwargs: 其它参数
|
||||
:return: Response对象
|
||||
"""
|
||||
...
|
||||
|
||||
|
||||
def check_headers(kwargs: Union[dict, CaseInsensitiveDict],
|
||||
headers: Union[dict, CaseInsensitiveDict],
|
||||
arg: str) -> bool:
|
||||
"""检查kwargs或headers中是否有arg所示属性
|
||||
:param kwargs: 要检查的参数dict
|
||||
:param headers: 要检查的headers
|
||||
:param arg: 属性名称
|
||||
:return: 检查结果
|
||||
"""
|
||||
...
|
||||
|
||||
|
||||
def set_charset(response: Response) -> Response:
|
||||
"""设置Response对象的编码
|
||||
:param response: Response对象
|
||||
:return: Response对象
|
||||
"""
|
||||
...
|
||||
Loaded 100 of 7311 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user