修复文本生成的静态元素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:
from psutil import Process
for pid in pids:
Process(pid).kill()
try:
Process(pid).kill()
except:
pass
else:
try:
self.run_cdp('Browser.close')

View File

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

View File

@ -200,12 +200,12 @@ class SessionElement(DrissionElement):
# 若为链接为None、js或邮件直接返回
if not link or link.lower().startswith(('javascript:', 'mailto:')):
return link
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':
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':
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 = 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):
page = html_or_ele

View File

@ -162,7 +162,7 @@ class ChromiumBase(BasePage):
try:
b_id = self.run_cdp('DOM.getDocument', _timeout=timeout)['root']['backendNodeId']
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,
_timeout=timeout)['object']['objectId']
result = True

View File

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

View File

@ -200,7 +200,7 @@ class SessionPage(BasePage):
:param index: 获取第几个从1开始可传入负数获取倒数第几个
: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):
"""返回页面中符合条件的所有元素、属性或节点文本

View File

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

View File

@ -62,7 +62,7 @@ class ElementRect(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] = ...
@property