1.click_an_hold(self, on_element=None) 源码
这里首先说ActionChains类中的一个方法click_and_hold(self, on_element=None) 贴:click_an_hold(self, on_element=None) 源码
def click_and_hold(self, on_element=None):
"""
Holds down the left mouse button on an element.
:Args:
- on_element: The element to mouse down.
If None, clicks on current mouse position.
"""
if on_element:
self.move_to_element(on_element)
if self._driver.w3c:
self.w3c_actions.pointer_action.click_and_hold()
self.w3c_actions.key_action.pause()
else:
self._actions.append(lambda: self._driver.execute(
Command.MOUSE_DOWN, {}))
return self
2.需求:
现有一个canvas元素,要在canvas元素的第一点point1(50, 100)与第二点point2(200, 200)相对偏移之间绘制一条线段。如下图:
3.实现步骤:
1.封装自己的click_and_hold_with_offset()方法
方法:套用一下click_and_hold()的壳子,将一下代码放在click_and_hold()方法后面,直接修改click_and_hold()方法也是可以的。
def click_and_hold_with_offset(self, on_element=None, xoffset=None, yoffset=None):
"""
Holds down the left mouse button on an element.
:Args:
- on_element: The element to mouse down.
If None, clicks on current mouse position.
"""
if on_element:
if xoffset and yoffset:
self.move_to_element_with_offset(on_element, xoffset, yoffset)
else:
self.move_to_element(on_element)
if self._driver.w3c:
self.w3c_actions.pointer_action.click_and_hold()
self.w3c_actions.key_action.pause()
else:
self._actions.append(lambda: self._driver.execute(
Command.MOUSE_DOWN, {}))
return self
2.使用默认的click_and_hold()方法绘制线段看一下情况(错误操作或不是想要的效果):
源码:直接复制粘贴看情况
from selenium import webdriver
from selenium.webdriver import ActionChains
from time import sleep
def drag_canvas(webdriver,beginX,beginY,endX,endY):
canvas = webdriver.find_element_by_id("mycanvas-9")
drawing = ActionChains(webdriver)\
.click_and_hold(canvas)\
.move_to_element_with_offset(canvas, beginX, beginY)\
.move_by_offset(endX, endY)\
.release()
drawing.perform()
driver = webdriver.Chrome()
driver.get("https://draw.yunser.com/")
driver.maximize_window()
sleep(2)
driver.implicitly_wait(10)
drag_canvas(driver, 50, 100, 200, 200)
sleep(10)
driver.quit()
效果图: what ??? 怎么有3个点两条线 ? ? ?
解释一下为什么要封装自己的click_and_hold_with_offset(self, on_element=None, xoffset=None, yoffset=None)方法,因为ActionChains类中的默认方法click_and_hold(self, on_element=None)仅只有一个参数on_element无法设置鼠标的第一个点击点位置,默认是击点为元素的中心,所以在使用click_and_hold(canvas)该语句的时候会在元素canvas的中心点点击左键。
3.使用自己封装的click_and_hold_with_offset(self, on_element=None, xoffset=None, yoffset=None)方法绘制线段看一下情况:
源码:直接复制粘贴看情况
from selenium import webdriver
from selenium.webdriver import ActionChains
from time import sleep
def drag_canvas(webdriver,beginX,beginY,endX,endY):
canvas = webdriver.find_element_by_id("mycanvas-9")
drawing = ActionChains(webdriver)\
.click_and_hold_with_offset(canvas, beginX, beginY)\
.move_by_offset(endX, endY)\
.release()
drawing.perform()
driver = webdriver.Chrome()
driver.get("https://draw.yunser.com/")
driver.maximize_window()
sleep(2)
driver.implicitly_wait(10)
drag_canvas(driver, 50, 100, 200, 200)
sleep(10)
driver.quit()
效果图:
此程序与需求图绘制效果相同,结果验证封装的click_and_hold_with_offset(self, on_element=None, xoffset=None, yoffset=None)方法适用于该需求所有要的效果。
|