Initial commit
This commit is contained in:
commit
57fc3525eb
7311 files changed
+1291398
No files matched your search
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
|
||||
"""
|
||||
...
|
||||
Reference in new issue
Block a user