简体   繁体   English

chrome浏览器的Selenium Webdriver中的拖放功能无法正常工作

[英]Drag and drop functionality not working properly in selenium webdriver for chrome browser

I have written below code to drag an element and add it in workspace. 我编写了以下代码来拖动元素并将其添加到工作区中。 There is no error in console window however drap drop is not performed on chrome browser. 控制台窗口中没有错误,但是在chrome浏览器中未执行拖放操作。

WebElement dragElement = driver.findElement(By.xpath("//*[@id='sidebar-wrapper']/div/div/nginclude/div[2]/accordion/div/div[1]/div[2]/div/div/div[1]/div[2]"));
Thread.sleep(4000);
System.out.println("Element Selected to Drag");
WebElement dropElement = driver.findElement(By.xpath("//*[@id='workspace']/div/div/div/div[2]/div/div/div/div[2]/span"));
Thread.sleep(4000);

act.clickAndHold(dragElement).moveToElement(dropElement).release().build().perform();

I have tried multiple times but not able to succeed. 我已经尝试了多次,但未能成功。 Please provide your inputs 请提供您的输入

You can try give the location of the element 您可以尝试给出元素的位置

act.clickAndHold(dragElement).perform();
act.moveToElement(dropElement, dropElement.getLocation().getX(), dropElement.getLocation().getY()).perform();
act.release(dropElement).perform();

This is another method provided in the Selenium documentation here: http://www.seleniumhq.org/docs/03_webdriver.jsp#drag-and-drop . 这是Selenium文档中提供的另一种方法, 网址为http : //www.seleniumhq.org/docs/03_webdriver.jsp#drag-and-drop

WebElement dragElement = driver.findElement(By.xpath("//*[@id='sidebar-wrapper']/div/div/nginclude/div[2]/accordion/div/div[1]/div[2]/div/div/div[1]/div[2]"));

WebElement dropElement = driver.findElement(By.xpath("//*[@id='workspace']/div/div/div/div[2]/div/div/div/div[2]/span"));

(new Actions(driver)).dragAndDrop(dragElement, dropElement).perform();

You can use below code for drag and drop but I suggest you to optimize your xpath. 您可以使用以下代码进行拖放,但是我建议您优化xpath。 It might be the real problem for you. 对您来说,这可能是真正的问题。

WebElement source = driver.findElement(By.xpath("//*[@id='sidebar-wrapper']/div/div/nginclude/div[2]/accordion/div/div[1]/div[2]/div/div/div[1]/div[2]"));
Thread.sleep(4000);
System.out.println("Element Selected to Drag");
WebElement target = driver.findElement(By.xpath("//*[@id='workspace']/div/div/div/div[2]/div/div/div/div[2]/span"));
Thread.sleep(4000);
Actions builder = new Actions(driver);
Action mouseOverHome = builder.dragAndDrop(source, target).build();
mouseOverHome.perform(); 

I had a similar issue in Firefox and resolved it by adding an extra movement instruction in front of the moveToElement() instruction, like this: 我在Firefox中遇到了类似的问题,并通过在moveToElement()指令之前添加了一条额外的移动指令来解决该问题,如下所示:

private void dragAndDrop(WebElement element, WebElement target) {
    Actions builder = new Actions(driver);
    builder.clickAndHold(element);
    builder.moveByOffset(20,20);  // THIS was the critical part for me
    builder.moveToElement(target);
    builder.release();
    builder.perform();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM