问题
So I am trying to automated a list element that can be clicked, and dragged to different portions of the ol elements, and then saved. But the test will oly go as far as to hold the element. it will not move by offset, and it will not move to the target element.
Chrome webdriver, Java/Selenium
public void clickAndDragListElement() {
Actions hold = new Actions(driver);
hold.clickAndHold(targetHoldElement)
.moveToElement(targetDestinationElement)
.release(targetHoldElement)
.build()
.perform();
}
(WebElements are defined outside the element)
回答1:
new Actions(driver)
.moveToElement(source)
.pause(Duration.ofSeconds(1))
.clickAndHold(source)
.pause(Duration.ofSeconds(1))
.moveByOffset(1, 0)
.moveToElement(destination)
.moveByOffset(1, 0)
.pause(Duration.ofSeconds(1))
.release().perform();
回答2:
Have you tried something like this:
// Create object of actions class
Actions act=new Actions(driver);
// find element which we need to drag
WebElement drag=driver.findElement(By.xpath(".//*[@id='draggable']"));
// find element which we need to drop
WebElement drop=driver.findElement(By.xpath(".//*[@id='droppable']"));
// this will drag element to destination
act.dragAndDrop(drag, drop).build().perform();
回答3:
I have tried this and it works perfectly for me:
public class DragAndDrop {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\Ranosys\\workspace\\MyTest\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
WebDriverWait wait=new WebDriverWait(driver,50 );
driver.manage().window().maximize();
driver.get("http://demo.guru99.com/test/drag_drop.html");
//Element which needs to drag.
WebElement From=driver.findElement(By.xpath("//*[@id='credit2']/a"));
//Element on which need to drop.
WebElement To=driver.findElement(By.xpath("//*[@id='bank']/li"));
//Using Action class for drag and drop.
Actions act=new Actions(driver);
//Dragged and dropped.
act.dragAndDrop(From, To).build().perform();
}
}
来源:https://stackoverflow.com/questions/48775864/click-and-drag-selenium-chrome-webdriver-is-not-dragging-but-will-click-and-h