调整元素点击和位置api

This commit is contained in:
g1879 2023-02-17 10:06:58 +08:00
parent 61fc745328
commit 5b78521bbe
2 changed files with 67 additions and 40 deletions

View File

@ -144,7 +144,7 @@ class ChromiumElement(DrissionElement):
@property
def location(self):
"""返回元素左上角的绝对坐标"""
return self.locations.page_location
return self.locations.location
@property
def locations(self):
@ -671,7 +671,7 @@ class ChromiumElement(DrissionElement):
:return: None
"""
warn("click_at()方法即将弃用请用click.left_at()方法代替。", DeprecationWarning)
self.click.left_at(offset_x, offset_y, button)
self.click.left_at(offset_x, offset_y)
def r_click(self):
"""右键单击"""
@ -1512,7 +1512,7 @@ class Locations(object):
self._ele = ele
@property
def page_location(self):
def location(self):
"""返回元素左上角的绝对坐标"""
cl = self.viewport_location
return self._get_page_coord(cl[0], cl[1]) if cl else (0, 0)
@ -1523,6 +1523,12 @@ class Locations(object):
cl = self.viewport_midpoint
return self._get_page_coord(cl[0], cl[1]) if cl else (0, 0)
@property
def click_point(self):
"""返回元素接受点击的点的绝对坐标"""
cl = self.viewport_click_point
return self._get_page_coord(cl[0], cl[1]) if cl else (0, 0)
@property
def viewport_location(self):
"""返回元素左上角在视口中的坐标"""
@ -1537,23 +1543,31 @@ class Locations(object):
@property
def viewport_click_point(self):
"""返回元素左上角可接受点击的点视口坐标"""
"""返回元素接受点击的点视口坐标"""
m = self._get_viewport_rect('padding')
return (int(self.viewport_midpoint[0]), int(m[1]) + 1) if m else (0, 0)
@property
def click_point(self):
"""返回元素左上角可接受点击的点的绝对坐标"""
cl = self.viewport_click_point
return self._get_page_coord(cl[0], cl[1]) if cl else (0, 0)
@property
def screen_location(self):
"""返回元素在屏幕上坐标,左上角为(0, 0)"""
"""返回元素左上角在屏幕上坐标,左上角为(0, 0)"""
vx, vy = self._ele.page.rect.viewport_location
ex, ey = self.viewport_location
return vx + ex, ey + vy
@property
def screen_midpoint(self):
"""返回元素中点在屏幕上坐标,左上角为(0, 0)"""
vx, vy = self._ele.page.rect.viewport_location
ex, ey = self.viewport_midpoint
return vx + ex, ey + vy
@property
def screen_click_point(self):
"""返回元素中点在屏幕上坐标,左上角为(0, 0)"""
vx, vy = self._ele.page.rect.viewport_location
ex, ey = self.viewport_click_point
return vx + ex, ey + vy
def _get_viewport_rect(self, quad):
"""按照类型返回在可视窗口中的范围
:param quad: 方框类型margin border padding
@ -1561,7 +1575,7 @@ class Locations(object):
"""
try:
return self._ele.page.run_cdp('DOM.getBoxModel', nodeId=self._ele.ids.node_id)['model'][quad]
except CallMethodError:
except Exception:
return None
def _get_page_coord(self, x, y):
@ -1641,36 +1655,44 @@ class Click(object):
return False
def left_at(self, offset_x=None, offset_y=None, button='left'):
"""带偏移量点击本元素相对于左上角坐标。不传入x或y值时点击元素左上角可接受点击的点
:param offset_x: 相对元素左上角坐标的x轴偏移量
:param offset_y: 相对元素左上角坐标的y轴偏移量
:param button: 左键还是右键
:return: None
"""
self._ele.page.scroll.to_see(self._ele)
x, y = offset_scroll(self._ele, offset_x, offset_y)
self._click(x, y, button)
def right(self):
"""右键单击"""
self._ele.page.scroll.to_see(self._ele)
x, y = self._ele.locations.viewport_click_point
self._click(x, y, 'right')
def middle(self):
"""中键单击"""
self._ele.page.scroll.to_see(self._ele)
x, y = self._ele.locations.viewport_click_point
self._click(x, y, 'middle')
def left_at(self, offset_x=None, offset_y=None):
"""带偏移量点击本元素相对于左上角坐标。不传入x或y值时点击元素左上角可接受点击的点
:param offset_x: 相对元素左上角坐标的x轴偏移量
:param offset_y: 相对元素左上角坐标的y轴偏移量
:return: None
"""
self.at(offset_x, offset_y, button='left')
def right_at(self, offset_x=None, offset_y=None):
"""带偏移量右键单击本元素相对于左上角坐标。不传入x或y值时点击元素中点
:param offset_x: 相对元素左上角坐标的x轴偏移量
:param offset_y: 相对元素左上角坐标的y轴偏移量
:return: None
"""
self.left_at(offset_x, offset_y, 'right')
self.at(offset_x, offset_y, button='right')
def middle(self):
"""中键单击"""
def at(self, offset_x=None, offset_y=None, button='left'):
"""带偏移量点击本元素相对于左上角坐标。不传入x或y值时点击元素click_point
:param offset_x: 相对元素左上角坐标的x轴偏移量
:param offset_y: 相对元素左上角坐标的y轴偏移量
:param button: 点击哪个键可选 left, middle, right, back, forward
:return: None
"""
self._ele.page.scroll.to_see(self._ele)
x, y = self._ele.locations.viewport_click_point
self._click(x, y, 'middle')
x, y = offset_scroll(self._ele, offset_x, offset_y)
self._click(x, y, button)
def _click(self, client_x, client_y, button='left'):
"""实施点击

View File

@ -396,7 +396,13 @@ class Locations(object):
self._ele: ChromiumElement = ...
@property
def page_location(self) -> Tuple[int, int]: ...
def location(self) -> Tuple[int, int]: ...
@property
def midpoint(self) -> Tuple[int, int]: ...
@property
def click_point(self) -> Tuple[int, int]: ...
@property
def viewport_location(self) -> Tuple[int, int]: ...
@ -404,17 +410,17 @@ class Locations(object):
@property
def viewport_midpoint(self) -> Tuple[int, int]: ...
@property
def midpoint(self) -> Tuple[int, int]: ...
@property
def viewport_click_point(self) -> Tuple[int, int]: ...
@property
def click_point(self) -> Tuple[int, int]: ...
def screen_location(self) -> Tuple[int, int]: ...
@property
def screen_location(self) -> Tuple[int, int]: ...
def screen_midpoint(self) -> Tuple[int, int]: ...
@property
def screen_click_point(self) -> Tuple[int, int]: ...
def _get_viewport_rect(self, quad: str) -> Union[list, None]: ...
@ -431,19 +437,20 @@ class Click(object):
def left(self, by_js: bool = None, retry: bool = False, timeout: float = 0.2,
wait_loading: Union[bool, float] = 0) -> bool: ...
def left_at(self, offset_x: int = None, offset_y: int = None, button: str = 'left') -> None: ...
def right(self): ...
def middle(self): ...
def left_at(self, offset_x: int = None, offset_y: int = None) -> None: ...
def right_at(self, offset_x: int = None, offset_y: int = None) -> None: ...
def middle(self): ...
def at(self, offset_x: int = None, offset_y: int = None, button='left') -> None: ...
def _click(self, client_x: int, client_y: int, button: str = 'left') -> None: ...
class ChromiumScroll(object):
def __init__(self, page_or_ele: Union[ChromiumBase, ChromiumElement, ChromiumFrame]):
self.t1: str = ...
self.t2: str = ...
@ -473,7 +480,6 @@ class ChromiumScroll(object):
class ChromiumSelect(object):
def __init__(self, ele: ChromiumElement):
self._ele: ChromiumElement = ...
@ -532,7 +538,6 @@ class ChromiumWaiter(object):
class ChromiumElementWaiter(object):
def __init__(self,
page_or_ele: Union[ChromiumBase, ChromiumElement],
loc_or_ele: Union[str, tuple, ChromiumElement],