Saturday, September 14, 2019

Post#61.Mouse Hover in Selenium


Mouse Hovering:
The mouse hover can be implemented using Actions Class in Selenium.
Consider you want to perform mouse over action on the ABOUT YSS menu as per given in below screenshot.
Website Link: https://yssofindia.org/


·         Mouse Hover Sequence: ABOUT YSS->>Contact Us->>Email Us->>Click On Email Us Option

Script:
package pack2;
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 MouseHover {

public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver", "C:/path/geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("http://www.yssofindia.org/");
WebElement aboutUs = driver.findElement(By.xpath("//*[@id='topmenu']/div/ul/li[5]/a/img"));
WebElement contactUs = driver.findElement(By.xpath("//*[@id='topmenu']/div/ul/li[5]/ul/li[9]/a"));
WebElement emailUs = driver.findElement(By.xpath("//*[@id='topmenu']/div/ul/li[5]/ul/li[9]/ul/li[1]/a"));
Actions action = new Actions(driver);
action.moveToElement(aboutUs).moveToElement(contactUs).moveToElement(emailUs).click().perform();
}
}

Note: moveToElement() method Moves the mouse cursor to the middle of the element.


No comments:

Post a Comment