一些修改(+)

修复auto_port()问题;
修复cookie设置问题
go_on改成resume;
ready_state增加'connecting';
ChromiumShadowRoot改为ShadowRoot
This commit is contained in:
g1879 2023-12-14 16:23:55 +08:00
parent 545b7a0732
commit 0cc81621de
12 changed files with 26 additions and 23 deletions

View File

@ -85,7 +85,7 @@ class BaseElement(BaseParser):
class DrissionElement(BaseElement):
"""ChromiumElement 和 SessionElement的基类
但不是ShadowRootElement的基类"""
但不是ShadowRoot的基类"""
@property
def link(self):

View File

@ -599,19 +599,19 @@ class ChromiumOptions(object):
class PortFinder(object):
used_port = {}
lock = Lock()
def __init__(self):
self.tmp_dir = Path(gettempdir()) / 'DrissionPage' / 'TempFolder'
self.tmp_dir.mkdir(parents=True, exist_ok=True)
if not PortFinder.used_port:
clean_folder(self.tmp_dir)
self._lock = Lock()
def get_port(self):
"""查找一个可用端口
:return: 可以使用的端口和用户文件夹路径组成的元组
"""
with self._lock:
with PortFinder.lock:
for i in range(9600, 19600):
if i in PortFinder.used_port:
continue

View File

@ -4,6 +4,7 @@
@Contact : g1879@qq.com
"""
from pathlib import Path
from threading import Lock
from typing import Union, Tuple, Any, Literal
@ -152,6 +153,7 @@ class ChromiumOptions(object):
class PortFinder(object):
used_port: dict = ...
lock: Lock = ...
@staticmethod
def get_port() -> Tuple[int, str]: ...

View File

@ -159,7 +159,7 @@ class ChromiumElement(DrissionElement):
if not info.get('shadowRoots', None):
return None
return ChromiumShadowRoot(self, backend_id=info['shadowRoots'][0]['backendNodeId'])
return ShadowRoot(self, backend_id=info['shadowRoots'][0]['backendNodeId'])
@property
def sr(self):
@ -764,8 +764,8 @@ class ChromiumElement(DrissionElement):
return self.rect.size
class ChromiumShadowRoot(BaseElement):
"""ChromiumShadowRoot是用于处理ShadowRoot的类使用方法和ChromiumElement基本一致"""
class ShadowRoot(BaseElement):
"""ShadowRoot是用于处理ShadowRoot的类使用方法和ChromiumElement基本一致"""
def __init__(self, parent_ele, obj_id=None, backend_id=None):
"""
@ -786,7 +786,7 @@ class ChromiumShadowRoot(BaseElement):
self._states = None
def __repr__(self):
return f'<ChromiumShadowRoot in {self.parent_ele}>'
return f'<ShadowRoot in {self.parent_ele}>'
def __call__(self, loc_or_str, timeout=None):
"""在内部查找元素
@ -1278,7 +1278,7 @@ def run_js(page_or_ele, script, as_expr=False, timeout=None, args=None):
:param args: 参数按顺序在js文本中对应arguments[0]arguments[1]...
:return: js执行结果
"""
if isinstance(page_or_ele, (ChromiumElement, ChromiumShadowRoot)):
if isinstance(page_or_ele, (ChromiumElement, ShadowRoot)):
page = page_or_ele.page
obj_id = page_or_ele._obj_id
is_page = False
@ -1337,7 +1337,7 @@ def parse_js_result(page, ele, result):
elif sub_type == 'node':
class_name = result['className']
if class_name == 'ShadowRoot':
return ChromiumShadowRoot(ele, obj_id=result['objectId'])
return ShadowRoot(ele, obj_id=result['objectId'])
elif class_name == 'HTMLDocument':
return result
else:

View File

@ -80,10 +80,10 @@ class ChromiumElement(DrissionElement):
def pseudo(self) -> Pseudo: ...
@property
def shadow_root(self) -> Union[None, ChromiumShadowRoot]: ...
def shadow_root(self) -> Union[None, ShadowRoot]: ...
@property
def sr(self) -> Union[None, ChromiumShadowRoot]: ...
def sr(self) -> Union[None, ShadowRoot]: ...
@property
def scroll(self) -> ElementScroller: ...
@ -205,7 +205,7 @@ class ChromiumElement(DrissionElement):
def _get_ele_path(self, mode: str) -> str: ...
class ChromiumShadowRoot(BaseElement):
class ShadowRoot(BaseElement):
def __init__(self, parent_ele: ChromiumElement, obj_id: str = None, backend_id: str = None):
self._obj_id: str = ...
@ -300,7 +300,7 @@ def make_chromium_ele(page: ChromiumBase, node_id: str = ..., obj_id: str = ...)
def make_js_for_find_ele_by_xpath(xpath: str, type_txt: str, node_txt: str) -> str: ...
def run_js(page_or_ele: Union[ChromiumBase, ChromiumElement, ChromiumShadowRoot], script: str,
def run_js(page_or_ele: Union[ChromiumBase, ChromiumElement, ShadowRoot], script: str,
as_expr: bool = False, timeout: float = None, args: tuple = ...) -> Any: ...

View File

@ -349,7 +349,7 @@ def make_session_ele(html_or_ele, loc=None, single=True):
page = None
html_or_ele = fromstring(html_or_ele)
# ShadowRootElement, ChromiumShadowRoot, ChromiumFrame
# ShadowRoot, ChromiumFrame
elif isinstance(html_or_ele, BaseElement) or the_type.endswith(".ChromiumFrame'>"):
page = html_or_ele.page
html_or_ele = fromstring(html_or_ele.html)

View File

@ -250,7 +250,8 @@ def set_browser_cookies(page, cookies):
if 'expiry' in cookie:
cookie['expires'] = int(cookie['expiry'])
cookie.pop('expiry')
if 'expires' in cookie:
if 'expires' in cookie and isinstance(cookie['expires'], str):
if cookie['expires'].isdigit():
cookie['expires'] = int(cookie['expires'])

View File

@ -190,7 +190,7 @@ class ChromiumBase(BasePage):
print('在FrameStartedLoading变成loading')
self._doc_got = False
self._ready_state = 'loading'
self._ready_state = 'connecting'
self._is_loading = True
self._load_end_time = perf_counter() + self.timeouts.page_load
if self._load_mode == 'eager':

View File

@ -173,7 +173,7 @@ class Listener(object):
if clear:
self.clear()
def go_on(self):
def resume(self):
"""继续暂停的监听"""
if self.listening:
return

View File

@ -38,7 +38,7 @@ class Listener(object):
def pause(self, clear: bool = True) -> None: ...
def go_on(self) -> None: ...
def resume(self) -> None: ...
def wait(self, count: int = 1, timeout: float = None, fit_count: bool = True,
raise_err: bool = None) -> Union[List[DataPacket], DataPacket, None]: ...

View File

@ -129,12 +129,12 @@ class PageStates(object):
@property
def ready_state(self):
"""返回当前页面加载状态,'loading' 'interactive' 'complete'"""
"""返回当前页面加载状态,'connecting' 'loading' 'interactive' 'complete'"""
return self._page._ready_state
@property
def has_alert(self):
"""返回当前页面加载状态,'loading' 'interactive' 'complete'"""
"""返回当前页面是否存在弹窗"""
return self._page._has_alert

View File

@ -5,7 +5,7 @@
"""
from typing import Union, Tuple, List, Optional
from .._elements.chromium_element import ChromiumShadowRoot, ChromiumElement
from .._elements.chromium_element import ShadowRoot, ChromiumElement
from .._pages.chromium_base import ChromiumBase
from .._pages.chromium_frame import ChromiumFrame
@ -43,11 +43,11 @@ class ElementStates(object):
class ShadowRootStates(object):
def __init__(self, ele: ChromiumShadowRoot):
def __init__(self, ele: ShadowRoot):
"""
:param ele: ChromiumElement
"""
self._ele: ChromiumShadowRoot = ...
self._ele: ShadowRoot = ...
@property
def is_enabled(self) -> bool: ...