使用 Python Selenium 绘制字符

Drawing characters using Python Selenium

我打算使用 Selenium Action 链在 https://sketchtoy.com/ canvas 中绘制字符 A B C D,

import time
from selenium.webdriver.common.action_chains import ActionChains

def draw(action, offset_list):
    for offset in offset_list:
        action.click_and_hold().move_by_offset(offset[0], offset[1]).perform()
        print(f'Moving to {offset[0]}, {offset[1]}')
        time.sleep(1)
        action.release().perform()

def main(driver):
        driver.get("https://sketchtoy.com/")
        action = ActionChains(driver)
        element = driver.find_element_by_xpath("html/body/div[1]/div[5]/div[2]/canvas")
        action.move_to_element(element).perform()
        draw(action, [(-100, -100), (100, -100), (-100, 100), (100, 100), (-100, -100)])

这是我希望它通过移动到类似于 X-Y 图的位置来绘制正方形的代码。

不过好像画的是三角形。

我不确定为什么会这样 运行。

这是一个盒子。按偏移量移动实际上是您要移动到一侧的距离。 move_by_offset(50,0) 向右移动 50。move_by_offset(0,50) 向下移动 50。您可以通过偏移移动或释放来使边缘变圆。

def main(driver):
        driver.get("https://sketchtoy.com/")
        action = ActionChains(driver)
        element = driver.find_element_by_xpath("html/body/div[1]/div[5]/div[2]/canvas")
        action.click_and_hold(element).move_by_offset(50,0).move_by_offset(5,0)
        action.move_by_offset(0,50)
        action.move_by_offset(-5,0).move_by_offset(-50,0).move_by_offset(-5,0)
        action.move_by_offset(0,-100)
        action.release()
        action.perform()

我如何移动到坐标并使用 Actions 绘图。

def draw(action, element, offset_list):
    action.click_and_hold(element)
    for offset in offset_list:
        action.move_by_offset(offset[0], offset[1])
        time.sleep(1)
    action.release().perform()

def main(driver):
    driver.get("https://sketchtoy.com/")
    action = ActionChains(driver)
    element = driver.find_element_by_xpath("html/body/div[1]/div[5]/div[2]/canvas")
    action.move_to_element(element).perform()
    draw(action, element, [(0,20), (0,5), (20, 0), (0,-5), (0,-20), (0, -5), (-20, 0), (0, 10)])
    draw(action, element, [(70, 0), (5, 0), (0, 25), (-20, 0), (0, -5)])
    draw(action, element, [(0, 60), (0, 10), (75, 0), (0, -10), (-5, 0)])