Pre Merge pull request !24 from g1879/debug

This commit is contained in:
g1879 2023-09-22 10:03:58 +00:00 committed by Gitee
commit 2f4adb467c
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
7 changed files with 29 additions and 13 deletions

View File

@ -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.locator import get_loc
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, \
NoRectError, BrowserConnectError
from .session_element import make_session_ele
@ -823,7 +823,7 @@ class ChromiumBase(BasePage):
pic_type = 'png'
else:
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
elif as_base64:
@ -831,7 +831,7 @@ class ChromiumBase(BasePage):
pic_type = 'png'
else:
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
else:
@ -853,9 +853,20 @@ class ChromiumBase(BasePage):
x, y = left_top
w = right_bottom[0] - x
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:
if (self.run_js('return document.body.scrollHeight > window.innerHeight;') and
not self.run_js('return document.body.scrollWidth > window.innerWidth;')):
x += 10
# elif heng and not shu:
# y += 5
vp = {'x': x, 'y': y, 'width': w, 'height': h, 'scale': 1}
png = self.run_cdp_loaded('Page.captureScreenshot', format=pic_type,
captureBeyondViewport=False, clip=vp)['data']
captureBeyondViewport=v, clip=vp)['data']
else:
png = self.run_cdp_loaded('Page.captureScreenshot', format=pic_type)['data']

View File

@ -515,6 +515,7 @@ class ChromiumElement(DrissionElement):
while not self.run_js(js) and perf_counter() < end_time:
sleep(.1)
self.scroll.to_see(True)
left, top = self.location
width, height = self.size
left_top = (left, top)

View File

@ -485,7 +485,7 @@ class ChromiumFrame(ChromiumBase):
pic_type = 'png'
else:
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
elif as_base64:
@ -493,7 +493,7 @@ class ChromiumFrame(ChromiumBase):
pic_type = 'png'
else:
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
else:
@ -522,11 +522,16 @@ class ChromiumFrame(ChromiumBase):
arguments[0].insertBefore(img, this);
return img;'''
new_ele = first_child.run_js(js, body)
new_ele.scroll.to_see(True)
new_ele.scroll.to_see(center=True)
top = int(self.frame_ele.style('border-top').split('px')[0])
left = int(self.frame_ele.style('border-left').split('px')[0])
r = self.page.run_cdp('Page.getLayoutMetrics')['visualViewport']
sx = r['pageX']
sy = r['pageY']
r = self.page.get_screenshot(path=path, as_bytes=as_bytes, as_base64=as_base64,
left_top=(cx + left, cy + top), right_bottom=(cx + w + left, cy + h + top))
left_top=(cx + left + sx, cy + top + sy),
right_bottom=(cx + w + left + sx, cy + h + top + sy))
self.page.remove_ele(new_ele)
return r

View File

@ -17,7 +17,6 @@ class ChromiumFrame(ChromiumBase):
self.frame_id: str = ...
self._frame_ele: ChromiumElement = ...
self._backend_id: str = ...
self.frame_page: ChromiumBase = ...
self._doc_ele: ChromiumElement = ...
self._is_diff_domain: bool = ...
self.doc_ele: ChromiumElement = ...

View File

@ -180,7 +180,7 @@ def location_in_viewport(page, loc_x, loc_y):
:param page: ChromePage对象
:param loc_x: 页面绝对坐标x
:param loc_y: 页面绝对坐标y
:return:
:return: bool
"""
js = f'''function(){{var x = {loc_x}; var y = {loc_y};
const scrollLeft = document.documentElement.scrollLeft;

View File

@ -63,7 +63,7 @@ class ResponseData(object):
def requestHeaders(self) -> Union[CaseInsensitiveDict, None]: ...
@requestHeaders.setter
def requestHeaders(self, val:dict) -> None: ...
def requestHeaders(self, val: dict) -> None: ...
@property
def postData(self) -> Union[dict, str, None]: ...
@ -81,7 +81,7 @@ def get_ele_txt(e: DrissionElement) -> 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: ...

View File

@ -6,7 +6,7 @@ with open("README.md", "r", encoding='utf-8') as fh:
setup(
name="DrissionPage",
version="3.2.32",
version="3.2.33",
author="g1879",
author_email="g1879@qq.com",
description="Python based web automation tool. It can control the browser and send and receive data packets.",