修复继承后出现的问题

This commit is contained in:
g1879 2024-01-18 10:43:11 +08:00
parent 4f12493edb
commit 5815474ea9
13 changed files with 32 additions and 21 deletions

View File

@ -53,6 +53,7 @@ class BaseElement(BaseParser):
def __init__(self, page=None):
self.page = page
self._type = 'BaseElement'
# ----------------以下属性或方法由后代实现----------------
@property
@ -362,6 +363,7 @@ class BasePage(BaseParser):
self._download_path = None
self._none_ele_return_value = False
self._none_ele_value = None
self._type = 'BasePage'
@property
def title(self):

View File

@ -14,6 +14,7 @@ from .._elements.none_element import NoneElement
class BaseParser(object):
_type: str
def __call__(self, loc_or_str: Union[Tuple[str, str], str], index: int = 1): ...

View File

@ -53,6 +53,7 @@ class ChromiumElement(DrissionElement):
self._clicker = None
self._tag = None
self._wait = None
self._type = 'ChromiumElement'
if node_id and obj_id and backend_id:
self._node_id = node_id
@ -783,6 +784,7 @@ class ShadowRoot(BaseElement):
self._node_id = self._get_node_id(obj_id)
self._backend_id = self._get_backend_id(self._node_id)
self._states = None
self._type = 'ShadowRoot'
def __repr__(self):
return f'<ShadowRoot in {self.parent_ele}>'

View File

@ -305,9 +305,13 @@ def make_session_ele(html_or_ele, loc=None, index=1):
raise ValueError("定位符必须为str或长度为2的tuple。")
# ---------------根据传入对象类型获取页面对象和lxml元素对象---------------
the_type = str(type(html_or_ele))
# 直接传入html文本
if isinstance(html_or_ele, str):
page = None
html_or_ele = fromstring(html_or_ele)
# SessionElement
if the_type.endswith(".SessionElement'>"):
elif html_or_ele._type == 'SessionElement':
page = html_or_ele.page
loc_str = loc[1]
@ -328,7 +332,7 @@ def make_session_ele(html_or_ele, loc=None, index=1):
loc = loc[0], loc_str
elif the_type.endswith(".ChromiumElement'>"):
elif html_or_ele._type == 'ChromiumElement':
loc_str = loc[1]
if loc[0] == 'xpath' and loc[1].lstrip().startswith('/'):
loc_str = f'.{loc[1]}'
@ -353,11 +357,6 @@ def make_session_ele(html_or_ele, loc=None, index=1):
html = sub(r'^<\?xml.*?>', '', html)
html_or_ele = fromstring(html)
# 直接传入html文本
elif isinstance(html_or_ele, str):
page = None
html_or_ele = fromstring(html_or_ele)
# ShadowRoot
elif isinstance(html_or_ele, BaseElement):
page = html_or_ele.page

View File

@ -16,7 +16,7 @@ from urllib.parse import quote
from DataRecorder.tools import make_valid_name
from .._base.base import BasePage
from .._elements.chromium_element import ChromiumElement, run_js, make_chromium_eles
from .._elements.chromium_element import run_js, make_chromium_eles
from .._elements.none_element import NoneElement
from .._elements.session_element import make_session_ele
from .._functions.locator import get_loc, is_loc
@ -62,6 +62,7 @@ class ChromiumBase(BasePage):
self._download_path = None
self._load_end_time = 0
self._init_jss = []
self._type = 'ChromiumBase'
if not hasattr(self, '_listener'):
self._listener = None
@ -541,7 +542,7 @@ class ChromiumBase(BasePage):
"""
if isinstance(loc_or_ele, (str, tuple)):
loc = get_loc(loc_or_ele)[1]
elif isinstance(loc_or_ele, ChromiumElement) or str(type(loc_or_ele)).endswith(".ChromiumFrame'>"):
elif loc_or_ele._type in ('ChromiumElement', 'ChromiumFrame'):
return loc_or_ele
else:
raise ValueError('loc_or_str参数只能是tuple、str、ChromiumElement类型。')
@ -680,13 +681,13 @@ class ChromiumBase(BasePage):
else:
xpath = loc_ind_ele
ele = self._ele(xpath, timeout=timeout)
if ele and not str(type(ele)).endswith(".ChromiumFrame'>"):
if ele and ele._type != 'ChromiumFrame':
raise TypeError('该定位符不是指向frame元素。')
r = ele
elif isinstance(loc_ind_ele, tuple):
ele = self._ele(loc_ind_ele, timeout=timeout)
if ele and not str(type(ele)).endswith(".ChromiumFrame'>"):
if ele and ele._type != 'ChromiumFrame':
raise TypeError('该定位符不是指向frame元素。')
r = ele
@ -698,7 +699,7 @@ class ChromiumBase(BasePage):
xpath = f'xpath:(//*[name()="frame" or name()="iframe"])[{loc_ind_ele}]'
r = self._ele(xpath, timeout=timeout)
elif str(type(loc_ind_ele)).endswith(".ChromiumFrame'>"):
elif loc_ind_ele._type == 'ChromiumFrame':
r = loc_ind_ele
else:
@ -717,7 +718,7 @@ class ChromiumBase(BasePage):
"""
loc = loc or 'xpath://*[name()="iframe" or name()="frame"]'
frames = self._ele(loc, timeout=timeout, index=None, raise_err=False)
return [i for i in frames if str(type(i)).endswith(".ChromiumFrame'>")]
return [i for i in frames if i._type == 'ChromiumFrame']
def get_session_storage(self, item=None):
"""获取sessionStorage信息不设置item则获取全部

View File

@ -62,6 +62,7 @@ class ChromiumBase(BasePage):
self._init_jss: list = ...
self._ready_state: Optional[str] = ...
self._rect: TabRect = ...
self._type: str = ...
def _connect_browser(self, tab_id: str = None) -> None: ...
@ -190,7 +191,7 @@ class ChromiumBase(BasePage):
def s_ele(self,
loc_or_ele: Union[Tuple[str, str], str] = None,
index:int = 1) -> Union[SessionElement, NoneElement]: ...
index: int = 1) -> Union[SessionElement, NoneElement]: ...
def s_eles(self, loc_or_str: Union[Tuple[str, str], str]) -> List[SessionElement]: ...

View File

@ -27,15 +27,14 @@ class ChromiumFrame(ChromiumBase):
:param ele: frame所在元素
:param info: frame所在元素信息
"""
page_type = str(type(page))
if 'ChromiumPage' in page_type or 'WebPage' in page_type:
if page._type in ('ChromiumPage', 'WebPage'):
self._page = self._target_page = self.tab = page
self._browser = page.browser
else: # Tab、Frame
self._page = page.page
self._browser = self._page.browser
self._target_page = page
self.tab = page.tab if 'ChromiumFrame' in page_type else page
self.tab = page.tab if page._type == 'ChromiumFrame' else page
self.address = page.address
self._tab_id = page.tab_id
@ -58,6 +57,7 @@ class ChromiumFrame(ChromiumBase):
self.doc_ele = ChromiumElement(self, obj_id=obj_id)
self._rect = None
self._type = 'ChromiumFrame'
end_time = perf_counter() + 2
while perf_counter() < end_time:
if self.url not in (None, 'about:blank'):

View File

@ -56,6 +56,7 @@ class ChromiumPage(ChromiumBase):
self._page = self
self._run_browser()
super().__init__(self.address, tab_id)
self._type = 'ChromiumPage'
self.set.timeouts(base=timeout)
self._page_init()

View File

@ -45,6 +45,7 @@ class ChromiumTab(ChromiumBase):
self._browser = page.browser
super().__init__(page.address, tab_id, page.timeout)
self._rect = None
self._type = 'ChromiumTab'
def _d_set_runtime_settings(self):
"""重写设置浏览器运行参数方法"""
@ -106,6 +107,7 @@ class WebPageTab(SessionPage, ChromiumTab, BasePage):
super().__init__(session_or_options=SessionOptions(read_file=False).from_session(copy(page.session),
page._headers))
super(SessionPage, self).__init__(page=page, tab_id=tab_id)
self._type = 'WebPageTab'
def __call__(self, loc_or_str, index=1, timeout=None):
"""在内部查找元素

View File

@ -35,6 +35,7 @@ class SessionPage(BasePage):
self._session = None
self._set = None
self._encoding = None
self._type = 'SessionPage'
self._s_set_start_options(session_or_options)
self._s_set_runtime_settings()
self._create_session()

View File

@ -48,6 +48,7 @@ class WebPage(SessionPage, ChromiumPage, BasePage):
chromium_options = ChromiumOptions(read_file=chromium_options)
chromium_options.set_timeouts(base=self._timeout).set_paths(download_path=self.download_path)
super(SessionPage, self).__init__(addr_or_opts=chromium_options, timeout=timeout)
self._type = 'WebPage'
self.change_mode(self._mode, go=False, copy_cookies=False)
def __call__(self, loc_or_str, index=1, timeout=None):

View File

@ -39,7 +39,7 @@ class Actions:
is_loc = True
lx = ele_or_loc[0] + offset_x
ly = ele_or_loc[1] + offset_y
elif isinstance(ele_or_loc, str) or 'ChromiumElement' in str(type(ele_or_loc)):
elif isinstance(ele_or_loc, str) or ele_or_loc._type == 'ChromiumElement':
ele_or_loc = self.page(ele_or_loc)
self.page.scroll.to_see(ele_or_loc)
x, y = ele_or_loc.rect.location if offset_x or offset_y else ele_or_loc.rect.midpoint

View File

@ -87,7 +87,7 @@ class Scroller(object):
if not self._wait_complete:
return
page = self._driver.page if 'ChromiumElement' in str(type(self._driver)) else self._driver
page = self._driver.page if self._driver._type == 'ChromiumElement' else self._driver
r = page.run_cdp('Page.getLayoutMetrics')
x = r['layoutViewport']['pageX']
y = r['layoutViewport']['pageY']
@ -173,5 +173,5 @@ class FrameScroller(PageScroller):
:param center: 是否尽量滚动到页面正中为None时如果被遮挡则滚动到页面正中
:return: None
"""
ele = loc_or_ele if 'ChromiumElement' in str(type(loc_or_ele)) else self._driver._ele(loc_or_ele)
ele = loc_or_ele if loc_or_ele._type == 'ChromiumElement' else self._driver._ele(loc_or_ele)
self._to_see(ele, center)