java - 如何在 chrome 中使用 selenium 拖放 canvas web 元素

标签 java reactjs selenium selenium-webdriver selenium-chromedriver

在使用 selenium 在 chrome 的 Canvas 网络元素(头像编辑器)中移动图片时需要一些帮助。

这是 Canvas 元素的链接:https://react-avatar-editor.netlify.com

下面是我需要使用 Selenium Webdriver 执行的操作的小演示:https://www.dropbox.com/s/9pf5eeaktpgu0m7/Screen%20Recording%202019-05-03%20at%2011.48.31%20PM.mov?dl=0

请参阅下面的代码示例。

相同的代码在 Firefox 中有效,但在 Chrome 中无效。 有人知道问题出在哪里吗?我该如何解决?

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;

import java.util.concurrent.TimeUnit;

public class TestTest {

    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "pathToChromedriver");
        WebDriver wd = new ChromeDriver();
        wd.manage().window().maximize();
        wd.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
        wd.get("https://react-avatar-editor.netlify.com/");
        WebElement canvas = wd.findElement(By.cssSelector("canvas.editor-canvas"));
        Actions builder = new Actions(wd);
        Action dragAndDrop = builder
                .moveToElement(canvas, 10, 10)
                .pause(2000)
                .clickAndHold()
                .pause(2000)
                .moveToElement(canvas, 100, 100)
                .pause(2000)
                .release()
                .build();
        dragAndDrop.perform();
        wd.quit();
    }
}

最佳答案

两个变化 - 第一,为 clickAndHold() 和 release() 方法提供 webelement。 二、目标坐标改为10、100

Action 拖放 = builder.moveToElement( Canvas , 10, 10) .暂停(2000) .clickAndHold( Canvas ) .暂停(2000) .moveToElement( Canvas , 10, 100) .暂停(2000) .release( Canvas ) 。 build (); dragAndDrop.perform();

关于java - 如何在 chrome 中使用 selenium 拖放 canvas web 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55977079/

相关文章:

javascript - react 渲染/更新 Canvas

javascript - 了解异步 React 渲染

javascript - Protractor ElementFinder/ElementArrayFinder 理解

Java - 找不到符号 - 构造函数

java - 跟踪未检查(或检查?)异常丢失的位置

javascript - 从 Java 导出随机 RSA 公钥并使用 Web Crypto 将其导入 JavaScript

selenium 2 Web 驱动程序另存为对话框

java - 以 Java 8 日期为逻辑类型的 Avro

javascript - function.querySnapshot 不是函数

java - 如何使用 ChromeDriver 安全地结束程序