Learn Selenium
上QQ阅读APP看书,第一时间看更新

The release on another WebElement action 

This is an overloaded version of the release() method. Using this, you can actually release the currently held WebElement in the middle of another WebElement. In this way, we don't have to calculate the offset of the target WebElement from the held WebElement.

The API syntax is as follows:

public Actions release(WebElement onElement)

The input parameter for the preceding method is obviously the target WebElement, where the held WebElement should be dropped. The return type is the instance of the Actions class.

Let's modify the preceding code example to use this method:

@Test
public void shouldClickAndHoldAndReleaseOnElement() {

driver.get("http://guidebook.seleniumacademy.com/Sortable.html");

WebElement three = driver.findElement(By.name("three"));
WebElement two = driver.findElement(By.name("two"));
Actions actions = new Actions(driver);

//Move tile3 to the position of tile2
actions.clickAndHold(three)
.release(two)
.perform();
}

Look at how simple the preceding code is. We have removed all the moveByOffset code and added the release() method that takes the WebElement with the name two as the input parameter.

Invoking the release() or release(WebElement) methods without calling the clickAndHold() method will result in an undefined behavior.