mirror of
https://gitee.com/g1879/DrissionPage.git
synced 2024-12-10 04:00:23 +08:00
支持离线运行;动作链type()增加interval参数
This commit is contained in:
parent
b854df764c
commit
421bbc470b
@ -8,7 +8,9 @@
|
||||
from datetime import datetime
|
||||
from http.cookiejar import Cookie, CookieJar
|
||||
|
||||
from tldextract import extract
|
||||
from tldextract import TLDExtract
|
||||
|
||||
from .settings import Settings
|
||||
|
||||
|
||||
def cookie_to_dict(cookie):
|
||||
@ -90,6 +92,7 @@ def set_browser_cookies(browser, cookies):
|
||||
|
||||
|
||||
def set_tab_cookies(page, cookies):
|
||||
suffixes_list = f"file:///{Settings.locate_suffixes_list}"
|
||||
for cookie in cookies_to_tuple(cookies):
|
||||
cookie = format_cookie(cookie)
|
||||
|
||||
@ -113,7 +116,8 @@ def set_tab_cookies(page, cookies):
|
||||
url = page._browser_url
|
||||
if not url.startswith('http'):
|
||||
raise RuntimeError(f'未设置域名,请设置cookie的domain参数或先访问一个网站。{cookie}')
|
||||
ex_url = extract(url)
|
||||
ex_url = TLDExtract(suffix_list_urls=["https://publicsuffix.org/list/public_suffix_list.dat",
|
||||
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)
|
||||
|
||||
|
@ -5,6 +5,7 @@
|
||||
@Copyright: (c) 2024 by g1879, Inc. All Rights Reserved.
|
||||
@License : BSD 3-Clause.
|
||||
"""
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
class Settings(object):
|
||||
@ -14,3 +15,7 @@ class Settings(object):
|
||||
singleton_tab_obj = True
|
||||
cdp_timeout = 30
|
||||
auto_handle_alert = None
|
||||
locate_suffixes_list = str(Path(__file__).parent.absolute() / 'suffixes.dat').replace('\\', '/')
|
||||
|
||||
def set_suffixes_list_path(self, path):
|
||||
Settings.locate_suffixes_list = str(Path(path).absolute())
|
||||
|
26
DrissionPage/_functions/settings.pyi
Normal file
26
DrissionPage/_functions/settings.pyi
Normal file
@ -0,0 +1,26 @@
|
||||
# -*- coding:utf-8 -*-
|
||||
"""
|
||||
@Author : g1879
|
||||
@Contact : g1879@qq.com
|
||||
@Copyright: (c) 2024 by g1879, Inc. All Rights Reserved.
|
||||
@License : BSD 3-Clause.
|
||||
"""
|
||||
from pathlib import Path
|
||||
from typing import Optional, Union
|
||||
|
||||
|
||||
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 = ...
|
||||
auto_handle_alert: Optional[bool] = ...
|
||||
locate_suffixes_list: str = ...
|
||||
|
||||
def set_suffixes_list_path(self, path: Union[str, Path]) -> None:
|
||||
"""设置用于识别域名后缀的文件路径
|
||||
:param path: 文件路径
|
||||
:return: None
|
||||
"""
|
||||
...
|
11792
DrissionPage/_functions/suffixes.dat
Normal file
11792
DrissionPage/_functions/suffixes.dat
Normal file
File diff suppressed because it is too large
Load Diff
@ -12,11 +12,12 @@ from urllib.parse import urlparse
|
||||
|
||||
from requests import Response
|
||||
from requests.structures import CaseInsensitiveDict
|
||||
from tldextract import extract
|
||||
from tldextract import TLDExtract
|
||||
|
||||
from .._base.base import BasePage
|
||||
from .._elements.session_element import SessionElement, make_session_ele
|
||||
from .._functions.cookies import cookie_to_dict, CookiesList
|
||||
from .._functions.settings import Settings
|
||||
from .._functions.web import format_headers
|
||||
from .._units.setter import SessionPageSetter
|
||||
|
||||
@ -155,7 +156,9 @@ class SessionPage(BasePage):
|
||||
cookies = self.session.cookies
|
||||
else:
|
||||
if self.url:
|
||||
ex_url = extract(self._session_url)
|
||||
ex_url = TLDExtract(
|
||||
suffix_list_urls=["https://publicsuffix.org/list/public_suffix_list.dat",
|
||||
f"file:///{Settings.locate_suffixes_list}"]).extract_str(self._session_url)
|
||||
domain = f'{ex_url.domain}.{ex_url.suffix}' if ex_url.suffix else ex_url.domain
|
||||
cookies = tuple(c for c in self.session.cookies if domain in c.domain or c.domain == '')
|
||||
else:
|
||||
|
@ -179,7 +179,7 @@ class Actions:
|
||||
self.owner._run_cdp('Input.dispatchKeyEvent', **data)
|
||||
return self
|
||||
|
||||
def type(self, keys):
|
||||
def type(self, keys, interval=0):
|
||||
modifiers = []
|
||||
if not isinstance(keys, (str, tuple, list)):
|
||||
keys = str(keys)
|
||||
@ -197,6 +197,7 @@ class Actions:
|
||||
|
||||
else:
|
||||
self.owner._run_cdp('Input.dispatchKeyEvent', type='char', text=character)
|
||||
sleep(interval)
|
||||
|
||||
for m in modifiers:
|
||||
self.key_up(m)
|
||||
|
@ -66,7 +66,7 @@ class Actions:
|
||||
:param offset_x: 偏移量x
|
||||
:param offset_y: 偏移量y
|
||||
:param duration: 拖动用时,传入0即瞬间到达
|
||||
:return: self
|
||||
:return: 动作链对象本身
|
||||
"""
|
||||
...
|
||||
|
||||
@ -75,7 +75,7 @@ class Actions:
|
||||
:param offset_x: 偏移量x
|
||||
:param offset_y: 偏移量y
|
||||
:param duration: 拖动用时,传入0即瞬间到达
|
||||
:return: self
|
||||
:return: 动作链对象本身
|
||||
"""
|
||||
...
|
||||
|
||||
@ -83,7 +83,7 @@ class Actions:
|
||||
"""点击鼠标左键,可先移动到元素上
|
||||
:param on_ele: ChromiumElement元素或文本定位符
|
||||
:param times: 点击次数
|
||||
:return: self
|
||||
:return: 动作链对象本身
|
||||
"""
|
||||
...
|
||||
|
||||
@ -91,7 +91,7 @@ class Actions:
|
||||
"""点击鼠标右键,可先移动到元素上
|
||||
:param on_ele: ChromiumElement元素或文本定位符
|
||||
:param times: 点击次数
|
||||
:return: self
|
||||
:return: 动作链对象本身
|
||||
"""
|
||||
...
|
||||
|
||||
@ -99,49 +99,49 @@ class Actions:
|
||||
"""点击鼠标中键,可先移动到元素上
|
||||
:param on_ele: ChromiumElement元素或文本定位符
|
||||
:param times: 点击次数
|
||||
:return: self
|
||||
:return: 动作链对象本身
|
||||
"""
|
||||
...
|
||||
|
||||
def hold(self, on_ele: Union[ChromiumElement, str] = None) -> Actions:
|
||||
"""按住鼠标左键,可先移动到元素上
|
||||
:param on_ele: ChromiumElement元素或文本定位符
|
||||
:return: self
|
||||
:return: 动作链对象本身
|
||||
"""
|
||||
...
|
||||
|
||||
def release(self, on_ele: Union[ChromiumElement, str] = None) -> Actions:
|
||||
"""释放鼠标左键,可先移动到元素上
|
||||
:param on_ele: ChromiumElement元素或文本定位符
|
||||
:return: self
|
||||
:return: 动作链对象本身
|
||||
"""
|
||||
...
|
||||
|
||||
def r_hold(self, on_ele: Union[ChromiumElement, str] = None) -> Actions:
|
||||
"""按住鼠标右键,可先移动到元素上
|
||||
:param on_ele: ChromiumElement元素或文本定位符
|
||||
:return: self
|
||||
:return: 动作链对象本身
|
||||
"""
|
||||
...
|
||||
|
||||
def r_release(self, on_ele: Union[ChromiumElement, str] = None) -> Actions:
|
||||
"""释放鼠标右键,可先移动到元素上
|
||||
:param on_ele: ChromiumElement元素或文本定位符
|
||||
:return: self
|
||||
:return: 动作链对象本身
|
||||
"""
|
||||
...
|
||||
|
||||
def m_hold(self, on_ele: Union[ChromiumElement, str] = None) -> Actions:
|
||||
"""按住鼠标中键,可先移动到元素上
|
||||
:param on_ele: ChromiumElement元素或文本定位符
|
||||
:return: self
|
||||
:return: 动作链对象本身
|
||||
"""
|
||||
...
|
||||
|
||||
def m_release(self, on_ele: Union[ChromiumElement, str] = None) -> Actions:
|
||||
"""释放鼠标中键,可先移动到元素上
|
||||
:param on_ele: ChromiumElement元素或文本定位符
|
||||
:return: self
|
||||
:return: 动作链对象本身
|
||||
"""
|
||||
...
|
||||
|
||||
@ -153,14 +153,14 @@ class Actions:
|
||||
:param on_ele: ChromiumElement元素或文本定位符
|
||||
:param button: 要按下的按键
|
||||
:param count: 点击次数
|
||||
:return: self
|
||||
:return: 动作链对象本身
|
||||
"""
|
||||
...
|
||||
|
||||
def _release(self, button: str) -> Actions:
|
||||
"""释放鼠标按键
|
||||
:param button: 要释放的按键
|
||||
:return: self
|
||||
:return: 动作链对象本身
|
||||
"""
|
||||
...
|
||||
|
||||
@ -170,63 +170,66 @@ class Actions:
|
||||
:param delta_y: 滚轮变化值y
|
||||
:param delta_x: 滚轮变化值x
|
||||
:param on_ele: ChromiumElement元素
|
||||
:return: self
|
||||
:return: 动作链对象本身
|
||||
"""
|
||||
...
|
||||
|
||||
def up(self, pixel: int) -> Actions:
|
||||
"""鼠标向上移动若干像素
|
||||
:param pixel: 鼠标移动的像素值
|
||||
:return: self
|
||||
:return: 动作链对象本身
|
||||
"""
|
||||
...
|
||||
|
||||
def down(self, pixel: int) -> Actions:
|
||||
"""鼠标向下移动若干像素
|
||||
:param pixel: 鼠标移动的像素值
|
||||
:return: self
|
||||
:return: 动作链对象本身
|
||||
"""
|
||||
...
|
||||
|
||||
def left(self, pixel: int) -> Actions:
|
||||
"""鼠标向左移动若干像素
|
||||
:param pixel: 鼠标移动的像素值
|
||||
:return: self
|
||||
:return: 动作链对象本身
|
||||
"""
|
||||
...
|
||||
|
||||
def right(self, pixel: int) -> Actions:
|
||||
"""鼠标向右移动若干像素
|
||||
:param pixel: 鼠标移动的像素值
|
||||
:return: self
|
||||
:return: 动作链对象本身
|
||||
"""
|
||||
...
|
||||
|
||||
def key_down(self, key: Union[KEYS, str]) -> Actions:
|
||||
"""按下键盘上的按键,
|
||||
:param key: 使用Keys获取的按键,或 'DEL' 形式按键名称
|
||||
:return: self
|
||||
:return: 动作链对象本身
|
||||
"""
|
||||
...
|
||||
|
||||
def key_up(self, key: Union[KEYS, str]) -> Actions:
|
||||
"""提起键盘上的按键
|
||||
:param key: 按键,特殊字符见Keys
|
||||
:return: self
|
||||
:return: 动作链对象本身
|
||||
"""
|
||||
...
|
||||
|
||||
def type(self, keys: Union[KEYS, str, list, tuple]) -> Actions:
|
||||
def type(self,
|
||||
keys: Union[KEYS, str, list, tuple],
|
||||
interval: float = 0) -> Actions:
|
||||
"""用模拟键盘按键方式输入文本,可输入字符串,也可输入组合键
|
||||
:param keys: 要按下的按键,特殊字符和多个文本可用list或tuple传入
|
||||
:return: self
|
||||
:param interval: 每个字符之间间隔时间
|
||||
:return: 动作链对象本身
|
||||
"""
|
||||
...
|
||||
|
||||
def input(self, text: Any) -> Actions:
|
||||
"""输入文本,也可输入组合键,组合键用tuple形式输入
|
||||
:param text: 文本值或按键组合
|
||||
:return: self
|
||||
:return: 动作链对象本身
|
||||
"""
|
||||
...
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user