修复文本生成的静态元素link报错问题;微调生成静态元素逻辑

This commit is contained in:
g1879 2024-03-07 17:41:50 +08:00
parent 51c8471fb5
commit 727c005f19
8 changed files with 25 additions and 18 deletions

View File

@ -213,7 +213,10 @@ class Browser(object):
if force: if force:
from psutil import Process from psutil import Process
for pid in pids: for pid in pids:
Process(pid).kill() try:
Process(pid).kill()
except:
pass
else: else:
try: try:
self.run_cdp('Browser.close') self.run_cdp('Browser.close')

View File

@ -123,7 +123,7 @@ class ChromiumElement(DrissionElement):
@property @property
def text(self): def text(self):
"""返回元素内所有文本,文本已格式化""" """返回元素内所有文本,文本已格式化"""
return get_ele_txt(make_session_ele(self.html)) return get_ele_txt(make_session_ele(self))
@property @property
def raw_text(self): def raw_text(self):
@ -447,10 +447,7 @@ class ChromiumElement(DrissionElement):
:param index: 获取第几个从1开始可传入负数获取倒数第几个 :param index: 获取第几个从1开始可传入负数获取倒数第几个
:return: SessionElement对象或属性文本 :return: SessionElement对象或属性文本
""" """
if self.tag in __FRAME_ELEMENT__: r = make_session_ele(self, locator, index=index)
r = make_session_ele(self.inner_html, locator, index=index)
else:
r = make_session_ele(self, locator, index=index)
if isinstance(r, NoneElement): if isinstance(r, NoneElement):
if Settings.raise_when_ele_not_found: if Settings.raise_when_ele_not_found:
raise ElementNotFoundError(None, 's_ele()', {'locator': locator}) raise ElementNotFoundError(None, 's_ele()', {'locator': locator})
@ -464,8 +461,6 @@ class ChromiumElement(DrissionElement):
:param locator: 定位符 :param locator: 定位符
:return: SessionElement或属性文本组成的列表 :return: SessionElement或属性文本组成的列表
""" """
if self.tag in __FRAME_ELEMENT__:
return make_session_ele(self.inner_html, locator, index=None)
return make_session_ele(self, locator, index=None) return make_session_ele(self, locator, index=None)
def _find_elements(self, locator, timeout=None, index=1, relative=False, raise_err=None): def _find_elements(self, locator, timeout=None, index=1, relative=False, raise_err=None):
@ -1080,7 +1075,7 @@ class ShadowRoot(BaseElement):
return None if r is False else r return None if r is False else r
else: else:
eles = make_session_ele(self.html).eles(loc) eles = make_session_ele(self, loc, index=None)
if not eles: if not eles:
return None return None

View File

@ -200,12 +200,12 @@ class SessionElement(DrissionElement):
# 若为链接为None、js或邮件直接返回 # 若为链接为None、js或邮件直接返回
if not link or link.lower().startswith(('javascript:', 'mailto:')): if not link or link.lower().startswith(('javascript:', 'mailto:')):
return link return link
else: # 其它情况直接返回绝对url else: # 其它情况直接返回绝对url
return make_absolute_link(link, self.owner.url) return make_absolute_link(link, self.owner.url) if self.owner else link
elif name == 'src': elif name == 'src':
return make_absolute_link(self.inner_ele.get('src'), self.owner.url) return make_absolute_link(self.inner_ele.get('src'),
self.owner.url) if self.owner else self.inner_ele.get('src')
elif name == 'text': elif name == 'text':
return self.text return self.text
@ -351,6 +351,10 @@ def make_session_ele(html_or_ele, loc=None, index=1):
html_or_ele = fromstring(html) html_or_ele = fromstring(html)
html_or_ele = html_or_ele.xpath(xpath)[0] html_or_ele = html_or_ele.xpath(xpath)[0]
elif html_or_ele._type == 'ChromiumFrame':
page = html_or_ele
html_or_ele = fromstring(html_or_ele.inner_html)
# 各种页面对象 # 各种页面对象
elif isinstance(html_or_ele, BasePage): elif isinstance(html_or_ele, BasePage):
page = html_or_ele page = html_or_ele

View File

@ -162,7 +162,7 @@ class ChromiumBase(BasePage):
try: try:
b_id = self.run_cdp('DOM.getDocument', _timeout=timeout)['root']['backendNodeId'] b_id = self.run_cdp('DOM.getDocument', _timeout=timeout)['root']['backendNodeId']
timeout = end_time - perf_counter() timeout = end_time - perf_counter()
timeout = .5 if timeout <= 0 else timeout timeout = 1 if timeout <= 1 else timeout
self._root_id = self.run_cdp('DOM.resolveNode', backendNodeId=b_id, self._root_id = self.run_cdp('DOM.resolveNode', backendNodeId=b_id,
_timeout=timeout)['object']['objectId'] _timeout=timeout)['object']['objectId']
result = True result = True

View File

@ -8,8 +8,7 @@
from pathlib import Path from pathlib import Path
from typing import Union, Tuple, List, Any, Optional, Literal from typing import Union, Tuple, List, Any, Optional, Literal
from .chromium_tab import ChromiumTab, WebPageTab from .chromium_tab import ChromiumTab
from .web_page import WebPage
from .._base.base import BasePage from .._base.base import BasePage
from .._base.browser import Browser from .._base.browser import Browser
from .._base.driver import Driver from .._base.driver import Driver

View File

@ -200,7 +200,7 @@ class SessionPage(BasePage):
:param index: 获取第几个从1开始可传入负数获取倒数第几个 :param index: 获取第几个从1开始可传入负数获取倒数第几个
:return: SessionElement对象或属性文本 :return: SessionElement对象或属性文本
""" """
return make_session_ele(self.html) if locator is None else self._ele(locator, index=index, method='s_ele()') return make_session_ele(self) if locator is None else self._ele(locator, index=index, method='s_ele()')
def s_eles(self, locator): def s_eles(self, locator):
"""返回页面中符合条件的所有元素、属性或节点文本 """返回页面中符合条件的所有元素、属性或节点文本

View File

@ -114,11 +114,14 @@ class ElementRect(object):
class TabRect(object): class TabRect(object):
def __init__(self, owner): def __init__(self, owner):
"""
:param owner: Page对象和Tab对象
"""
self._owner = owner self._owner = owner
@property @property
def window_state(self): def window_state(self):
"""返回窗口状态normal、fullscreen、maximized、 minimized""" """返回窗口状态normal、fullscreen、maximized、minimized"""
return self._get_window_rect()['windowState'] return self._get_window_rect()['windowState']
@property @property
@ -187,6 +190,9 @@ class FrameRect(object):
"""异域iframe使用""" """异域iframe使用"""
def __init__(self, frame): def __init__(self, frame):
"""
:param frame: ChromiumFrame对象
"""
self._frame = frame self._frame = frame
@property @property

View File

@ -62,7 +62,7 @@ class ElementRect(object):
class TabRect(object): class TabRect(object):
def __init__(self, owner: Union[ChromiumPage, ChromiumTab, WebPage, WebPageTab]): def __init__(self, owner: ChromiumBase):
self._owner: Union[ChromiumPage, ChromiumTab, WebPage, WebPageTab] = ... self._owner: Union[ChromiumPage, ChromiumTab, WebPage, WebPageTab] = ...
@property @property