Mouse Hover Action in Selenium:
What is Mouse Hover?
- Mouse hover, which is called simply hover, triggers an event when a user assigns a mouse to the specified location, such as the action taken to move the mouse over a hyperlink item on a web page, such as a pop-up window or a description box Causes.
- The stroller can be coded into web pages using DHTML, Javascript and other methods. Typically, if the function uses javascript then it is called mouseover.
Mouse Hover in Selenium:
- Mouse events such as hovering, clicking on any element or main menu of the web page, and simulating the mouse action / movements is not difficult in the webdriver.
- Most of the tasks can be done directly in the webdriver here, I am going to discuss some of them.
- We use the User Interaction API constructor action with the MoveToElement method to perform the task of monitoring the movements made by mouse events.
Mouse Hover using Action Class:
- In mouser action, we use Actions(driver), object.moveToElement(), build(). and perform() to hover on any WebElement.
- Action class is used to perform multiple keyboard operation and mouse hover operations.
- The build() method is used to compile all the listed actions into a single step to perform any task.
Steps to Handle Mouse Hover Action & Click on Element:
- Create object of Actions class to Handle Mouse Hover Action.
- Move cursor on Mouse Hover WebElement.
- Then click on Hover Element.
Mouse Hover Action in Selenium:
1 2 3 4 5 6 7 8 | // Create object of Action class Actions action = new Actions(driver); // Find element using locator and store into WebElement WebElement element = driver.findElement(By.id("elementId")); // Perform moveToElement operation using action (object) on element. action.moveToElement(element).perform(); |
Mouse Hover and Click on WebElement:
1 2 3 4 5 6 7 8 9 10 11 | // Find element using locator and store into WebElement WebElement elementToHover = driver.findElement(By.id("elementToHover ")); // Find element using locator and store into WebElement WebElement elementToClick = driver.findElement(By.id("elementToClick ")); // Create object of Action class Actions action = new Actions(driver); // Perform moveToElement operation using action (object) on element. action.moveToElement(elementToHover).click(elementToClick).build().perform(); |
Example of Mouse Hover and Perform Click operation:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.interactions.Actions; public class Mouse_Hover { public static void main(String[] args) { System.setProperty("webdriver.gecko.driver","D:\\geckodriver.exe"); WebDriver driver = new FirefoxDriver(); driver.get("https://www.stqatools.com"); // Find element using locator and store into WebElement WebElement elementToHover = driver.findElement(By.id("elementToHover ")); // Find element using locator and store into WebElement WebElement elementToClick = driver.findElement(By.id("elementToClick ")); // Create object of Action class Actions action = new Actions(driver); // Perform moveToElement operation using action (object) on element. action.moveToElement(elementToHover).click(elementToClick).build().perform(); } } |
Mouse Hover Actions Using Actions Class In Selenium and How to mouse hover and click on element in webdriver