首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在selenium中正确模拟拖放

如何在selenium中正确模拟拖放
EN

Stack Overflow用户
提问于 2014-12-08 16:57:18
回答 1查看 1.3K关注 0票数 2

我正在做一个可以执行拖放操作的测试。

我目前的代码:

代码语言:javascript
运行
复制
WebElement element;
By mainSelector, secondarySelector;
Actions action;

action = new Actions(driver);

mainSelector = By.cssSelector("tbody.naam tr:nth-child(1) td:nth-child(1)");
secondarySelector = By.cssSelector("tbody.bedrijf tr:nth-child(1) td:nth-child(1)");

action.click(driver.findElement(mainSelector));

action.clickAndHold(driver.findElement(mainSelector))
    .moveToElement(driver.findElement(secondarySelector), 5, 5)
    .perform();
action.release(driver.findElement(secondarySelector));

action.perform();

action.dragAndDropBy(driver.findElement(mainSelector), 300, 300).perform();

action.dragAndDrop(driver.findElement(mainSelector), driver.findElement(secondarySelector)).perform();

但是这并不能做任何事情。我已经添加了多个性能,因此请确保这不是问题所在。我添加了一个偏移量,因为我读到这有时是错误的。我使用firefox进行测试。

EN

回答 1

Stack Overflow用户

发布于 2014-12-08 17:21:34

如果想要将mainSelector拖到secondarySelector中,可以执行以下操作

Method 1

代码语言:javascript
运行
复制
mainSelector = driver.findElement(By.cssSelector("tbody.naam tr:nth-child(1) td:nth-child(1)"));
secondarySelector = driver.findElement(By.cssSelector("tbody.bedrijf tr:nth-child(1) td:nth-child(1)"));

action = new Actions(driver)
action.dragAndDrop(mainSelector, secondarySelector).perform();

Method 2

代码语言:javascript
运行
复制
action.clickAndHold(mainSelector).moveToElement(secondarySelector).release().build().perform();

这两种方法都可以完成任务。

希望它能有所帮助!:)

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27354460

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档