Mousehover in Selenium WebDriver
Hi Friends, today we shall learn how to perform mousehover over an element using Selenium Webdriver with Java. For performing the mouse hover over an element in Selenium, we make use of the Actions class. The Actions class provided by Selenium Webdriver is used to generate complex user gestures including mouseover, right click, double click, drag and drop etc.
Mouse Hover using Actions Class:
Actions action = new Actions(driver); WebElement element = driver.findElement(By.id("elementId")); action.moveToElement(element).perform();
Here, we are instantiating an object of Actions class. After that, we pass the WebElement to be mouse hovered as parameter to the moveToElement() method present in the Actions class. Then, we will call the perform() method to perform the generated action.
Sample code to mouse hover over an element
package learnSelenium; 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 MouseOver { public static void main(String[] args) throws InterruptedException{ WebDriver driver = new FirefoxDriver(); //Launching sample website driver.get("http:www.facebook.com"); driver.manage().window().maximize(); //Mouseover on submit button Actions action = new Actions(driver); WebElement ele = driver.findElement(By.id("Log in")); action.moveToElement(ele).perform(); //Thread.sleep just for user to notice the event Thread.sleep(5000); //Closing the driver instance driver.quit(); } }
No comments:
Post a Comment