我使用下面的代码使用webdriver拖放动作
driver.get("https://www.w3schools.com/html/tryit.asp?filename=tryhtml5_draganddrop");
driver.switchTo().frame("iframeResult");
Actions act = new Actions(driver);
act.dragAndDrop(driver.findElement(By.xpath("html/body/img")),driver.findElement(By.xpath("html/body/div[1]"))).perform();
它正在拖动元素但不将其放在目标
中答案 0 :(得分:0)
尝试以下解决方案:
WebElement From = driver.findElement(By.xpath(".//*[@id='drag1']"));
WebElement To = driver.findElement(By.xpath(".//*[@id='div1']"));
Actions builder = new Actions(driver);
Action dragAndDrop = builder.clickAndHold(From).moveToElement(To).release(To).build();
dragAndDrop.perform();
希望它会对你有所帮助。
答案 1 :(得分:0)
driver.get("http://jqueryui.com/droppable/");
driver.switchTo().frame(0);
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
WebElement dragElement=driver.findElement(By.id("draggable"));
WebElement dropElement=driver.findElement(By.id("droppable"));
Actions builder = new Actions(driver); // Configure the Action
Action dragAndDrop = builder.clickAndHold(dragElement)
.moveToElement(dropElement)
.release(dropElement)
.build(); // Get the action
dragAndDrop.perform(); // Execute the Action
试试这段代码我希望它能帮到你
答案 2 :(得分:0)
根据RNS答案 - > WebElements必须为final
。
final WebElement From = driver.findElement(By.xpath(".//*[@id='drag1']"));
final WebElement To = driver.findElement(By.xpath(".//*[@id='div1']"));
Actions builder = new Actions(driver);
Action dragAndDrop = builder.clickAndHold(From).moveToElement(To).release(To).build();
dragAndDrop.perform();
或使用Serenity
final WebElement From = driver.findElement(By.xpath(".//*[@id='drag1']"));
final WebElement To = driver.findElement(By.xpath(".//*[@id='div1']"));
withAction().dragAndDrop(From, To).build().perform();