如何正确模拟硒的拖放

时间:2014-12-08 08:57:18

标签: selenium selenium-webdriver

我正在进行拖放测试。

目前我的代码:

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

1 个答案:

答案 0 :(得分:-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();

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

希望它有所帮助! :)