selenium-webdriver Actions (Emulating complex user gestures) Move to Element


Example

C#

Suppose you want to test that when you hover over an element, a drop list is displayed. You may want to check the contents of this list, or perhaps select an option from the list.

First create an Action, to hover over the element (e.g. my element has link text "Admin"):

Actions mouseHover = new Actions(driver);
mouseHover.MoveToElement(driver.FindElement(By.LinkText("Admin"))).Perform();

In the example above:

  • You have created the action mouseHover
  • You have told driver to move to a specific element
  • From here you can perform other Actions with the mouseHover object or continue testing with your driver object

This approach is of particular use when clicking on an element performs a different function than hovering over it.

A full example:

Actions mouseHover = new Actions(driver);
mouseHover.MoveToElement(driver.FindElement(By.LinkText("Admin"))).Perform();

Assert.IsTrue(driver.FindElement(By.LinkText("Edit Record")).Displayed);
Assert.IsTrue(driver.FindElement(By.LinkText("Delete Record")).Displayed);