硒拖放方法不适用于Chrome浏览器

时间:2019-07-05 11:01:12

标签: selenium-webdriver

从Selenium IDE导出的

功能齐全的拖放方法无法在webDriver上运行。

webDriver上的测试通过,但未执行任何操作。 chrome的版本为75.0.3770.100。 selenium webDriver的版本是3.141.59。 Selenium IDE是chrome扩展。该应用程序是HTML5 reactJS

    public void dragAndDrop(By by1, By by2) {
            WebElement from = driver.findElement(by1);
            WebElement to = driver.findElement(by2);
            Actions actions = new Actions(driver);
           actions.dragAndDrop(from,to).perform();
        }

实现-

    dragAndDrop(By.cssSelector(".py-1:nth-child(3)"), By.cssSelector(".jss11amios > div"));

1 个答案:

答案 0 :(得分:0)

为此找到了解决方案-

 public void dragAndDrop(By by1, By by2) {
        WebElement From = driver.findElement(by1);
        WebElement To = driver.findElement(by2);
        JavascriptExecutor js = (JavascriptExecutor) driver;
        js.executeScript("function createEvent(typeOfEvent) " +
                "{var event =document.createEvent(\"CustomEvent\");" +
                "event.initCustomEvent(typeOfEvent,true, true, null);" +
                "event.dataTransfer = {data: {},setData: function (key, value) {this.data[key] = value;}," +
                "getData: function (key) {return this.data[key];}};" +
                "return event;}" +
                "function dispatchEvent(element, event,transferData) {if (transferData !== undefined)" +
                " {event.dataTransfer = transferData;}" +
                "if (element.dispatchEvent) {element.dispatchEvent(event);} " +
                "else if (element.fireEvent) {element.fireEvent(\"on\" + event.type, event);}}" +
                "function simulateHTML5DragAndDrop(element, destination) " +
                "{" +
                "var dragStartEvent =createEvent('dragstart');" +
                "dispatchEvent(element, dragStartEvent);" +
                "var dropEvent = createEvent('drop');" +
                "dispatchEvent(destination, dropEvent,dragStartEvent.dataTransfer);" +
                "var dragEndEvent = createEvent('dragend');" +
                "dispatchEvent(element, dragEndEvent,dropEvent.dataTransfer);" +
                "}" +
                "var source = arguments[0];" +
                "var destination = arguments[1];" +
                "simulateHTML5DragAndDrop(source,destination);", From, To);
    }