增加命令行命令;拖动删除shake参数;微调ActionChains逻辑

This commit is contained in:
g1879 2023-02-22 16:27:11 +08:00
parent 35daaa2712
commit 13821c0d75
5 changed files with 43 additions and 46 deletions

View File

@ -80,9 +80,7 @@ class ActionChains:
:param on_ele: ChromiumElement元素或文本定位符
:return: self
"""
if on_ele:
self.move_to(on_ele)
self._hold('left').wait(.05)._release('left')
self._hold(on_ele, 'left').wait(.05)._release('left')
return self
def r_click(self, on_ele=None):
@ -90,9 +88,7 @@ class ActionChains:
:param on_ele: ChromiumElement元素或文本定位符
:return: self
"""
if on_ele:
self.move_to(on_ele)
self._hold('right').wait(.05)._release('right')
self._hold(on_ele, 'right').wait(.05)._release('right')
return self
def m_click(self, on_ele=None):
@ -100,9 +96,7 @@ class ActionChains:
:param on_ele: ChromiumElement元素或文本定位符
:return: self
"""
if on_ele:
self.move_to(on_ele)
self._hold('middle').wait(.05)._release('middle')
self._hold(on_ele, 'middle').wait(.05)._release('middle')
return self
def hold(self, on_ele=None):
@ -110,9 +104,7 @@ class ActionChains:
:param on_ele: ChromiumElement元素或文本定位符
:return: self
"""
if on_ele:
self.move_to(on_ele)
self._hold('left')
self._hold(on_ele, 'left')
return self
def release(self, on_ele=None):
@ -130,9 +122,7 @@ class ActionChains:
:param on_ele: ChromiumElement元素或文本定位符
:return: self
"""
if on_ele:
self.move_to(on_ele)
self._hold('right')
self._hold(on_ele, 'right')
return self
def r_release(self, on_ele=None):
@ -150,9 +140,7 @@ class ActionChains:
:param on_ele: ChromiumElement元素或文本定位符
:return: self
"""
if on_ele:
self.move_to(on_ele)
self._hold('middle')
self._hold(on_ele, 'middle')
return self
def m_release(self, on_ele=None):
@ -165,11 +153,14 @@ class ActionChains:
self._release('middle')
return self
def _hold(self, button):
def _hold(self, on_ele=None, button='left'):
"""按下鼠标按键
:param on_ele: ChromiumElement元素或文本定位符
:param button: 要按下的按键
:return: self
"""
if on_ele:
self.move_to(on_ele.locations.click_point)
self._dr.Input.dispatchMouseEvent(type='mousePressed', button=button, clickCount=1,
x=self.curr_x, y=self.curr_y, modifiers=self.modifier)
return self

View File

@ -43,7 +43,7 @@ class ActionChains:
def m_release(self, on_ele: Union[ChromiumElement, str] = None) -> ActionChains: ...
def _hold(self, button: str) -> ActionChains: ...
def _hold(self, on_ele: Union[ChromiumElement, str] = None, button: str = 'left') -> ActionChains: ...
def _release(self, button: str) -> ActionChains: ...

View File

@ -528,24 +528,22 @@ class ChromiumElement(DrissionElement):
x, y = offset_scroll(self, offset_x, offset_y)
self.page.run_cdp('Input.dispatchMouseEvent', type='mouseMoved', x=x, y=y)
def drag(self, offset_x=0, offset_y=0, speed=40, shake=True):
def drag(self, offset_x=0, offset_y=0, speed=40):
"""拖拽当前元素到相对位置
:param offset_x: x变化值
:param offset_y: y变化值
:param speed: 拖动的速度传入0即瞬间到达
:param shake: 是否随机抖动
:return: None
"""
curr_x, curr_y = self.locations.midpoint
offset_x += curr_x
offset_y += curr_y
self.drag_to((offset_x, offset_y), speed, shake)
self.drag_to((offset_x, offset_y), speed)
def drag_to(self, ele_or_loc, speed=40, shake=True):
def drag_to(self, ele_or_loc, speed=40):
"""拖拽当前元素,目标为另一个元素或坐标元组
:param ele_or_loc: 另一个元素或坐标元组坐标为元素中点的坐标
:param speed: 拖动的速度传入0即瞬间到达
:param shake: 是否随机抖动
:return: None
"""
# x, y目标点坐标
@ -566,18 +564,14 @@ class ChromiumElement(DrissionElement):
points.append((target_x, target_y))
from .action_chains import ActionChains
from random import randint
actions = ActionChains(self.page)
actions.hold(self)
# 逐个访问要经过的点
for x, y in points:
if shake:
x += randint(-3, 4)
y += randint(-3, 4)
actions.move(x - current_x, y - current_y)
current_x, current_y = x, y
actions.wait(.1)
actions.wait(.05)
actions.release()
def _get_obj_id(self, node_id=None, backend_id=None):
@ -832,7 +826,7 @@ class ChromiumShadowRoot(BaseElement):
self._states = None
def __repr__(self):
return f'<ShadowRootElement in {self.parent_ele} >'
return f'<ChromiumShadowRoot in {self.parent_ele}>'
def __call__(self, loc_or_str, timeout=None):
"""在内部查找元素
@ -867,7 +861,7 @@ class ChromiumShadowRoot(BaseElement):
def states(self):
"""返回用于获取元素状态的对象"""
if self._states is None:
self._states = ShadowRootElementStates(self)
self._states = ShadowRootStates(self)
return self._states
def run_js(self, script, *args, as_expr=False):
@ -1472,7 +1466,7 @@ class ChromiumElementStates(object):
return location_in_viewport(self._ele.page, x, y) if x else False
class ShadowRootElementStates(object):
class ShadowRootStates(object):
def __init__(self, ele):
"""
:param ele: ChromiumElement

View File

@ -181,12 +181,11 @@ class ChromiumElement(DrissionElement):
def hover(self, offset_x: int = None, offset_y: int = None) -> None: ...
def drag(self, offset_x: int = 0, offset_y: int = 0, speed: int = 40, shake: bool = True) -> None: ...
def drag(self, offset_x: int = 0, offset_y: int = 0, speed: int = 40) -> None: ...
def drag_to(self,
ele_or_loc: Union[tuple, ChromiumElement],
speed: int = 40,
shake: bool = True) -> None: ...
speed: int = 40) -> None: ...
def _get_obj_id(self, node_id: str = None, backend_id: str = None) -> str: ...
@ -229,7 +228,7 @@ class ChromiumShadowRoot(BaseElement):
self._backend_id: str = ...
self.page: ChromiumPage = ...
self.parent_ele: ChromiumElement = ...
self._states: ShadowRootElementStates = ...
self._states: ShadowRootStates = ...
def __repr__(self) -> str: ...
@ -241,7 +240,7 @@ class ChromiumShadowRoot(BaseElement):
def ids(self) -> Ids: ...
@property
def states(self) -> ShadowRootElementStates: ...
def states(self) -> ShadowRootStates: ...
@property
def tag(self) -> str: ...
@ -374,7 +373,7 @@ class ChromiumElementSetter(object):
def innerHTML(self, html: str) -> None: ...
class ShadowRootElementStates(object):
class ShadowRootStates(object):
def __init__(self, ele: ChromiumShadowRoot):
"""
:param ele: ChromiumElement

View File

@ -1,14 +1,27 @@
from click import command, option
from DrissionPage.easy_set import set_paths
from DrissionPage import ChromiumPage
from DrissionPage.easy_set import set_paths, configs_to_here as ch
@command()
@option("-p", "--browser-path", help="Setting browser path.")
def main(browser_path):
"""DrissionPage CLI."""
if browser_path:
set_paths(browser_path=browser_path)
return 0
@option("-p", "--set-browser-path", help="设置浏览器路径")
@option("-u", "--set-user-path", help="设置用户数据路径")
@option("-c", "--configs-to-here", is_flag=True, help="复制默认配置文件到当前路径")
@option("-l", "--launch-browser", default=-1, help="启动浏览器传入端口号0表示用配置文件中的值")
def main(set_browser_path, set_user_path, configs_to_here, launch_browser):
if set_browser_path:
set_paths(browser_path=set_browser_path)
if set_user_path:
set_paths(user_data_path=set_user_path)
if configs_to_here:
ch()
if launch_browser >= 0:
port = f'127.0.0.1:{launch_browser}' if launch_browser else None
ChromiumPage(port)
if __name__ == '__main__':