如何在Selenium中通过HTML5 Canvas执行鼠标滚轮滚动?

The*_*der 9 java selenium mousewheel html5-canvas selenium-webdriver

我正在研究GWT应用程序(类似于Paint).在这里,我有一个HTML5 Canvas,其中有一个功能,滚动鼠标滚轮上下将放大和缩小画布.

我搜索了很多,但没有找到解决方法来解决这个问题.这是做了什么:

int PosX = 0;
int PosY = 10;
JavascriptExecutor executor = (JavascriptExecutor) getDriver();
String script = "document.getElementById('frontCanvas').scrollBy("
                                + PosX + "," + PosY + ")";
executor.executeScript(script); 

WebDriverWait wait = new WebDriverWait(getDriver(), 20);
wait.until(ExpectedConditions.javaScriptThrowsNoExceptions(script));
Run Code Online (Sandbox Code Playgroud)

现在,上面的代码正在为另一个Angular应用程序工作,我在其中向上和向下滚动一个div元素(它有一个滚动条),但它在我的画布上没有工作(它没有滚动条)在GWT应用程序中.

我正在使用Selenium 3.14.0并在Chrome浏览器上运行此代码.任何人都可以建议可以采取哪些措施来解决这个问题?

Deb*_*anB 5

HTML5 画布

HTML 元素用于通过 JavaScript 动态绘制图形。元素只是图形的容器。您必须使用 JavaScript 来实际绘制图形。Canvas 有几种绘制路径、框、圆、文本和添加图像的方法。

在一般情况下,scroll鼠标滚轮 向上向下,我们可能都选择了动作类。但是根据使用 Selenium WebDriver 对 HTML5 Canvas 应用程序的自动测试,这个 API 似乎并不那么可靠。在 Firefox 中,每个mouse downmouse up、 或mouse click都出现在元素的中心。因此,上面的代码mouse move向所提供的 ( x , y )生成一个事件,然后mouse move向 Canvas 的中心生成一个事件,然后是 a mouse down, mouse up,并且click所有这些都位于 Canvas 的中心。这可能是罚款的按钮,但是行不通的画布,要能够hoverclick等在特定位置。Safari 中的情况更糟,它只产生一个异常,表明不支持鼠标移动事件。与此同时,Chrome 运行良好。

选择

一种解决方法是使用JavascriptExecutor接口使用 JavaScript 手动调度合成鼠标事件。

以叶出@ FlorentB的。回答,以滚动一个鼠标滚轮 向上向下,你可以发出鼠标悬停鼠标移动 事件顶端元素与脚本注入,您可以采用如下方案:

  • 代码块:

    package demo;
    
    import org.openqa.selenium.By;
    import org.openqa.selenium.JavascriptExecutor;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebDriverException;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.chrome.ChromeOptions;
    import org.openqa.selenium.support.ui.ExpectedConditions;
    import org.openqa.selenium.support.ui.WebDriverWait;
    
    public class Canvas {
    
        static WebDriver driver;
        public static void main(String[] args) {
    
            System.setProperty("webdriver.chrome.driver", "C:\\Utility\\BrowserDrivers\\chromedriver.exe");
            ChromeOptions options = new ChromeOptions();
            options.addArguments("start-maximized");
            options.addArguments("disable-infobars");
            options.addArguments("--disable-extensions");
            driver = new ChromeDriver(options);
            driver.get("https://www.google.co.uk/maps");
            WebElement elm = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("#scene > div.widget-scene > canvas")));
            // Mouse wheel UP or Zoom In 
            wheel_element(elm, -500, 0, 0);
            System.out.println("Mouse wheel UP or Zoom In through Wheel achieved !!!");
            // Mouse wheel DOWN or Zoom Out 
            wheel_element(elm, 120, 0, 0);
            System.out.println("Mouse wheel DOWN or Zoom Out through Wheel achieved !!!");
            System.out.println("Mouse Scroll through Wheel achieved !!!");
        }
    
        public static void wheel_element(WebElement element, int deltaY, int offsetX, int offsetY)
        {
            try{
                  String script = "var element = arguments[0];"
                    +"var deltaY = arguments[1];"
                    +"var box = element.getBoundingClientRect();"
                    +"var clientX = box.left + (arguments[2] || box.width / 2);"
                    +"var clientY = box.top + (arguments[3] || box.height / 2);"
                    +"var target = element.ownerDocument.elementFromPoint(clientX, clientY);"
                    +"for (var e = target; e; e = e.parentElement) {"
                      +"if (e === element) {"
                    +"target.dispatchEvent(new MouseEvent('mouseover', {view: window, bubbles: true, cancelable: true, clientX: clientX, clientY: clientY}));"
                    +"target.dispatchEvent(new MouseEvent('mousemove', {view: window, bubbles: true, cancelable: true, clientX: clientX, clientY: clientY}));"
                    +"target.dispatchEvent(new WheelEvent('wheel',     {view: window, bubbles: true, cancelable: true, clientX: clientX, clientY: clientY, deltaY: deltaY}));"
                    +"return;"
                      +"}"
                    +"}";  
    
                  WebElement parent = (WebElement) ((JavascriptExecutor) driver).executeScript("return arguments[0].parentNode;", element);
                  ((JavascriptExecutor) driver).executeScript(script, parent, deltaY, offsetX, offsetY);
                }catch(WebDriverException e)
                {
                System.out.println("Exception caught in Catch block");
                }
        }
    
    }
    
    Run Code Online (Sandbox Code Playgroud)
  • 控制台输出:

    Mouse wheel UP or Zoom In through Wheel achieved !!!
    Mouse wheel DOWN or Zoom Out through Wheel achieved !!!
    Mouse Scroll through Wheel achieved !!!
    
    Run Code Online (Sandbox Code Playgroud)

参考

您可以在以下位置找到一些相关的详细讨论:


Nea*_*u V 0

这将使用 JS 滚动当前页面中的特定内容

JavascriptExecutor executor = (JavascriptExecutor) getDriver();
executor.executeScript("window.scrollBy(" + start + "," + end + ")");
Run Code Online (Sandbox Code Playgroud)

否则你可以滚动直到找到 WebElement 示例:

WebElement x;
JavascriptExecutor executor = (JavascriptExecutor) getDriver();
getJs().executeScript("arguments[0].scrollIntoView();", x);
Run Code Online (Sandbox Code Playgroud)

谢谢,