mirror of
https://gitee.com/g1879/DrissionPage.git
synced 2024-12-10 04:00:23 +08:00
添加拖拽函数
This commit is contained in:
parent
2834cc8ca3
commit
106e6a4ce5
@ -25,6 +25,7 @@ class DriverElement(DrissionElement):
|
|||||||
def __init__(self, ele: WebElement, timeout: float = 10):
|
def __init__(self, ele: WebElement, timeout: float = 10):
|
||||||
super().__init__(ele)
|
super().__init__(ele)
|
||||||
self.timeout = timeout
|
self.timeout = timeout
|
||||||
|
self._driver = ele.parent
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
attrs = [f"{attr}='{self.attrs[attr]}'" for attr in self.attrs]
|
attrs = [f"{attr}='{self.attrs[attr]}'" for attr in self.attrs]
|
||||||
@ -120,7 +121,7 @@ class DriverElement(DrissionElement):
|
|||||||
try:
|
try:
|
||||||
self.inner_ele.click()
|
self.inner_ele.click()
|
||||||
return True
|
return True
|
||||||
except Exception as e:
|
except:
|
||||||
# print(e)
|
# print(e)
|
||||||
sleep(0.2)
|
sleep(0.2)
|
||||||
# 若点击失败,用js方式点击
|
# 若点击失败,用js方式点击
|
||||||
@ -218,6 +219,29 @@ class DriverElement(DrissionElement):
|
|||||||
except:
|
except:
|
||||||
raise
|
raise
|
||||||
|
|
||||||
|
def drag_to(self, ele_or_loc: Union[tuple, WebElement, DrissionElement]) -> bool:
|
||||||
|
"""拖拽当前元素,目标为另一个元素或坐标元组
|
||||||
|
:param ele_or_loc: 另一个元素或相对当前位置
|
||||||
|
:return: 是否拖拽成功
|
||||||
|
"""
|
||||||
|
from selenium.webdriver import ActionChains
|
||||||
|
loc1 = self.location
|
||||||
|
actions = ActionChains(self._driver)
|
||||||
|
|
||||||
|
if isinstance(ele_or_loc, DriverElement):
|
||||||
|
actions.drag_and_drop(self.inner_ele, ele_or_loc.inner_ele)
|
||||||
|
elif isinstance(ele_or_loc, WebElement):
|
||||||
|
actions.drag_and_drop(self.inner_ele, ele_or_loc)
|
||||||
|
elif isinstance(ele_or_loc, tuple):
|
||||||
|
actions.drag_and_drop_by_offset(self.inner_ele, ele_or_loc[0], ele_or_loc[1])
|
||||||
|
else:
|
||||||
|
raise KeyError('Need WebElement object or coordinate information.')
|
||||||
|
actions.perform()
|
||||||
|
loc2 = self.location
|
||||||
|
if loc1 == loc2:
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
def execute_driver_find(page_or_ele: Union[WebElement, WebDriver], loc: tuple, mode: str = 'single',
|
def execute_driver_find(page_or_ele: Union[WebElement, WebDriver], loc: tuple, mode: str = 'single',
|
||||||
show_errmsg: bool = False, timeout: float = 10) -> Union[DriverElement, List[DriverElement]]:
|
show_errmsg: bool = False, timeout: float = 10) -> Union[DriverElement, List[DriverElement]]:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user