How to perform mouseover function in Selenium WebDriver using Java?

package seleniumLearnWebDriver;

import org.testng.annotations.Test;
import org.testng.annotations.BeforeTest;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.AfterTest;

public class MouseOver {
    WebDriver driver;
      @Test // This is one of the way & littile bit hard
      public void mousehover() {
          driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
          driver.get("http://seleniumlearn.com/");
          driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
          WebElement element = driver.findElement(By.id("menu-336-1"));
          Actions act = new Actions(driver);
          act.moveToElement(element).build().perform();
          driver.findElement(By.linkText("Selenium Quiz")).click();

          } 

// Second sample program and easy way

  @Test
      public void mouseover() throws Exception {
          driver.get("http://seleniumlearn.com/");
          Thread.sleep(3000);

          Actions act=new Actions(driver);

          act.moveToElement(driver.findElement(By.id("menu-336-1"))).build().perform();
          Thread.sleep(3000);
          act.moveToElement(driver.findElement(By.linkText("Selenium Training"))).click().perform();

      }

 

  @BeforeTest
  public void beforeTest() {
    // driver=new FirefoxDriver();
        System.setProperty("webdriver.chrome.driver","D:\\lib\\chromedriver.exe"); //--->chrome browser path
        driver=new ChromeDriver();
        driver.manage().window().maximize();
    }

  @AfterTest
  public void afterTest() {
  }

}

Tags: