tree()可显示文本;修复SessionPage()访问本地中文路径的问题;tab_ids不再屏蔽插件tab

This commit is contained in:
g1879 2024-05-11 17:50:36 +08:00
parent c14a3af41b
commit 514a9da2f4
5 changed files with 34 additions and 7 deletions

View File

@ -14,4 +14,4 @@ from ._configs.chromium_options import ChromiumOptions
from ._configs.session_options import SessionOptions
__all__ = ['ChromiumPage', 'ChromiumOptions', 'SessionOptions', 'SessionPage', 'WebPage', '__version__']
__version__ = '4.0.4.22'
__version__ = '4.0.4.23'

View File

@ -411,7 +411,8 @@ class BasePage(BaseParser):
if p.exists():
url = str(p.absolute())
is_file = True
self._url = quote(url, safe='-_.~!*\'"();:@&=+$,/\\?#[]%')
self._url = url if is_file else quote(url, safe='-_.~!*\'"();:@&=+$,/\\?#[]%')
retry = retry if retry is not None else self.retry_times
interval = interval if interval is not None else self.retry_interval
return retry, interval, is_file

View File

@ -135,7 +135,7 @@ class Browser(object):
"""返回所有标签页id组成的列表"""
j = self._driver.get(f'http://{self.address}/json').json() # 不要改用cdp因为顺序不对
return [i['id'] for i in j if i['type'] in ('page', 'webview')
and not i['url'].startswith(('devtools://', 'chrome-extension://'))]
and not i['url'].startswith('devtools://')]
@property
def process_id(self):

View File

@ -466,9 +466,12 @@ def get_pdf(page, path=None, name=None, kwargs=None):
return r
def tree(ele_or_page):
def tree(ele_or_page, text=False, show_js=False, show_css=False):
"""把页面或元素对象DOM结构打印出来
:param ele_or_page: 页面或元素对象
:param text: 是否打印文本输入数字可指定打印文本长度上线
:param show_js: 打印文本时是否包含<script>内文本text参数为False时无效
:param show_css: 打印文本时是否包含<style>内文本text参数为False时无效
:return: None
"""
@ -488,13 +491,33 @@ def tree(ele_or_page):
e = list_ele[i]
attrs = ' '.join([f"{k}='{v}'" for k, v in e.attrs.items()])
print(f'{new_body}{tail}<{e.tag} {attrs}>'.replace('\n', ' '))
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()])
print(f'<{ele.tag} {attrs}>'.replace('\n', ' '))
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)

View File

@ -73,7 +73,10 @@ def get_pdf(page: Union[ChromiumPage, ChromiumTab],
kwargs: dict = None) -> Union[bytes, str]: ...
def tree(ele_or_page: BaseParser) -> None: ...
def tree(ele_or_page: BaseParser,
text: Union[int, bool] = False,
show_js: bool = False,
show_css: bool = False) -> None: ...
def format_headers(txt: str) -> dict: ...