我尝试了各种选项,但无法在使用 Selenium 的浏览器中模拟鼠标单击以将元素从一个位置拖动到另一个位置。测试运行时,我看到元素被选中,但它没有移动到指定的放置点。非常感谢任何建议或见解!
这是我在最近一次尝试中定义函数的方式(这个主题的变体也尝试过但失败了):
private void dragAndDrop(WebElement dragPoint, WebElement dropPoint, WebDriver driver) {
Actions builder = new Actions(driver);
builder.clickAndHold(dragPoint).perform();
builder.pause(Duration.ofSeconds(1));
builder.moveByOffset(10,0).perform();
builder.moveToElement(dropPoint).perform();
builder.moveByOffset(10,0).perform();
builder.pause(Duration.ofSeconds(1));
builder.release();
builder.build();
builder.perform();
}
还尝试了以下(相同的结果):
private void dragAndDrop(WebElement dragPoint, WebElement dropPoint, WebDriver driver) {
Actions builder = new Actions(driver);
Action dragAndDrop = builder.dragAndDrop(dragPoint, dropPoint).build();
dragAndDrop.perform();
}
在测试中,使用 xpath 唯一标识 2 个元素并调用该函数:
WebElement dragPoint = driver.findElement(By.xpath(".../div[3]/...(etc.)/div[@class='rst__moveHandle']"));
WebElement dropPoint = driver.findElement(By.xpath(".../div[5]/...(etc.)/div[@class='rst__moveHandle']"));
dragAndDrop(dragPoint, dropPoint, driver);
相关库:
react-beautiful-dnd: https://github.com/atlassian/react-beautiful-dnd
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;