Python Selenium ActionChains 不适用于画布

问题描述 投票:0回答:1

我正在尝试在 python 中使用 selenium 测试画布元素:在画布中,有一个自定义视频编辑器,我可以在其中拖动元素。我正在使用 Selenium ActionChains,但它似乎无法在画布中拖放不同的元素。这是代码摘录:

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()

canvas = driver.find_element(By.CSS_SELECTOR, "canvas")

# Get the position of the canvas element on the page
canvas_x, canvas_y = canvas.location["x"], canvas.location["y"]

initial_x = 0
initial_y = 180

offset_x = 0
offset_y = -180

# Perform the move and drag action
ActionChains(driver).move_to_element_with_offset(
    canvas, initial_x, initial_y
).click_and_hold().move_by_offset(offset_x, offset_y).release().perform()

在此代码中,我尝试将位于画布中心下方 180px 处的元素拖动并将其移动到中心。看起来画布根本没有检测到鼠标单击并按住,我不知道该怎么做。

python selenium-webdriver testing canvas
1个回答
0
投票

问题出在画布元素上,其属性

width
height
与页面中的实际宽度和高度不同,因为它们是响应式的。通过定位画布容器解决了这个问题,画布容器是一个具有与画布相同的实际宽度和高度的
div
元素,而不是画布元素本身。

© www.soinside.com 2019 - 2024. All rights reserved.