mirror of
https://gitee.com/g1879/DrissionPage.git
synced 2024-12-10 04:00:23 +08:00
元素对象添加stats.is_whole_in_viewport属性;修复元素截图视口外空白问题
This commit is contained in:
parent
ca847a4c58
commit
640059a445
@ -19,7 +19,7 @@ from .chromium_element import ChromiumScroll, ChromiumElement, run_js, make_chro
|
|||||||
from .commons.constants import HANDLE_ALERT_METHOD, ERROR, NoneElement
|
from .commons.constants import HANDLE_ALERT_METHOD, ERROR, NoneElement
|
||||||
from .commons.locator import get_loc
|
from .commons.locator import get_loc
|
||||||
from .commons.tools import get_usable_path, clean_folder
|
from .commons.tools import get_usable_path, clean_folder
|
||||||
from .commons.web import set_browser_cookies, ResponseData
|
from .commons.web import set_browser_cookies, ResponseData, location_in_viewport
|
||||||
from .errors import ContextLossError, ElementLossError, AlertExistsError, CallMethodError, TabClosedError, \
|
from .errors import ContextLossError, ElementLossError, AlertExistsError, CallMethodError, TabClosedError, \
|
||||||
NoRectError, BrowserConnectError
|
NoRectError, BrowserConnectError
|
||||||
from .session_element import make_session_ele
|
from .session_element import make_session_ele
|
||||||
@ -823,7 +823,7 @@ class ChromiumBase(BasePage):
|
|||||||
pic_type = 'png'
|
pic_type = 'png'
|
||||||
else:
|
else:
|
||||||
if as_bytes not in ('jpg', 'jpeg', 'png', 'webp'):
|
if as_bytes not in ('jpg', 'jpeg', 'png', 'webp'):
|
||||||
raise ValueError("只能接收 'jpg', 'jpeg', 'png', 'webp' 四种格式。")
|
raise TypeError("只能接收 'jpg', 'jpeg', 'png', 'webp' 四种格式。")
|
||||||
pic_type = 'jpeg' if as_bytes == 'jpg' else as_bytes
|
pic_type = 'jpeg' if as_bytes == 'jpg' else as_bytes
|
||||||
|
|
||||||
elif as_base64:
|
elif as_base64:
|
||||||
@ -831,7 +831,7 @@ class ChromiumBase(BasePage):
|
|||||||
pic_type = 'png'
|
pic_type = 'png'
|
||||||
else:
|
else:
|
||||||
if as_base64 not in ('jpg', 'jpeg', 'png', 'webp'):
|
if as_base64 not in ('jpg', 'jpeg', 'png', 'webp'):
|
||||||
raise ValueError("只能接收 'jpg', 'jpeg', 'png', 'webp' 四种格式。")
|
raise TypeError("只能接收 'jpg', 'jpeg', 'png', 'webp' 四种格式。")
|
||||||
pic_type = 'jpeg' if as_base64 == 'jpg' else as_base64
|
pic_type = 'jpeg' if as_base64 == 'jpg' else as_base64
|
||||||
|
|
||||||
else:
|
else:
|
||||||
@ -853,9 +853,21 @@ class ChromiumBase(BasePage):
|
|||||||
x, y = left_top
|
x, y = left_top
|
||||||
w = right_bottom[0] - x
|
w = right_bottom[0] - x
|
||||||
h = right_bottom[1] - y
|
h = right_bottom[1] - y
|
||||||
|
v = not (location_in_viewport(self, x, y) and
|
||||||
|
location_in_viewport(self, right_bottom[0], right_bottom[1]))
|
||||||
|
|
||||||
|
if v:
|
||||||
|
shu = self.run_js('return document.body.scrollHeight > window.innerHeight;')
|
||||||
|
heng = self.run_js('return document.body.scrollWidth > window.innerWidth;')
|
||||||
|
if shu and not heng:
|
||||||
|
x += 10
|
||||||
|
# elif heng and not shu:
|
||||||
|
# y += 5
|
||||||
|
|
||||||
vp = {'x': x, 'y': y, 'width': w, 'height': h, 'scale': 1}
|
vp = {'x': x, 'y': y, 'width': w, 'height': h, 'scale': 1}
|
||||||
png = self.run_cdp_loaded('Page.captureScreenshot', format=pic_type,
|
png = self.run_cdp_loaded('Page.captureScreenshot', format=pic_type,
|
||||||
captureBeyondViewport=False, clip=vp)['data']
|
captureBeyondViewport=v, clip=vp)['data']
|
||||||
|
|
||||||
else:
|
else:
|
||||||
png = self.run_cdp_loaded('Page.captureScreenshot', format=pic_type)['data']
|
png = self.run_cdp_loaded('Page.captureScreenshot', format=pic_type)['data']
|
||||||
|
|
||||||
|
@ -515,6 +515,7 @@ class ChromiumElement(DrissionElement):
|
|||||||
while not self.run_js(js) and perf_counter() < end_time:
|
while not self.run_js(js) and perf_counter() < end_time:
|
||||||
sleep(.1)
|
sleep(.1)
|
||||||
|
|
||||||
|
self.scroll.to_see(True)
|
||||||
left, top = self.location
|
left, top = self.location
|
||||||
width, height = self.size
|
width, height = self.size
|
||||||
left_top = (left, top)
|
left_top = (left, top)
|
||||||
@ -1420,6 +1421,14 @@ class ChromiumElementStates(object):
|
|||||||
x, y = self._ele.locations.click_point
|
x, y = self._ele.locations.click_point
|
||||||
return location_in_viewport(self._ele.page, x, y) if x else False
|
return location_in_viewport(self._ele.page, x, y) if x else False
|
||||||
|
|
||||||
|
@property
|
||||||
|
def is_whole_in_viewport(self):
|
||||||
|
"""返回元素是否整个都在视口内"""
|
||||||
|
x1, y1 = self._ele.location
|
||||||
|
w, h = self._ele.size
|
||||||
|
x2, y2 = x1 + w, y1 + h
|
||||||
|
return location_in_viewport(self._ele.page, x1, y1) and location_in_viewport(self._ele.page, x2, y2)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_covered(self):
|
def is_covered(self):
|
||||||
"""返回元素是否被覆盖,与是否在视口中无关"""
|
"""返回元素是否被覆盖,与是否在视口中无关"""
|
||||||
|
@ -230,6 +230,9 @@ class ChromiumElementStates(object):
|
|||||||
@property
|
@property
|
||||||
def is_in_viewport(self) -> bool: ...
|
def is_in_viewport(self) -> bool: ...
|
||||||
|
|
||||||
|
@property
|
||||||
|
def is_whole_in_viewport(self) -> bool: ...
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_covered(self) -> bool: ...
|
def is_covered(self) -> bool: ...
|
||||||
|
|
||||||
|
@ -180,7 +180,7 @@ def location_in_viewport(page, loc_x, loc_y):
|
|||||||
:param page: ChromePage对象
|
:param page: ChromePage对象
|
||||||
:param loc_x: 页面绝对坐标x
|
:param loc_x: 页面绝对坐标x
|
||||||
:param loc_y: 页面绝对坐标y
|
:param loc_y: 页面绝对坐标y
|
||||||
:return:
|
:return: bool
|
||||||
"""
|
"""
|
||||||
js = f'''function(){{var x = {loc_x}; var y = {loc_y};
|
js = f'''function(){{var x = {loc_x}; var y = {loc_y};
|
||||||
const scrollLeft = document.documentElement.scrollLeft;
|
const scrollLeft = document.documentElement.scrollLeft;
|
||||||
|
@ -81,7 +81,7 @@ def get_ele_txt(e: DrissionElement) -> str: ...
|
|||||||
def format_html(text: str) -> str: ...
|
def format_html(text: str) -> str: ...
|
||||||
|
|
||||||
|
|
||||||
def location_in_viewport(page, loc_x: int, loc_y: int) -> bool: ...
|
def location_in_viewport(page: ChromiumBase, loc_x: int, loc_y: int) -> bool: ...
|
||||||
|
|
||||||
|
|
||||||
def offset_scroll(ele: ChromiumElement, offset_x: int, offset_y: int) -> tuple: ...
|
def offset_scroll(ele: ChromiumElement, offset_x: int, offset_y: int) -> tuple: ...
|
||||||
|
Loading…
x
Reference in New Issue
Block a user