4.0.4.5完善tree()

This commit is contained in:
g1879 2024-02-05 15:47:55 +08:00
parent 6451eab8a4
commit 7bb1e64a79
5 changed files with 36 additions and 25 deletions

View File

@ -36,7 +36,7 @@ class BaseParser(object):
def html(self): def html(self):
return '' return ''
def s_ele(self, locator): def s_ele(self, locator=None):
pass pass
def s_eles(self, locator): def s_eles(self, locator):

View File

@ -11,6 +11,7 @@ from typing import Union, Tuple, List, Any, Optional
from DownloadKit import DownloadKit from DownloadKit import DownloadKit
from .._elements.none_element import NoneElement from .._elements.none_element import NoneElement
from .._elements.session_element import SessionElement
class BaseParser(object): class BaseParser(object):
@ -29,9 +30,11 @@ class BaseParser(object):
@property @property
def html(self) -> str: ... def html(self) -> str: ...
def s_ele(self, locator: Union[Tuple[str, str], str, BaseElement], index: int = 1): ... def s_ele(self,
locator: Union[Tuple[str, str], str, BaseElement, None] = None,
index: int = 1) -> SessionElement: ...
def s_eles(self, locator: Union[Tuple[str, str], str]): ... def s_eles(self, locator: Union[Tuple[str, str], str]) -> List[SessionElement]: ...
def _ele(self, def _ele(self,
locator: Union[Tuple[str, str], str], locator: Union[Tuple[str, str], str],

View File

@ -365,24 +365,32 @@ def get_blob(page, url, as_bytes=True):
return result return result
def tree(ele_or_page, layer=5, last_one=False, body=''): def tree(ele_or_page):
try: """把页面或元素对象DOM结构打印出来
list_ele = ele_or_page.s_ele().children(timeout=0.1) :param ele_or_page: 页面或元素对象
except: :return: None
list_ele = [] """
length = len(list_ele) def _tree(obj, last_one=True, body=''):
body_unit = ' ' if last_one else '' list_ele = obj.children()
tail = '├───' length = len(list_ele)
new_body = body + body_unit body_unit = ' ' if last_one else ''
tail = '├───'
new_body = body + body_unit
if length > 0 and layer >= 1: if length > 0:
new_last_one = False new_last_one = False
for i in range(length): for i in range(length):
if i == length - 1: if i == length - 1:
tail = '└───' tail = '└───'
new_last_one = True new_last_one = True
e = list_ele[i] e = list_ele[i]
print(f'{new_body}{tail}{i}<{e.tag}> {e.attrs}') attrs = ' '.join([f"{k}='{v}'" for k, v in e.attrs.items()])
print(f'{new_body}{tail}<{e.tag} {attrs}>'.replace('\n', ' '))
tree(e, layer - 1, new_last_one, new_body) _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()])
print(f'<{ele.tag} {attrs}>'.replace('\n', ' '))
_tree(ele)

View File

@ -11,7 +11,7 @@ from typing import Union
from requests import Session from requests import Session
from requests.cookies import RequestsCookieJar from requests.cookies import RequestsCookieJar
from .._base.base import BasePage, DrissionElement from .._base.base import DrissionElement, BaseParser
from .._elements.chromium_element import ChromiumElement from .._elements.chromium_element import ChromiumElement
from .._pages.chromium_base import ChromiumBase from .._pages.chromium_base import ChromiumBase
@ -52,4 +52,4 @@ def is_cookie_in_driver(page: ChromiumBase, cookie: dict) -> bool: ...
def get_blob(page: ChromiumBase, url: str, as_bytes: bool = True) -> bytes: ... def get_blob(page: ChromiumBase, url: str, as_bytes: bool = True) -> bytes: ...
def tree(ele_or_page, layer=5, last_one=False, body='') -> None: ... def tree(ele_or_page:BaseParser) -> None: ...

View File

@ -10,7 +10,7 @@ from ._functions.by import By
from ._functions.keys import Keys from ._functions.keys import Keys
from ._functions.settings import Settings from ._functions.settings import Settings
from ._functions.tools import wait_until, configs_to_here from ._functions.tools import wait_until, configs_to_here
from ._functions.web import get_blob from ._functions.web import get_blob, tree
from ._units.actions import Actions from ._units.actions import Actions
__all__ = ['make_session_ele', 'Actions', 'Keys', 'By', 'Settings', 'wait_until', 'configs_to_here', 'get_blob'] __all__ = ['make_session_ele', 'Actions', 'Keys', 'By', 'Settings', 'wait_until', 'configs_to_here', 'get_blob', 'tree']