以下代码无效。 reportDataFields显示项目列表(对于ex abc,abd,abe),我想选择abc并放入目标。它也没有显示任何错误消息。
Actions action = new Actions(driver);
List<WebElement> reportFields = driver.findElements(By.className("reportDataFields"));
WebElement target = driver.findElement(By.id("rptDataSections"));
for (int i = 0; i < reportFields.size(); i++) {
if (reportFields.get(i).getText().equals(Section)) {
action.dragAndDrop(reportFields.get(i), target).release().build().perform();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
答案 0 :(得分:0)
我认为每次使用时都应该创建一个new
Actions
接口实例。
使用我的个性化drag and drop
功能尝试以下代码:
List<WebElement> reportFields = driver.findElements(By.className("reportDataFields"));
WebElement target = driver.findElement(By.id("rptDataSections"));
for (int i = 0; i < reportFields.size(); i++) {
if (reportFields.get(i).getText().equals(Section)) {
WebElement draggedFrom = reportFields.get(i);
new Actions(driver)
.moveToElement(draggedFrom)
.pause(Duration.ofSeconds(1))
.clickAndHold(draggedFrom)
.pause(Duration.ofSeconds(1))
.moveByOffset(1, 0)
.moveToElement(target)
.moveByOffset(1, 0)
.pause(Duration.ofSeconds(1))
.release().perform();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}