【问题标题】:How to properly simulate drag and drop in selenium如何正确模拟硒中的拖放
【发布时间】:2015-02-05 21:35:54
【问题描述】:

我正在做一个执行拖放的测试。

我目前的代码:

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进行测试。

【问题讨论】:

标签: selenium selenium-webdriver


【解决方案1】:

如果你想把mainSelector拖到secondarySelector,你可以这样做

Method 1

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

action.clickAndHold(mainSelector).moveToElement(secondarySelector).release().build().perform();

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

希望对您有所帮助! :)

【讨论】:

  • dragAndDrop 函数只接受 WebElements 对吗?不是By 选择器?
  • 我已将它添加到尝试列表中,但很遗憾,它对我不起作用。
  • @Funonly :你能展示你的完整实现吗?
  • @Rupesh 你的意思是javascript?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-06-05
  • 2018-09-21
  • 2010-11-13
  • 1970-01-01
  • 1970-01-01
  • 2018-04-26
  • 1970-01-01
相关资源
最近更新 更多